Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files- sanatan_assistant.py +41 -0
sanatan_assistant.py
CHANGED
|
@@ -145,3 +145,44 @@ def query_by_metadata_field(
|
|
| 145 |
response["documents"], response["metadatas"], response["ids"]
|
| 146 |
)
|
| 147 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
response["documents"], response["metadatas"], response["ids"]
|
| 146 |
)
|
| 147 |
)
|
| 148 |
+
|
| 149 |
+
def query_by_metadata_field(
|
| 150 |
+
collection_name: allowedCollections,
|
| 151 |
+
query: str,
|
| 152 |
+
metadata_field: str,
|
| 153 |
+
metadata_search_operator: Literal[
|
| 154 |
+
"$in", "$eq", "$gt", "$gte", "$lt", "$lte", "$ne", "$nin"
|
| 155 |
+
],
|
| 156 |
+
metadata_value: Any,
|
| 157 |
+
n_results=5,
|
| 158 |
+
):
|
| 159 |
+
"""
|
| 160 |
+
Search a scripture collection by metadata. Do NOT use this for semantic search. Only use when a specific metadata field is provided.
|
| 161 |
+
|
| 162 |
+
Parameters:
|
| 163 |
+
- collection_name (str): The name of the scripture collection to search. ...
|
| 164 |
+
- query (str): The search query.
|
| 165 |
+
- metadata_field (str) : The name of the metadata field. e.g. azhwar_name
|
| 166 |
+
- metadata_search_operator (str) : The search operator e.g. $eq or $in
|
| 167 |
+
- metadata_value : Value to search for can be any primitive datatype like str or int (or a list[str] if metadata_search_operator = '$in'). for e.g. Thirumangai Azhwar or '2233' or 2233
|
| 168 |
+
- n_results (int): Number of results to return. Default is 5.
|
| 169 |
+
|
| 170 |
+
Returns:
|
| 171 |
+
- A list of matching results.
|
| 172 |
+
"""
|
| 173 |
+
logger.info("Searching collection [%s] for [%s]", collection_name, query)
|
| 174 |
+
response = sanatanDatabase.search_by_metadata(
|
| 175 |
+
collection_name=collection_name,
|
| 176 |
+
query=query,
|
| 177 |
+
metadata_field=metadata_field,
|
| 178 |
+
metadata_search_operator=metadata_search_operator,
|
| 179 |
+
metadata_value=metadata_value,
|
| 180 |
+
n_results=n_results,
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
return "\n\n".join(
|
| 184 |
+
f"Document: {doc}\nMetadata: {meta}\nID: {id_}"
|
| 185 |
+
for doc, meta, id_ in zip(
|
| 186 |
+
response["documents"], response["metadatas"], response["ids"]
|
| 187 |
+
)
|
| 188 |
+
)
|