-
Notifications
You must be signed in to change notification settings - Fork 39
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
Comments
SearchTests.cs has Vector similarity tests, here are two of them that might help you understand how to implement vector data insertion and querying. 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. |
Why didn’t the following code work? I want to add conditions for other fields except for the sending vector field.
|
Did you create an index using the FT.CREATE command? |
hey @qq312888991, Did you manage? |
Thank you, it's already done. The code to resolve it is as follows:
|
How to call the OpenAI Embeddings API to store vector data and implement vector query functionality?
The text was updated successfully, but these errors were encountered: