Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement vector data insertion and querying? #105

Closed
qq312888991 opened this issue Apr 12, 2023 · 6 comments
Closed

How to implement vector data insertion and querying? #105

qq312888991 opened this issue Apr 12, 2023 · 6 comments
Labels
question Further information is requested

Comments

@qq312888991
Copy link

How to call the OpenAI Embeddings API to store vector data and implement vector query functionality?

@shacharPash shacharPash added the question Further information is requested label Apr 16, 2023
@shacharPash
Copy link
Contributor

SearchTests.cs has Vector similarity tests, here are two of them that might help you understand how to implement vector data insertion and querying.
Later I plan to add it to the Examples folder.

Example with HSET:

    [Fact]
    public void QueryingVectorFields()
    {
        IDatabase db = redisFixture.Redis.GetDatabase();
        db.Execute("FLUSHALL");
        var ft = db.FT();

        var schema = new Schema().AddVectorField("v", Schema.VectorField.VectorAlgo.HNSW, new Dictionary<string, object>()
        {
            ["TYPE"] = "FLOAT32",
            ["DIM"] = "2",
            ["DISTANCE_METRIC"] = "L2",
        });

        ft.Create("idx", new FTCreateParams(), schema);

        db.HashSet("a", "v", "aaaaaaaa");
        db.HashSet("b", "v", "aaaabaaa");
        db.HashSet("c", "v", "aaaaabaa");

        var q = new Query("*=>[KNN 2 @v $vec]").ReturnFields("__v_score").Dialect(2);
        var res = ft.Search("idx", q.AddParam("vec", "aaaaaaaa"));
        Assert.Equal(2, res.TotalResults);
    }

Example with JSON.SET:

    [Fact]
    public void VectorSimilaritySearch()
    {
        IDatabase db = redisFixture.Redis.GetDatabase();
        db.Execute("FLUSHALL");
        var ft = db.FT();
        var json = db.JSON();

        json.Set("vec:1", "$", "{\"vector\":[1,1,1,1]}");
        json.Set("vec:2", "$", "{\"vector\":[2,2,2,2]}");
        json.Set("vec:3", "$", "{\"vector\":[3,3,3,3]}");
        json.Set("vec:4", "$", "{\"vector\":[4,4,4,4]}");

        var schema = new Schema().AddVectorField(FieldName.Of("$.vector").As("vector"), Schema.VectorField.VectorAlgo.FLAT, new Dictionary<string, object>()
        {
            ["TYPE"] = "FLOAT32",
            ["DIM"] = "4",
            ["DISTANCE_METRIC"] = "L2",
        });

        var idxDef = new FTCreateParams().On(IndexDataType.JSON).Prefix("vec:");
        Assert.True(ft.Create("vss_idx", idxDef, schema));

        float[] vec = new float[] { 2, 2, 2, 2 };
        byte[] queryVec = MemoryMarshal.Cast<float, byte>(vec).ToArray();


        var query = new Query("*=>[KNN 3 @vector $query_vec]")
                            .AddParam("query_vec", queryVec)
                            .SetSortBy("__vector_score")
                            .Dialect(2);
        var res = ft.Search("vss_idx", query);

        Assert.Equal(3, res.TotalResults);

        Assert.Equal("vec:2", res.Documents[0].Id.ToString());

        Assert.Equal(0, res.Documents[0]["__vector_score"]);

       var jsonRes = res.ToJson();
        Assert.Equal("{\"vector\":[2,2,2,2]}", jsonRes![0]);
    }

I would love to hear if this was helpful, if you have any further questions feel free to write to me.

@qq312888991
Copy link
Author

Why didn’t the following code work? I want to add conditions for other fields except for the sending vector field.

    var base_query = $"(@modelsId:1)=>[{search_type} {number_of_results} @{vector_field_name} $vec_param]";
    var query = new Query(base_query)
                .AddParam("vec_param", embedding.SelectMany(BitConverter.GetBytes).ToArray())
                .Limit(0, number_of_results)
                .Dialect(2);
    string ss = query.QueryString;
    ISearchCommands ft = db.FT();
    var results = ft.Search(index_name, query);

@qq312888991
Copy link
Author

1681893984831

@shacharPash
Copy link
Contributor

Did you create an index using the FT.CREATE command?
I don't see it in the code you sent

@shacharPash
Copy link
Contributor

hey @qq312888991, Did you manage?
If so, I would be happy to close the issue.
If not, feel free to write me what the problem is and I will help you.

@qq312888991
Copy link
Author

qq312888991 commented May 4, 2023

嘿,你成功了吗?如果是这样,我很乐意关闭该问题。如果没有,请随时写信给我问题所在,我会帮助你。

Thank you, it's already done. The code to resolve it is as follows:

    var base_query = $"(@modelsId:{{1}})=>[{search_type} {number_of_results} @{vector_field_name} $vec_param]";

    var query = new Query(base_query)
                .AddParam("vec_param", embedding.SelectMany(BitConverter.GetBytes).ToArray())
                .Limit(0, number_of_results)
                .Dialect(2);
    string ss = query.QueryString;
    ISearchCommands ft = db.FT();
    var results = ft.Search(index_name, query);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants