Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions python/semantic_kernel/connectors/azure_cosmos_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import ast
import asyncio
import json
import sys
from collections.abc import Sequence
from importlib import metadata
Expand Down Expand Up @@ -795,33 +794,38 @@ async def _inner_search(
if isinstance(where_clauses, str)
else f"WHERE ({' AND '.join(where_clauses)}) "
)
else:
where_clauses = "" # Empty string instead of None
vector_field_name = vector_field.storage_name or vector_field.name
select_clause = self._build_select_clause(options.include_vectors)
params.append({"name": "@vector", "value": vector})
if vector_field.distance_function not in DISTANCE_FUNCTION_MAP_NOSQL:
raise VectorStoreModelException(
f"Distance function '{vector_field.distance_function}' is not supported by Azure Cosmos DB NoSQL."
)
distance_obj = json.dumps({"distanceFunction": DISTANCE_FUNCTION_MAP_NOSQL[vector_field.distance_function]})
# Cosmos DB VectorDistance function only accepts 2 parameters: field and vector
# Distance function is configured in the vector index, not in the query
if search_type == SearchType.VECTOR:
distance_clause = f"VectorDistance(c.{vector_field_name}, @vector, false {distance_obj})"
distance_clause = f"VectorDistance(c.{vector_field_name}, @vector)"
elif search_type == SearchType.KEYWORD_HYBRID:
# Hybrid search: requires both a vector and keywords
params.append({"name": "@keywords", "value": values})
text_field = options.additional_property_name
if not text_field:
raise VectorStoreModelException("Hybrid search requires 'keyword_field_name' in options.")
distance_clause = f"RRF(VectorDistance(c.{vector_field_name}, @vector, false, {distance_obj}), "
f"FullTextScore(c.{text_field}, @keywords))"
distance_clause = (
f"RRF(VectorDistance(c.{vector_field_name}, @vector), FullTextScore(c.{text_field}, @keywords))"
)
else:
raise VectorStoreModelException(f"Search type '{search_type}' is not supported.")
query = (
f"SELECT TOP @top {select_clause}, " # nosec: B608
f"{distance_clause} as {NOSQL_SCORE_PROPERTY_NAME} " # nosec: B608
"FROM c "
f"{where_clauses}" # nosec: B608
f"ORDER BY RANK {distance_clause}" # nosec: B608
f"ORDER BY {distance_clause}" # nosec: B608
)

container_proxy = await self._get_container_proxy(self.collection_name, **kwargs)
try:
results = container_proxy.query_items(query, parameters=params)
Expand Down
Loading
Loading