-
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.
* Implement neural search vector store * Create OpensearchNeuralSearch vector store * Implement infrastructure for hybrid search (not yet live) * Remove duplicate azure_endpoint assignment --------- Co-authored-by: Michael B. Klein <mbklein@gmail.com> Co-authored-by: Brendan Quinn <brendan-quinn@northwestern.edu>
- Loading branch information
1 parent
e29ddc1
commit 7f9b6ac
Showing
13 changed files
with
124 additions
and
60 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,87 @@ | ||
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, | ||
endpoint: str, | ||
index: str, | ||
model_id: str, | ||
vector_field: str = "embedding", | ||
search_pipeline: str = None, | ||
text_field: str = "id", | ||
**kwargs: Any, | ||
): | ||
self.client = 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) | ||
|
||
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
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