spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/index.html
Apache License 2.0
2.87k stars 720 forks source link

Spring AI using Milvus Vector store throwing Keyname 'distance' not found #1256

Open sahilavasthi opened 3 weeks ago

sahilavasthi commented 3 weeks ago

Building a RAG application using Spring AI 1.0.0.M1 version, JDK 21, Ollama & Milvus v2.4.0 vector store running as a docker container using docker compose. Please note, I am creating a new Collection in default database of Milvus using milvus sdk

Application is able to insert the document; however when I execute below statement for the retrieval List documents = vectorStore.similaritySearch(SearchRequest.defaults() .query(question.question()).withTopK(5));

I get following exception; key name is 'distance' in below exception

io.milvus.exception.ParamException: The key name is not found at io.milvus.response.QueryResultsWrapper$RowRecord.get(QueryResultsWrapper.java:158) ~[milvus-sdk-java-2.4.2.jar!/:na] at org.springframework.ai.vectorstore.MilvusVectorStore.getResultSimilarity(MilvusVectorStore.java:373) ~[spring-ai-milvus-store-1.0.0-M1.jar!/:na] at org.springframework.ai.vectorstore.MilvusVectorStore.lambda$similaritySearch$1(MilvusVectorStore.java:360) ~[spring-ai-milvus-store-1.0.0-M1.jar!/:na] at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:178) ~[na:na] at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1708) ~[na:na] at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na] at

Created a new Collection in the default Milvus vector store as shown below,

 MilvusClientV2 client = new MilvusClientV2(connectConfig);

CreateCollectionReq.CollectionSchema schema = client.createSchema();

schema.addField(AddFieldReq.builder()
        .fieldName("embedding")
        .dataType(DataType.FloatVector)
        .dimension(1024)
        .description("vector embedding")
        .build());

schema.addField(AddFieldReq.builder()
        .fieldName("id")
        .dataType(DataType.Int64)
        .isPrimaryKey(true)
        .autoID(true)
        .build());

CreateCollectionReq quickSetupReq = CreateCollectionReq.builder()
        .collectionName(collection_name)
        .collectionSchema(schema)
        .dimension(1024)
        .vectorFieldName("embedding")
        .metricType("COSINE")
        .enableDynamicField(true)
        .build();

client.createCollection(quickSetupReq);

IndexParam indexParamForVectorField = IndexParam.builder()
        .fieldName("embedding")
        .indexName("vector_index")
        .indexType(IndexParam.IndexType.IVF_FLAT)
        .metricType(IndexParam.MetricType.COSINE)
        .extraParams(Map.of("nlist", 1, "nprobe", 1))
        .build();

List<IndexParam> indexParams = new ArrayList<>();
indexParams.add(indexParamForVectorField);

CreateIndexReq createIndexReq = CreateIndexReq.builder()
        .collectionName(collection_name)
        .indexParams(indexParams)
        .build();

client.createIndex(createIndexReq);

I pass this new collection under vectorstore section of applications.yaml file so that it can pick up this newly created collection


Is this a bug in Spring AI 1.0.0.M1 version Or I should do something else.

Thanks.

markpollack commented 3 weeks ago

M1 has been out for a while, can you try again snapshot dep?