fix: return empty list instead of raising exception for qdrant search when score_threshold is 1 (#24032)
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -197,6 +197,8 @@ sdks/python-client/dify_client.egg-info
|
|||||||
!.vscode/README.md
|
!.vscode/README.md
|
||||||
pyrightconfig.json
|
pyrightconfig.json
|
||||||
api/.vscode
|
api/.vscode
|
||||||
|
# vscode Code History Extension
|
||||||
|
.history
|
||||||
|
|
||||||
.idea/
|
.idea/
|
||||||
|
|
||||||
|
@@ -331,6 +331,12 @@ class QdrantVector(BaseVector):
|
|||||||
def search_by_vector(self, query_vector: list[float], **kwargs: Any) -> list[Document]:
|
def search_by_vector(self, query_vector: list[float], **kwargs: Any) -> list[Document]:
|
||||||
from qdrant_client.http import models
|
from qdrant_client.http import models
|
||||||
|
|
||||||
|
score_threshold = float(kwargs.get("score_threshold") or 0.0)
|
||||||
|
if score_threshold >= 1:
|
||||||
|
# return empty list because some versions of qdrant may response with 400 bad request,
|
||||||
|
# and at the same time, the score_threshold with value 1 may be valid for other vector stores
|
||||||
|
return []
|
||||||
|
|
||||||
filter = models.Filter(
|
filter = models.Filter(
|
||||||
must=[
|
must=[
|
||||||
models.FieldCondition(
|
models.FieldCondition(
|
||||||
@@ -355,7 +361,7 @@ class QdrantVector(BaseVector):
|
|||||||
limit=kwargs.get("top_k", 4),
|
limit=kwargs.get("top_k", 4),
|
||||||
with_payload=True,
|
with_payload=True,
|
||||||
with_vectors=True,
|
with_vectors=True,
|
||||||
score_threshold=float(kwargs.get("score_threshold") or 0.0),
|
score_threshold=score_threshold,
|
||||||
)
|
)
|
||||||
docs = []
|
docs = []
|
||||||
for result in results:
|
for result in results:
|
||||||
@@ -363,7 +369,6 @@ class QdrantVector(BaseVector):
|
|||||||
continue
|
continue
|
||||||
metadata = result.payload.get(Field.METADATA_KEY.value) or {}
|
metadata = result.payload.get(Field.METADATA_KEY.value) or {}
|
||||||
# duplicate check score threshold
|
# duplicate check score threshold
|
||||||
score_threshold = float(kwargs.get("score_threshold") or 0.0)
|
|
||||||
if result.score > score_threshold:
|
if result.score > score_threshold:
|
||||||
metadata["score"] = result.score
|
metadata["score"] = result.score
|
||||||
doc = Document(
|
doc = Document(
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
from core.rag.datasource.vdb.qdrant.qdrant_vector import QdrantConfig, QdrantVector
|
from core.rag.datasource.vdb.qdrant.qdrant_vector import QdrantConfig, QdrantVector
|
||||||
|
from core.rag.models.document import Document
|
||||||
from tests.integration_tests.vdb.test_vector_store import (
|
from tests.integration_tests.vdb.test_vector_store import (
|
||||||
AbstractVectorTest,
|
AbstractVectorTest,
|
||||||
setup_mock_redis,
|
setup_mock_redis,
|
||||||
@@ -18,6 +19,14 @@ class QdrantVectorTest(AbstractVectorTest):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def search_by_vector(self):
|
||||||
|
super().search_by_vector()
|
||||||
|
# only test for qdrant, may not work on other vector stores
|
||||||
|
hits_by_vector: list[Document] = self.vector.search_by_vector(
|
||||||
|
query_vector=self.example_embedding, score_threshold=1
|
||||||
|
)
|
||||||
|
assert len(hits_by_vector) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_qdrant_vector(setup_mock_redis):
|
def test_qdrant_vector(setup_mock_redis):
|
||||||
QdrantVectorTest().run_all_tests()
|
QdrantVectorTest().run_all_tests()
|
||||||
|
Reference in New Issue
Block a user