redis / redis-om-spring

Spring Data Redis extensions for better search, documents models, and more
MIT License
604 stars 93 forks source link

Vector Search example using redis-om-spring #432

Closed shannonantony closed 5 months ago

shannonantony commented 5 months ago

@bsbodden Thanks again for all your help.

I am trying to implement vector search based on a natural language input. How can I extend the ROMS-VSS sample to accept 'natural language' input for the 'productText' field and retrieve matching products based on sentenceEmbedding vector search? Basically I need to convert the natural language input to a vector and search against sentenceEmbedding vector and retrieve matching products.

Any guidance will be highly appreciated.

thanks

Jay

bsbodden commented 5 months ago

The example already does that... In Product you have a sentenceEmbedding:

https://github.com/redis/redis-om-spring/blob/main/demos/roms-vss/src/main/java/com/redis/om/vss/domain/Product.java#L87

In the controller we do a similarity search using the value of that sentence embedding to find similar items:

https://github.com/redis/redis-om-spring/blob/main/demos/roms-vss/src/main/java/com/redis/om/vss/controllers/ProductController.java#L105

To use your own input query, rather than then pre-computed embedding for the text of another product you would take you natural language query, create an embedding and use that for the similarity search.

RomsVSS Product->Description->Embedding->Search for Products with similar embeddings for Description

What you are asking: Input Query->Embedding->Search for Products with similar embeddings for Description

Hope that helps

shannonantony commented 5 months ago

@bsbodden Thanks. I am specifically trying to figure out which Java API method I should use to convert the natural language query input to vector embeddings. Can you please provide sample code for that?

Should I use something like this?

float[] sentenceEmbed = featureExtractor.getSentenceEmbeddingAsFloatArrayFor(query);

thanks again

Jay

shannonantony commented 5 months ago

@bsbodden , Finally I could make the application running. However the quality of similarity is not meeting the expectation. Is there any way I could tune the parameters to get better results. If I define 2 vectorize fields- field1 & field2, How do I implement the method where input query matches with either field1 or field2

thanks Jay

bsbodden commented 5 months ago

Jay, glad you got it work. The similarity perceive accuracy depends on the model that's being used and the data being vectorized. Right now we have 1 model set as the default for each for the vectorization types. But if you find a model that works via DJL you can set a different model in a properties file. I plan to integrate more tightly with Spring AI soon.

shannonantony commented 5 months ago

Thanks @bsbodden . Integration with Spring AI would be great. can you give me the property syntax to reference other models. thanks Jay