Skip to content

Commit

Permalink
Switch to query attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasonjo committed Dec 6, 2024
1 parent a1554b5 commit 419d8c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
28 changes: 23 additions & 5 deletions libs/neo4j/langchain_neo4j/vectorstores/neo4j_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ def __init__(
relevance_score_fn: Optional[Callable[[float], float]] = None,
index_type: IndexType = DEFAULT_INDEX_TYPE,
graph: Optional[Neo4jGraph] = None,
effective_search_ratio: int = 1,
) -> None:
try:
import neo4j
Expand Down Expand Up @@ -593,7 +592,7 @@ def __init__(
self.retrieval_query = retrieval_query
self.search_type = search_type
self._index_type = index_type
self.effective_search_ratio = effective_search_ratio

# Calculate embedding dimension
self.embedding_dimension = len(embedding.embed_query("foo"))

Expand Down Expand Up @@ -991,6 +990,7 @@ def similarity_search(
k: int = 4,
params: Dict[str, Any] = {},
filter: Optional[Dict[str, Any]] = None,
effective_search_ratio: int = 1,
**kwargs: Any,
) -> List[Document]:
"""Run similarity search with Neo4jVector.
Expand All @@ -1003,7 +1003,9 @@ def similarity_search(
filter (Optional[Dict[str, Any]]): Dictionary of argument(s) to
filter on metadata.
Defaults to None.
effective_search_ratio (int): Controls the candidate pool size
by multiplying $k to balance query accuracy and performance.
Defaults to 1.
Returns:
List of Documents most similar to the query.
"""
Expand All @@ -1014,6 +1016,7 @@ def similarity_search(
query=query,
params=params,
filter=filter,
effective_search_ratio=effective_search_ratio,
**kwargs,
)

Expand All @@ -1023,6 +1026,7 @@ def similarity_search_with_score(
k: int = 4,
params: Dict[str, Any] = {},
filter: Optional[Dict[str, Any]] = None,
effective_search_ratio: int = 1,
**kwargs: Any,
) -> List[Tuple[Document, float]]:
"""Return docs most similar to query.
Expand All @@ -1035,6 +1039,9 @@ def similarity_search_with_score(
filter (Optional[Dict[str, Any]]): Dictionary of argument(s) to
filter on metadata.
Defaults to None.
effective_search_ratio (int): Controls the candidate pool size
by multiplying $k to balance query accuracy and performance.
Defaults to 1.
Returns:
List of Documents most similar to the query and score for each
Expand All @@ -1046,6 +1053,7 @@ def similarity_search_with_score(
query=query,
params=params,
filter=filter,
effective_search_ratio=effective_search_ratio,
**kwargs,
)
return docs
Expand All @@ -1056,6 +1064,7 @@ def similarity_search_with_score_by_vector(
k: int = 4,
filter: Optional[Dict[str, Any]] = None,
params: Dict[str, Any] = {},
effective_search_ratio: int = 1,
**kwargs: Any,
) -> List[Tuple[Document, float]]:
"""
Expand All @@ -1076,6 +1085,9 @@ def similarity_search_with_score_by_vector(
Defaults to None.
params (Dict[str, Any]): The search params for the index type.
Defaults to empty dict.
effective_search_ratio (int): Controls the candidate pool size
by multiplying $k to balance query accuracy and performance.
Defaults to 1.
Returns:
List[Tuple[Document, float]]: A list of tuples, each containing
Expand Down Expand Up @@ -1161,7 +1173,7 @@ def similarity_search_with_score_by_vector(
"embedding": embedding,
"keyword_index": self.keyword_index_name,
"query": remove_lucene_chars(kwargs["query"]),
"ef": self.effective_search_ratio,
"ef": effective_search_ratio,
**params,
**filter_params,
}
Expand Down Expand Up @@ -1217,6 +1229,7 @@ def similarity_search_by_vector(
k: int = 4,
filter: Optional[Dict[str, Any]] = None,
params: Dict[str, Any] = {},
effective_search_ratio: int = 1,
**kwargs: Any,
) -> List[Document]:
"""Return docs most similar to embedding vector.
Expand All @@ -1234,7 +1247,12 @@ def similarity_search_by_vector(
List of Documents most similar to the query vector.
"""
docs_and_scores = self.similarity_search_with_score_by_vector(
embedding=embedding, k=k, filter=filter, params=params, **kwargs
embedding=embedding,
k=k,
filter=filter,
params=params,
effective_search_ratio=effective_search_ratio,
**kwargs,
)
return [doc for doc, _ in docs_and_scores]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1003,11 +1003,16 @@ def test_neo4jvector_effective_search_ratio() -> None:
username=username,
password=password,
pre_delete_collection=True,
effective_search_ratio=2,
)
output = docsearch.similarity_search("foo", k=2)
output = docsearch.similarity_search("foo", k=2, effective_search_ratio=2)
assert len(output) == 2

output1 = docsearch.similarity_search_with_score(
"foo", k=2, effective_search_ratio=2
)
assert len(output1) == 2
# Assert ordered by score
assert output1[0][1] > output1[1][1]
drop_vector_indexes(docsearch)


Expand Down

0 comments on commit 419d8c7

Please sign in to comment.