chore: format
This commit is contained in:
1 parent
3c986adeda
commit
5990c51ab5
13 files changed
+82
-24
No files matched your search
@@ -69,7 +69,11 @@ class ChromaClient(VectorDBBase):
|
|||||||
return self.client.delete_collection(name=collection_name)
|
return self.client.delete_collection(name=collection_name)
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: list[list[float | int]], filter: Optional[dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: list[list[float | int]],
|
||||||
|
filter: Optional[dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
# Search for the nearest neighbor items based on the vectors and return 'limit' number of results.
|
# Search for the nearest neighbor items based on the vectors and return 'limit' number of results.
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -153,7 +153,11 @@ class ElasticsearchClient(VectorDBBase):
|
|||||||
|
|
||||||
# Status: works
|
# Status: works
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: list[list[float]], filter: Optional[dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: list[list[float]],
|
||||||
|
filter: Optional[dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
query = {
|
query = {
|
||||||
"size": limit,
|
"size": limit,
|
||||||
|
|||||||
@@ -179,7 +179,11 @@ class MilvusClient(VectorDBBase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: list[list[float | int]], filter: Optional[dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: list[list[float | int]],
|
||||||
|
filter: Optional[dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
# Search for the nearest neighbor items based on the vectors and return 'limit' number of results.
|
# Search for the nearest neighbor items based on the vectors and return 'limit' number of results.
|
||||||
collection_name = collection_name.replace("-", "_")
|
collection_name = collection_name.replace("-", "_")
|
||||||
|
|||||||
@@ -157,7 +157,11 @@ class MilvusClient(VectorDBBase):
|
|||||||
collection.insert(entities)
|
collection.insert(entities)
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: List[List[float]], filter: Optional[Dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: List[List[float]],
|
||||||
|
filter: Optional[Dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
if not vectors:
|
if not vectors:
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -113,7 +113,11 @@ class OpenSearchClient(VectorDBBase):
|
|||||||
self.client.indices.delete(index=self._get_index_name(collection_name))
|
self.client.indices.delete(index=self._get_index_name(collection_name))
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: list[list[float | int]], filter: Optional[dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: list[list[float | int]],
|
||||||
|
filter: Optional[dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
try:
|
try:
|
||||||
if not self.has_collection(collection_name):
|
if not self.has_collection(collection_name):
|
||||||
|
|||||||
@@ -521,7 +521,11 @@ class Oracle23aiClient(VectorDBBase):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: List[List[Union[float, int]]], filter: Optional[dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: List[List[Union[float, int]]],
|
||||||
|
filter: Optional[dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
"""
|
"""
|
||||||
Search for similar vectors in the database.
|
Search for similar vectors in the database.
|
||||||
|
|||||||
@@ -487,20 +487,27 @@ class PgvectorClient(VectorDBBase):
|
|||||||
if PGVECTOR_PGCRYPTO:
|
if PGVECTOR_PGCRYPTO:
|
||||||
where_clauses.append(
|
where_clauses.append(
|
||||||
pgcrypto_decrypt(
|
pgcrypto_decrypt(
|
||||||
DocumentChunk.vmetadata, PGVECTOR_PGCRYPTO_KEY, JSONB
|
DocumentChunk.vmetadata,
|
||||||
|
PGVECTOR_PGCRYPTO_KEY,
|
||||||
|
JSONB,
|
||||||
)[key].astext.in_([str(v) for v in in_values])
|
)[key].astext.in_([str(v) for v in in_values])
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
where_clauses.append(
|
where_clauses.append(
|
||||||
DocumentChunk.vmetadata[key].astext.in_([str(v) for v in in_values])
|
DocumentChunk.vmetadata[key].astext.in_(
|
||||||
|
[str(v) for v in in_values]
|
||||||
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# Handle simple equality: {"field": "value"}
|
# Handle simple equality: {"field": "value"}
|
||||||
if PGVECTOR_PGCRYPTO:
|
if PGVECTOR_PGCRYPTO:
|
||||||
where_clauses.append(
|
where_clauses.append(
|
||||||
pgcrypto_decrypt(
|
pgcrypto_decrypt(
|
||||||
DocumentChunk.vmetadata, PGVECTOR_PGCRYPTO_KEY, JSONB
|
DocumentChunk.vmetadata,
|
||||||
)[key].astext == str(value)
|
PGVECTOR_PGCRYPTO_KEY,
|
||||||
|
JSONB,
|
||||||
|
)[key].astext
|
||||||
|
== str(value)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
where_clauses.append(
|
where_clauses.append(
|
||||||
|
|||||||
@@ -391,7 +391,11 @@ class PineconeClient(VectorDBBase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: List[List[Union[float, int]]], filter: Optional[dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: List[List[Union[float, int]]],
|
||||||
|
filter: Optional[dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
"""Search for similar vectors in a collection."""
|
"""Search for similar vectors in a collection."""
|
||||||
if not vectors or not vectors[0]:
|
if not vectors or not vectors[0]:
|
||||||
|
|||||||
@@ -145,7 +145,11 @@ class QdrantClient(VectorDBBase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: list[list[float | int]], filter: Optional[dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: list[list[float | int]],
|
||||||
|
filter: Optional[dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
# Search for the nearest neighbor items based on the vectors and return 'limit' number of results.
|
# Search for the nearest neighbor items based on the vectors and return 'limit' number of results.
|
||||||
if limit is None:
|
if limit is None:
|
||||||
|
|||||||
@@ -254,7 +254,11 @@ class QdrantClient(VectorDBBase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: List[List[float | int]], filter: Optional[Dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: List[List[float | int]],
|
||||||
|
filter: Optional[Dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
"""
|
"""
|
||||||
Search for the nearest neighbor items based on the vectors with tenant isolation.
|
Search for the nearest neighbor items based on the vectors with tenant isolation.
|
||||||
|
|||||||
@@ -295,7 +295,11 @@ class S3VectorClient(VectorDBBase):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: List[List[Union[float, int]]], filter: Optional[dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: List[List[Union[float, int]]],
|
||||||
|
filter: Optional[dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
"""
|
"""
|
||||||
Search for similar vectors in a collection using multiple query vectors.
|
Search for similar vectors in a collection using multiple query vectors.
|
||||||
|
|||||||
@@ -159,7 +159,11 @@ class WeaviateClient(VectorDBBase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, collection_name: str, vectors: List[List[Union[float, int]]], filter: Optional[dict] = None, limit: int = 10
|
self,
|
||||||
|
collection_name: str,
|
||||||
|
vectors: List[List[Union[float, int]]],
|
||||||
|
filter: Optional[dict] = None,
|
||||||
|
limit: int = 10,
|
||||||
) -> Optional[SearchResult]:
|
) -> Optional[SearchResult]:
|
||||||
sane_collection_name = self._sanitize_collection_name(collection_name)
|
sane_collection_name = self._sanitize_collection_name(collection_name)
|
||||||
if not self.client.collections.exists(sane_collection_name):
|
if not self.client.collections.exists(sane_collection_name):
|
||||||
|
|||||||
@@ -1624,7 +1624,11 @@ async def query_knowledge_bases(
|
|||||||
|
|
||||||
if search_results and search_results.ids and search_results.ids[0]:
|
if search_results and search_results.ids and search_results.ids[0]:
|
||||||
result_ids = search_results.ids[0]
|
result_ids = search_results.ids[0]
|
||||||
result_distances = search_results.distances[0] if search_results.distances else [0] * len(result_ids)
|
result_distances = (
|
||||||
|
search_results.distances[0]
|
||||||
|
if search_results.distances
|
||||||
|
else [0] * len(result_ids)
|
||||||
|
)
|
||||||
|
|
||||||
for knowledge_base_id, distance in zip(result_ids, result_distances):
|
for knowledge_base_id, distance in zip(result_ids, result_distances):
|
||||||
if knowledge_base_id in seen_ids:
|
if knowledge_base_id in seen_ids:
|
||||||
@@ -1634,7 +1638,9 @@ async def query_knowledge_bases(
|
|||||||
if len(top_results_heap) < count:
|
if len(top_results_heap) < count:
|
||||||
heapq.heappush(top_results_heap, (distance, knowledge_base_id))
|
heapq.heappush(top_results_heap, (distance, knowledge_base_id))
|
||||||
elif distance > top_results_heap[0][0]:
|
elif distance > top_results_heap[0][0]:
|
||||||
heapq.heapreplace(top_results_heap, (distance, knowledge_base_id))
|
heapq.heapreplace(
|
||||||
|
top_results_heap, (distance, knowledge_base_id)
|
||||||
|
)
|
||||||
|
|
||||||
page_offset += page_size
|
page_offset += page_size
|
||||||
if len(accessible_knowledge_bases.items) < page_size:
|
if len(accessible_knowledge_bases.items) < page_size:
|
||||||
@@ -1649,16 +1655,17 @@ async def query_knowledge_bases(
|
|||||||
for distance, knowledge_base_id in sorted_results:
|
for distance, knowledge_base_id in sorted_results:
|
||||||
knowledge_base = Knowledges.get_knowledge_by_id(knowledge_base_id)
|
knowledge_base = Knowledges.get_knowledge_by_id(knowledge_base_id)
|
||||||
if knowledge_base:
|
if knowledge_base:
|
||||||
matching_knowledge_bases.append({
|
matching_knowledge_bases.append(
|
||||||
"id": knowledge_base.id,
|
{
|
||||||
"name": knowledge_base.name,
|
"id": knowledge_base.id,
|
||||||
"description": knowledge_base.description or "",
|
"name": knowledge_base.name,
|
||||||
"similarity": round(distance, 4),
|
"description": knowledge_base.description or "",
|
||||||
})
|
"similarity": round(distance, 4),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
return json.dumps(matching_knowledge_bases, ensure_ascii=False)
|
return json.dumps(matching_knowledge_bases, ensure_ascii=False)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception(f"query_knowledge_bases error: {e}")
|
log.exception(f"query_knowledge_bases error: {e}")
|
||||||
return json.dumps({"error": str(e)})
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
Reference in new issue
Block a user