txtai icon indicating copy to clipboard operation
txtai copied to clipboard

A .Net binding for txtai?

Open oliver021 opened this issue 2 years ago • 19 comments

Hello community, it would be very nice to make a .Net binding. Where can I find a guide for that? So please if anyone can give me some instructions on how to get in that would be great. Of course, I would also like to know if this binding issue is only handled by maintainers or if it is something open to the community.

oliver021 avatar May 24 '22 19:05 oliver021

Thank you for reaching out. A .NET binding would be great.

Currently there are API bindings for Go, Java, JavaScript and Rust. If you're familiar with any of those languages, looking at those projects is the best resource on creating a new binding.

If you do end up creating a .NET binding, I'd be happy to add a link to it here - https://neuml.github.io/txtai/api/#supported-language-bindings

davidmezzetti avatar May 25 '22 11:05 davidmezzetti

Hi, it's great. I shall to tell you that library is great job from you. As soon as possible I want to start develop a binding for .Net; now I am doing research how works the others bindings. So I going to bring news about this issue very soon.

oliver021 avatar May 25 '22 16:05 oliver021

Thank you for the kind words. Looking forward to hearing about the .NET bindings. Feel free to reach out if you have any questions.

davidmezzetti avatar May 25 '22 19:05 davidmezzetti

I think rust is a suitable option to create the .NET interop. I wonder what others think?

GeorgeS2019 avatar May 28 '22 20:05 GeorgeS2019

I don't know about how to build .Net bindings from Rust source. Since I don't know Rust so I can't say if it's possible or feasible, I do know that there is an API for interoperability between .Net and other assemblies, especially dynamic link libraries using P/Invoke. My opinion is that as long as you can code in C#/VB.Net/F# to produce .Net assemblies, you're better off. Of course there are plenty of exceptions to this rule. But I don't think this is the case.

oliver021 avatar May 29 '22 01:05 oliver021

Thank you for the thoughts and comments on this. Originally when I saw this issue, I thought the bindings would be for the txtai API. Basically make these methods available over HTTP. Is the intention something else?

davidmezzetti avatar May 29 '22 12:05 davidmezzetti

No, speed is the aim. Not WebAPI.

GeorgeS2019 avatar May 29 '22 17:05 GeorgeS2019

Basically make these methods available over HTTP. Is the intention something else?

Yes, I thought the same thing, this is the idea that I have, it's basically to make a HTTP client to request the txtai server, or not?

image

In this case, I just need schemas to code a client for txtai over HTTP, but it's here: https://neuml.github.io/txtai/api/methods/ The above link is very important to understand how to code a HTTP client.

oliver021 avatar May 29 '22 20:05 oliver021

Hi guys, I bring a code to show how I going to make my solution:

public class Similarity
    {
        /// <summary>
        /// The client used to communicate with the TxtAI API.
        /// </summary>
        private readonly HttpClient client;

        /// <summary>
        /// The serializer used to serialize and deserialize the JSON.
        /// </summary>
        private readonly IApiSerializer serializer;

        /// <summary>
        /// Construct to inject HttpClient.
        /// </summary>
        /// <param name="client">HttpClient</param>
        public Similarity(HttpClient client, IApiSerializer serializer) {
            this.client = client;
            this.serializer = serializer;
        }

        /// <summary>
        /// Computes the similarity between query and list of text. Returns a list of
        /// {id: value, score: value} sorted by highest score, where id is the index
        /// in texts.
        /// </summary>
        /// <param name="query">The text to be compared.</param>
        /// <param name="texts">list of text.</param>
        /// <returns>list of <see cref="IndexResult"/></returns>
        public async Task<List<IndexResult>> SimilarityRequest(string query, List<string> texts) {
            // convert to json the query and texts
            var body = new {
                query,
                texts
            };

            // serialize the json
            var json = this.serializer.Serialize(body);

            // post the json to the TxtAI API
            var response = await this.client.PostAsync("/similarity", new StringContent(json, System.Text.Encoding.UTF8, "application/json"));

            // deserialize the json
            return this.serializer.Deserialize<List<IndexResult>>(await response.Content.ReadAsStringAsync());
        }

        /// <summary>
        /// Computes the similarity between list of queries and list of text. Returns a list
        /// * of {id: value, score: value} sorted by highest score per query, where id is the
        /// * index in texts.
        /// </summary>
        /// <param name="queries">list of text.</param>
        /// <param name="texts">list of text.</param>
        /// <returns>list of <see cref="IndexResult"/></returns>
        public async Task<List<IndexResult>> SimilarityRequest(List<string> queries, List<string> texts) {
            // convert to json the queries and texts
            var body = new {
                queries,
                texts
            };

            // serialize the json
            var json = this.serializer.Serialize(body);

            // post the json to the TxtAI API
            var response = await this.client.PostAsync("/similarity", new StringContent(json, System.Text.Encoding.UTF8, "application/json"));

            // deserialize the json
            return this.serializer.Deserialize<List<IndexResult>>(await response.Content.ReadAsStringAsync());
        }
    }

I will declare consts to http path, at the moment this may be the final results, the java binding help me a lot 'cause is a type static language like C#, so I think that this is the most coherent about other bindings. Please, give me feedback, If is this correct I going to finish really soon.

oliver021 avatar May 29 '22 23:05 oliver021

This looks great @oliver021. Once you have a GitHub repo ready, let me know and I'll take a look.

davidmezzetti avatar May 30 '22 00:05 davidmezzetti

Hi David, this is a initial view of the final job, I can't test all methods but you can take a look now. https://github.com/oliver021/txtai.net

oliver021 avatar Jun 04 '22 20:06 oliver021

Thank you @oliver021! I'll take a look at the project and try to get it up and running. Will let you know.

davidmezzetti avatar Jun 06 '22 13:06 davidmezzetti

Hi David, how you look the project?

oliver021 avatar Jul 06 '22 18:07 oliver021

Thank you for following up.

I have taken a look at the code and it all looks like it would work. I just haven't have the time to spin up a .NET environment to test it out but plan to do so within the next week.

davidmezzetti avatar Jul 07 '22 20:07 davidmezzetti

Thank you.

I am very interested in this project, txtai is a really good work. So I will continue follow up.

oliver021 avatar Jul 08 '22 04:07 oliver021

Hi @oliver021, I've dug through the code in more detail and it looks like it is porting everything over as I would expect. Thank you once again for putting this together!

Do you have instructions on how to get this up and running? I see the solution files in the project but the README is empty. Did you intend for this library to be cross platform (i.e. can a developer on Linux use it?)? Is Visual Studio the only option to compile or can it be done other ways?

davidmezzetti avatar Jul 14 '22 16:07 davidmezzetti

Yes David, there are severals options to run this code, It's cross platform too. Le me prepare instructions for you,

oliver021 avatar Jul 14 '22 18:07 oliver021

Thank you!

davidmezzetti avatar Jul 14 '22 19:07 davidmezzetti

Hello, just wanted to follow up and see if this was still planned.

davidmezzetti avatar Aug 30 '22 13:08 davidmezzetti

Closing due to inactivity

davidmezzetti avatar Oct 12 '22 13:10 davidmezzetti