- Notifications
You must be signed in to change notification settings - Fork352
metadata and generic filters in vector search#689
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 2 additions & 2 deletionspgml-sdks/python/pgml/examples/question_answering.py
This file contains hidden or 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
39 changes: 23 additions & 16 deletionspgml-sdks/python/pgml/pgml/collection.py
This file contains hidden or 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 |
|---|---|---|
| @@ -298,18 +298,23 @@ def upsert_documents( | ||
| ) | ||
| continue | ||
| metadata = document | ||
| _uuid = "" | ||
| if id_key not in list(document.keys()): | ||
| log.info("id key is not present.. hashing") | ||
| source_uuid = hashlib.md5( | ||
| (text + " " + json.dumps(document)).encode("utf-8") | ||
| ).hexdigest() | ||
| else: | ||
| _uuid = document.pop(id_key) | ||
| try: | ||
| source_uuid = str(uuid.UUID(_uuid)) | ||
| except Exception: | ||
| source_uuid = hashlib.md5(str(_uuid).encode("utf-8")).hexdigest() | ||
| if _uuid: | ||
| document[id_key] = source_uuid | ||
| upsert_statement = "INSERT INTO {documents_table} (text, source_uuid, metadata) VALUES ({text}, {source_uuid}, {metadata}) \ | ||
| ON CONFLICT (source_uuid) \ | ||
| @@ -323,9 +328,6 @@ def upsert_documents( | ||
| # put the text and id back in document | ||
| document[text_key] = text | ||
| self.pool.putconn(conn) | ||
| def register_text_splitter( | ||
| @@ -683,7 +685,8 @@ def vector_search( | ||
| top_k: int = 5, | ||
| model_id: int = 1, | ||
| splitter_id: int = 1, | ||
| metadata_filter: Optional[Dict[str, Any]] = {}, | ||
| generic_filter: Optional[str] = "", | ||
| ) -> List[Dict[str, Any]]: | ||
| """ | ||
| This function performs a vector search on a database using a query and returns the top matching | ||
| @@ -753,13 +756,6 @@ def vector_search( | ||
| % (model_id, splitter_id, model_id, splitter_id) | ||
| ) | ||
| return [] | ||
| cte_select_statement = """ | ||
| WITH query_cte AS ( | ||
| @@ -775,7 +771,7 @@ def vector_search( | ||
| SELECT cte.score, chunks.chunk, documents.metadata | ||
| FROM cte | ||
| INNER JOIN {chunks_table} chunks ON chunks.id = cte.chunk_id | ||
| INNER JOIN {documents_table} documents ON documents.id = chunks.document_id | ||
| """.format( | ||
| model=sql.Literal(model).as_string(conn), | ||
| query_text=query, | ||
| @@ -784,9 +780,20 @@ def vector_search( | ||
| top_k=top_k, | ||
| chunks_table=self.chunks_table, | ||
| documents_table=self.documents_table, | ||
| ) | ||
| if metadata_filter: | ||
| cte_select_statement += ( | ||
| " AND documents.metadata @> {metadata_filter}".format( | ||
Contributor 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. This is nice. We should also add a GIN index to | ||
| metadata_filter=sql.Literal(json.dumps(metadata_filter)).as_string( | ||
| conn | ||
| ) | ||
| ) | ||
| ) | ||
| if generic_filter: | ||
| cte_select_statement += " AND " + generic_filter | ||
| search_results = run_select_statement( | ||
| conn, cte_select_statement, order_by="score", ascending=False | ||
| ) | ||
125 changes: 100 additions & 25 deletionspgml-sdks/python/pgml/tests/test_collection.py
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.