ppsingh commited on
Commit
712bf59
·
1 Parent(s): b63e909

adding filter

Browse files
app.py CHANGED
@@ -18,7 +18,7 @@ except Exception as e:
18
  def retrieve(
19
  query:str,
20
  collection_name:str = None,
21
- filter_metadata:list[dict] = None,
22
  ) -> list:
23
  """
24
  Retrieve semantically similar documents from the vector database for MCP clients.
@@ -71,12 +71,16 @@ with gr.Blocks() as ui:
71
  placeholder="country",
72
  info="Filter by document source type (leave empty for all)"
73
  )
74
- subtype_input = gr.Textbox(
75
  label="Value in filter to be looked for(optional)",
76
  lines=1,
77
  placeholder="Ecuador, Guatemala",
78
  info="Filter by document subtype (leave empty for all)"
79
  )
 
 
 
 
80
 
81
  submit_btn = gr.Button("Submit", variant="primary")
82
 
@@ -91,7 +95,7 @@ with gr.Blocks() as ui:
91
  # UI event handler
92
  submit_btn.click(
93
  fn=retrieve,
94
- inputs=[query_input,collection_name],
95
  outputs=output,
96
  api_name="retrieve"
97
  )
 
18
  def retrieve(
19
  query:str,
20
  collection_name:str = None,
21
+ filter_metadata:dict = None,
22
  ) -> list:
23
  """
24
  Retrieve semantically similar documents from the vector database for MCP clients.
 
71
  placeholder="country",
72
  info="Filter by document source type (leave empty for all)"
73
  )
74
+ sources_value = gr.Textbox(
75
  label="Value in filter to be looked for(optional)",
76
  lines=1,
77
  placeholder="Ecuador, Guatemala",
78
  info="Filter by document subtype (leave empty for all)"
79
  )
80
+ if sources_input & sources_value:
81
+ filter_metadata = {sources_input:sources_value}
82
+ else:
83
+ filter_metadata = None
84
 
85
  submit_btn = gr.Button("Submit", variant="primary")
86
 
 
95
  # UI event handler
96
  submit_btn.click(
97
  fn=retrieve,
98
+ inputs=[query_input,collection_name, filter_metadata],
99
  outputs=output,
100
  api_name="retrieve"
101
  )
utils/__pycache__/vectorstore_interface.cpython-311.pyc CHANGED
Binary files a/utils/__pycache__/vectorstore_interface.cpython-311.pyc and b/utils/__pycache__/vectorstore_interface.cpython-311.pyc differ
 
utils/retriever.py CHANGED
@@ -70,7 +70,7 @@ def get_vectorstore() -> VectorStoreInterface:
70
  return vectorstore
71
 
72
  def create_filter(
73
- filter_metadata:list[Dict] = None,
74
  ) -> Optional[rest.Filter]:
75
  """
76
  Create a Qdrant filter based on metadata criteria.
@@ -89,21 +89,21 @@ def create_filter(
89
 
90
  conditions = []
91
  logging.info(f"Defining filters for {filter_metadata}")
92
- for condition in filter_metadata:
93
- for key, val in condition:
94
- if isinstance(val, str):
95
- conditions.append(rest.FieldCondition(
96
- key=f"metadata.{key}",
97
- match=rest.MatchValue(value=val)
98
- )
99
  )
100
- else:
101
- conditions.append(
102
- rest.FieldCondition(
103
- key=f"metadata.{key}",
104
- match=rest.MatchAny(any=val)
 
 
105
  )
106
- )
107
  return conditions
108
 
109
 
 
70
  return vectorstore
71
 
72
  def create_filter(
73
+ filter_metadata:dict = None,
74
  ) -> Optional[rest.Filter]:
75
  """
76
  Create a Qdrant filter based on metadata criteria.
 
89
 
90
  conditions = []
91
  logging.info(f"Defining filters for {filter_metadata}")
92
+
93
+ for key, val in filter_metadata.items():
94
+ if isinstance(val, str):
95
+ conditions.append(rest.FieldCondition(
96
+ key=f"metadata.{key}",
97
+ match=rest.MatchValue(value=val)
 
98
  )
99
+ )
100
+ else:
101
+ conditions.append(
102
+ rest.FieldCondition(
103
+ key=f"metadata.{key}",
104
+ match=rest.MatchAny(any=val)
105
+ )
106
  )
 
107
  return conditions
108
 
109