fix(rag): normalize Milvus L2 retrieval scores - #3070
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
oss-maintainer
left a comment
There was a problem hiding this comment.
Summary
Right problem, right direction: raw Milvus L2 distances were fed straight into the higher-is-better score contract and then compared against scoreThreshold, so L2 collections filtered essentially backwards. Passing the configured metric to SearchReq and normalizing before thresholding fixes it, with tests for conversion, propagation and ordering. Two compatibility asks: validate configured metric against the existing collection index, and call out the threshold retuning in a changelog.
Automated review by github-manager-bot
| .databaseName(databaseName) | ||
| .collectionName(collectionName) | ||
| .data(Collections.singletonList(queryVector)) | ||
| .metricType(metricType) |
There was a problem hiding this comment.
Passing metricType explicitly to SearchReq is new behaviour: previously Milvus searched with the metric recorded on the collection index, now it searches with the configured metric. When a store connects to an existing collection whose index metric differs from the builder default (COSINE), this turns a previously-working search into a provider-side error or wrong ranking. Could you validate the configured metric against describeIndex/describeCollection at connect time and fail fast with a message naming both values? The javadoc note alone will be easy to miss for people who configured the store before this change.
| * scores in the expected direction and are left unchanged. | ||
| * | ||
| * @param rawScore the raw score or distance returned by Milvus | ||
| * @return a higher-is-better score |
There was a problem hiding this comment.
1 / (1 + distance) is a reasonable monotone map into (0, 1], and applying the threshold after normalization is the correct order. This is still a breaking change for anyone who already tuned scoreThreshold against raw L2 distances, and the resulting scores are not comparable with COSINE/IP scores. Worth a changelog entry plus a note in the RAG docs that thresholds must be re-tuned for L2 collections.
Summary
SearchReq.scoreThresholdafter score normalization.Why
Milvus L2 search returns distances where smaller values indicate greater similarity. AgentScope retrieval uses a higher-is-better score contract, so raw L2 distances must be adapted before they are exposed as document scores.
This PR uses
1 / (1 + distance)as an AgentScope-side normalization formula. This formula is not a Milvus-defined conversion.Validation
MilvusStoreTest: 65 tests passed.git diff --checkpassed.References
Fixes #3069