-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from nulib/deploy/staging
Deploy to production
- Loading branch information
Showing
16 changed files
with
194 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from langchain_core.documents import Document | ||
from langchain_core.vectorstores import VectorStore | ||
from opensearchpy import OpenSearch | ||
from typing import Any, List, Tuple | ||
|
||
|
||
class OpenSearchNeuralSearch(VectorStore): | ||
"""Read-only OpenSearch vectorstore with neural search.""" | ||
|
||
def __init__( | ||
self, | ||
client: None, | ||
endpoint: str, | ||
index: str, | ||
model_id: str, | ||
vector_field: str = "embedding", | ||
search_pipeline: str = None, | ||
text_field: str = "id", | ||
**kwargs: Any, | ||
): | ||
self.client = client or OpenSearch( | ||
hosts=[{"host": endpoint, "port": "443", "use_ssl": True}], **kwargs | ||
) | ||
self.index = index | ||
self.model_id = model_id | ||
self.vector_field = vector_field | ||
self.search_pipeline = search_pipeline | ||
self.text_field = text_field | ||
|
||
def similarity_search( | ||
self, query: str, k: int = 10, subquery: Any = None, **kwargs: Any | ||
) -> List[Document]: | ||
"""Return docs most similar to the embedding vector.""" | ||
docs_with_scores = self.similarity_search_with_score( | ||
query, k, subquery, **kwargs | ||
) | ||
return [doc[0] for doc in docs_with_scores] | ||
|
||
def similarity_search_with_score( | ||
self, query: str, k: int = 10, subquery: Any = None, **kwargs: Any | ||
) -> List[Tuple[Document, float]]: | ||
"""Return docs most similar to query.""" | ||
dsl = { | ||
"size": k, | ||
"query": { | ||
"hybrid": { | ||
"queries": [ | ||
{ | ||
"neural": { | ||
self.vector_field: { | ||
"query_text": query, | ||
"model_id": self.model_id, | ||
"k": k, | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
} | ||
|
||
if subquery: | ||
dsl["query"]["hybrid"]["queries"].append(subquery) | ||
|
||
for key, value in kwargs.items(): | ||
dsl[key] = value | ||
|
||
response = self.client.search(index=self.index, body=dsl, params={"search_pipeline": self.search_pipeline} if self.search_pipeline else None) | ||
|
||
documents_with_scores = [ | ||
( | ||
Document( | ||
page_content=hit["_source"][self.text_field], | ||
metadata=(hit["_source"]), | ||
), | ||
hit["_score"], | ||
) | ||
for hit in response["hits"]["hits"] | ||
] | ||
|
||
return documents_with_scores | ||
|
||
def add_texts(self, texts: List[str], metadatas: List[dict], **kwargs: Any) -> None: | ||
pass | ||
|
||
@classmethod | ||
def from_texts(cls, texts: List[str], metadatas: List[dict], **kwargs: Any) -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# ruff: noqa: E402 | ||
import sys | ||
sys.path.append('./src') | ||
|
||
from unittest import TestCase | ||
from handlers.opensearch_neural_search import OpenSearchNeuralSearch | ||
from langchain_core.documents import Document | ||
|
||
class MockClient(): | ||
def search(self, index, body, params): | ||
return { | ||
"hits": { | ||
"hits": [ | ||
{ | ||
"_source": { | ||
"id": "test" | ||
}, | ||
"_score": 0.12345 | ||
} | ||
] | ||
} | ||
} | ||
|
||
class TestOpenSearchNeuralSearch(TestCase): | ||
def test_similarity_search(self): | ||
docs = OpenSearchNeuralSearch(client=MockClient(), endpoint="test", index="test", model_id="test").similarity_search(query="test", subquery={"_source": {"excludes": ["embedding"]}}, size=10) | ||
self.assertEqual(docs, [Document(page_content='test', metadata={'id': 'test'})]) | ||
|
||
def test_similarity_search_with_score(self): | ||
docs = OpenSearchNeuralSearch(client=MockClient(), endpoint="test", index="test", model_id="test").similarity_search_with_score(query="test") | ||
self.assertEqual(docs, [(Document(page_content='test', metadata={'id': 'test'}), 0.12345)]) | ||
|
||
def test_add_texts(self): | ||
try: | ||
OpenSearchNeuralSearch(client=MockClient(), endpoint="test", index="test", model_id="test").add_texts(texts=["test"], metadatas=[{"id": "test"}]) | ||
except Exception as e: | ||
self.fail(f"from_texts raised an exception: {e}") | ||
|
||
def test_from_texts(self): | ||
try: | ||
OpenSearchNeuralSearch.from_texts(clas="test", texts=["test"], metadatas=[{"id": "test"}]) | ||
except Exception as e: | ||
self.fail(f"from_texts raised an exception: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.