@@ -683,6 +683,7 @@ def vector_search(
683683top_k :int = 5 ,
684684model_id :int = 1 ,
685685splitter_id :int = 1 ,
686+ ** kwargs :Any ,
686687 )-> List [Dict [str ,Any ]]:
687688"""
688689 This function performs a vector search on a database using a query and returns the top matching
@@ -702,6 +703,9 @@ def vector_search(
702703 splitter used to split the documents into chunks. It is used to retrieve the embeddings table
703704 associated with the specified splitter, defaults to 1
704705 :type splitter_id: int (optional)
706+ :param kwargs: Additional filtering parameters to be used in the search query. These parameters
707+ are from the metadata of the documents and can be used to filter the search results based on
708+ metadata values.
705709 :return: a list of dictionaries containing search results for a given query. Each dictionary
706710 contains the following keys: "score", "text", and "metadata". The "score" key contains a float
707711 value representing the similarity score between the query and the search result. The "text" key
@@ -749,6 +753,13 @@ def vector_search(
749753% (model_id ,splitter_id ,model_id ,splitter_id )
750754 )
751755return []
756+
757+ if kwargs :
758+ metadata_filter = [f"documents.metadata->>'{ k } ' = '{ v } '" if isinstance (v ,str )else f"documents.metadata->>'{ k } ' ={ v } " for k ,v in kwargs .items ()]
759+ metadata_filter = " AND " .join (metadata_filter )
760+ metadata_filter = f"AND{ metadata_filter } "
761+ else :
762+ metadata_filter = ""
752763
753764cte_select_statement = """
754765 WITH query_cte AS (
@@ -764,7 +775,7 @@ def vector_search(
764775 SELECT cte.score, chunks.chunk, documents.metadata
765776 FROM cte
766777 INNER JOIN {chunks_table} chunks ON chunks.id = cte.chunk_id
767- INNER JOIN {documents_table} documents ON documents.id = chunks.document_id;
778+ INNER JOIN {documents_table} documents ON documents.id = chunks.document_id {metadata_filter}
768779 """ .format (
769780model = sql .Literal (model ).as_string (conn ),
770781query_text = query ,
@@ -773,6 +784,7 @@ def vector_search(
773784top_k = top_k ,
774785chunks_table = self .chunks_table ,
775786documents_table = self .documents_table ,
787+ metadata_filter = metadata_filter ,
776788 )
777789
778790search_results = run_select_statement (