-
Notifications
You must be signed in to change notification settings - Fork 107
Feature: Add Document Retrieval with Metadata Filtering similar to Chroma Vector Store #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1b0c89d
99d1440
a986493
b0ce110
6fafde4
80f5061
3dad796
7d50960
27d8ecc
a7dd0bd
9017260
9cd202c
1471950
20e2bfc
e82e26a
0540dc8
69bcc75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -672,6 +672,39 @@ async def __query_collection( | |
| return combined_results | ||
| return dense_results | ||
|
|
||
| async def __query_collection_with_filter( | ||
| self, | ||
| *, | ||
| limit: Optional[int] = None, | ||
| offset: Optional[int] = None, | ||
| filter: Optional[dict] = None, | ||
| columns: Optional[list[str]] = None, | ||
| **kwargs: Any, | ||
| ) -> Sequence[RowMapping]: | ||
| """Asynchronously query the database collection using filters and parameters and return matching rows.""" | ||
|
|
||
| column_names = ", ".join(f'"{col}"' for col in columns) | ||
|
|
||
| safe_filter = None | ||
| filter_dict = None | ||
| if filter and isinstance(filter, dict): | ||
| safe_filter, filter_dict = self._create_filter_clause(filter) | ||
|
|
||
| suffix_id = str(uuid.uuid4()).split("-")[0] | ||
| where_filters = f"WHERE {safe_filter}" if safe_filter else "" | ||
| dense_query_stmt = f"""SELECT {column_names} | ||
| FROM "{self.schema_name}"."{self.table_name}" {where_filters} LIMIT :limit_{suffix_id} OFFSET :offset_{suffix_id}; | ||
| """ | ||
| param_dict = {f"limit_{suffix_id}": limit, f"offset_{suffix_id}": offset} | ||
| if filter_dict: | ||
| param_dict.update(filter_dict) | ||
| async with self.engine.connect() as conn: | ||
| result = await conn.execute(text(dense_query_stmt), param_dict) | ||
| result_map = result.mappings() | ||
| results = result_map.fetchall() | ||
|
|
||
| return results | ||
|
|
||
| async def asimilarity_search( | ||
| self, | ||
| query: str, | ||
|
|
@@ -995,6 +1028,71 @@ async def is_valid_index( | |
| results = result_map.fetchall() | ||
| return bool(len(results) == 1) | ||
|
|
||
| async def aget( | ||
| self, | ||
| ids: Optional[Sequence[str]] = None, | ||
| where: Optional[dict] = None, | ||
| limit: Optional[int] = None, | ||
| offset: Optional[int] = None, | ||
| where_document: Optional[dict] = None, | ||
| include: Optional[list[str]] = None, | ||
| **kwargs: Any, | ||
| ) -> dict[str, Any]: | ||
| """Retrieve documents from the collection using filters and parameters.""" | ||
| filter = {} | ||
| if ids: | ||
| filter.update({self.id_column: {"$in": ids}}) | ||
| if where: | ||
| filter.update(where) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using We can convert the |
||
| if where_document: | ||
| filter.update({self.content_column: where_document}) | ||
|
|
||
| if include is None: | ||
| include = ["metadatas", "documents"] | ||
|
|
||
| fields_mapping = { | ||
| "embeddings": [self.embedding_column], | ||
| "metadatas": self.metadata_columns + [self.metadata_json_column] | ||
| if self.metadata_json_column | ||
| else self.metadata_columns, | ||
| "documents": [self.content_column], | ||
| } | ||
|
|
||
| included_fields = ["ids"] | ||
| columns = [self.id_column] | ||
|
|
||
| for field, cols in fields_mapping.items(): | ||
| if field in include: | ||
| included_fields.append(field) | ||
| columns.extend(cols) | ||
|
|
||
| results = await self.__query_collection_with_filter( | ||
| limit=limit, offset=offset, filter=filter, columns=columns, **kwargs | ||
| ) | ||
|
|
||
| final_results = {field: [] for field in included_fields} | ||
|
|
||
| for row in results: | ||
| final_results["ids"].append(str(row[self.id_column])) | ||
|
|
||
| if "metadatas" in final_results: | ||
| metadata = ( | ||
| row.get(self.metadata_json_column) or {} | ||
| if self.metadata_json_column | ||
| else {} | ||
| ) | ||
| for col in self.metadata_columns: | ||
| metadata[col] = row[col] | ||
| final_results["metadatas"].append(metadata) | ||
|
|
||
| if "documents" in final_results: | ||
| final_results["documents"].append(row[self.content_column]) | ||
|
|
||
| if "embeddings" in final_results: | ||
| final_results["embeddings"].append(row[self.embedding_column]) | ||
|
|
||
| return final_results | ||
|
|
||
| async def aget_by_ids(self, ids: Sequence[str]) -> list[Document]: | ||
| """Get documents by ids.""" | ||
|
|
||
|
|
@@ -1249,6 +1347,20 @@ def _create_filter_clause(self, filters: Any) -> tuple[str, dict]: | |
| else: | ||
| return "", {} | ||
|
|
||
| def get( | ||
| self, | ||
| ids: Optional[Sequence[str]] = None, | ||
| where: Optional[dict] = None, | ||
| limit: Optional[int] = None, | ||
| offset: Optional[int] = None, | ||
| where_document: Optional[dict] = None, | ||
| include: Optional[list[str]] = None, | ||
| **kwargs: Any, | ||
| ) -> dict[str, Any]: | ||
| raise NotImplementedError( | ||
| "Sync methods are not implemented for AsyncPGVectorStore. Use PGVectorStore interface instead." | ||
| ) | ||
|
|
||
| def get_by_ids(self, ids: Sequence[str]) -> list[Document]: | ||
| raise NotImplementedError( | ||
| "Sync methods are not implemented for AsyncPGVectorStore. Use PGVectorStore interface instead." | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.