Skip to content

Commit

Permalink
Allow ndarray as query vector input
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
maxjakob committed Apr 24, 2024
1 parent f573a52 commit 60c09da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libs/elasticsearch/langchain_elasticsearch/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ def query(
}

# Embedding provided via the embedding function
if query_vector and not self.query_model_id:
knn["query_vector"] = query_vector
if query_vector is not None and not self.query_model_id:
knn["query_vector"] = list(query_vector)

# Case 2: Used when model has been deployed to
# Elasticsearch and can infer the query vector from the query text
Expand Down
35 changes: 35 additions & 0 deletions libs/elasticsearch/tests/integration_tests/test_vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uuid
from typing import Any, Dict, Generator, List, Union

import numpy as np
import pytest
from elasticsearch import NotFoundError
from elasticsearch.helpers import BulkIndexError
Expand Down Expand Up @@ -465,6 +466,40 @@ def assert_query(query_body: dict, query: str) -> dict:
output = docsearch.similarity_search("foo", k=1, custom_query=assert_query)
assert output == [Document(page_content="foo")]

def test_similarity_search_approx_by_vector(
self, elasticsearch_connection: dict, index_name: str
) -> None:
"""Test end to end construction and search with metadata."""
texts = ["foo", "bar", "baz"]
embeddings = ConsistentFakeEmbeddings()
docsearch = ElasticsearchStore.from_texts(
texts,
embedding=embeddings,
**elasticsearch_connection,
index_name=index_name,
)
query_vector = embeddings.embed_query("foo")

def assert_query(query_body: dict, query: str) -> dict:
assert query_body == {
"knn": {
"field": "vector",
"filter": [],
"k": 1,
"num_candidates": 50,
"query_vector": query_vector,
},
}
return query_body

# accept ndarray as query vector
output = docsearch.similarity_search_by_vector_with_relevance_scores(
np.array(query_vector), # type: ignore
k=1,
custom_query=assert_query,
)
assert output == [(Document(page_content="foo"), 1.0)]

def test_similarity_search_approx_with_hybrid_search_rrf(
self, es_client: Any, elasticsearch_connection: dict, index_name: str
) -> None:
Expand Down

0 comments on commit 60c09da

Please sign in to comment.