-
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 #30 from TogetherCrew/fix/metadata-filtering
Fix/metadata filtering
- Loading branch information
Showing
17 changed files
with
531 additions
and
513 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from llama_index.embeddings import BaseEmbedding | ||
from llama_index.schema import NodeWithScore | ||
from llama_index.vector_stores import PGVectorStore, VectorStoreQueryResult | ||
from llama_index.vector_stores.postgres import DBEmbeddingRow | ||
from sqlalchemy import Date, and_, cast, or_, select, text | ||
from tc_hivemind_backend.embeddings.cohere import CohereEmbedding | ||
|
||
|
||
class RetrieveSimilarNodes: | ||
"""Retriever similar nodes over a postgres vector store.""" | ||
|
||
def __init__( | ||
self, | ||
vector_store: PGVectorStore, | ||
similarity_top_k: int, | ||
embed_model: BaseEmbedding = CohereEmbedding(), | ||
) -> None: | ||
"""Init params.""" | ||
self._vector_store = vector_store | ||
self._embed_model = embed_model | ||
self._similarity_top_k = similarity_top_k | ||
|
||
def query_db( | ||
self, query: str, filters: list[dict[str, str]] | None = None | ||
) -> list[NodeWithScore]: | ||
""" | ||
query database with given filters (similarity search is also done) | ||
Parameters | ||
------------- | ||
query : str | ||
the user question | ||
filters : list[dict[str, str]] | None | ||
a list of filters to apply with `or` condition | ||
the dictionary would be applying `and` | ||
operation between keys and values of json metadata_ | ||
if `None` then no filtering would be applied | ||
""" | ||
self._vector_store._initialize() | ||
embedding = self._embed_model.get_text_embedding(text=query) | ||
stmt = select( # type: ignore | ||
self._vector_store._table_class.id, | ||
self._vector_store._table_class.node_id, | ||
self._vector_store._table_class.text, | ||
self._vector_store._table_class.metadata_, | ||
self._vector_store._table_class.embedding.cosine_distance(embedding).label( | ||
"distance" | ||
), | ||
).order_by(text("distance asc")) | ||
|
||
if filters is not None and filters != []: | ||
conditions = [] | ||
for condition in filters: | ||
filters_and = [] | ||
for key, value in condition.items(): | ||
if key == "date": | ||
# Apply ::date cast when the key is 'date' | ||
filter_condition = cast( | ||
self._vector_store._table_class.metadata_.op("->>")(key), | ||
Date, | ||
) == cast(value, Date) | ||
else: | ||
filter_condition = ( | ||
self._vector_store._table_class.metadata_.op("->>")(key) | ||
== value | ||
) | ||
|
||
filters_and.append(filter_condition) | ||
|
||
conditions.append(and_(*filters_and)) | ||
|
||
stmt = stmt.where(or_(*conditions)) | ||
|
||
stmt = stmt.limit(self._similarity_top_k) | ||
|
||
with self._vector_store._session() as session, session.begin(): | ||
res = session.execute(stmt) | ||
|
||
results = [ | ||
DBEmbeddingRow( | ||
node_id=item.node_id, | ||
text=item.text, | ||
metadata=item.metadata_, | ||
similarity=(1 - item.distance) if item.distance is not None else 0, | ||
) | ||
for item in res.all() | ||
] | ||
query_result = self._vector_store._db_rows_to_query_result(results) | ||
nodes = self._get_nodes_with_score(query_result) | ||
return nodes | ||
|
||
def _get_nodes_with_score( | ||
self, query_result: VectorStoreQueryResult | ||
) -> list[NodeWithScore]: | ||
"""get nodes from a query_results""" | ||
nodes_with_scores = [] | ||
for index, node in enumerate(query_result.nodes): | ||
score: float | None = None | ||
if query_result.similarities is not None: | ||
score = query_result.similarities[index] | ||
nodes_with_scores.append(NodeWithScore(node=node, score=score)) | ||
|
||
return nodes_with_scores |
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,34 @@ | ||
from unittest import TestCase | ||
from unittest.mock import MagicMock | ||
|
||
from bot.retrievers.retrieve_similar_nodes import RetrieveSimilarNodes | ||
from llama_index.schema import TextNode | ||
|
||
|
||
class TestRetrieveSimilarNodes(TestCase): | ||
def setUp(self): | ||
self.table_name = "sample_table" | ||
self.dbname = "community_some_id" | ||
|
||
self.vector_store = MagicMock() | ||
self.embed_model = MagicMock() | ||
self.retriever = RetrieveSimilarNodes( | ||
vector_store=self.vector_store, | ||
similarity_top_k=5, | ||
embed_model=self.embed_model, | ||
) | ||
|
||
def test_init(self): | ||
self.assertEqual(self.retriever._similarity_top_k, 5) | ||
self.assertEqual(self.vector_store, self.retriever._vector_store) | ||
|
||
def test_get_nodes_with_score(self): | ||
# Test the _get_nodes_with_score private method | ||
query_result = MagicMock() | ||
query_result.nodes = [TextNode(), TextNode(), TextNode()] | ||
query_result.similarities = [0.8, 0.9, 0.7] | ||
|
||
result = self.retriever._get_nodes_with_score(query_result) | ||
|
||
self.assertEqual(len(result), 3) | ||
self.assertAlmostEqual(result[0].score, 0.8, delta=0.001) |
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.