Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -181,6 +181,23 @@ def retrieval(query: str):
|
|
181 |
try:
|
182 |
query_embedding = get_text_embedding(query)
|
183 |
results = collection.query(query_embeddings=[query_embedding], n_results=5)
|
184 |
-
return {"results": results.get("documents", [])}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
except Exception as e:
|
186 |
return {"error": str(e)}
|
|
|
181 |
try:
|
182 |
query_embedding = get_text_embedding(query)
|
183 |
results = collection.query(query_embeddings=[query_embedding], n_results=5)
|
184 |
+
#return {"results": results.get("documents", [])}
|
185 |
+
# Set a similarity threshold (adjust as needed)
|
186 |
+
SIMILARITY_THRESHOLD = 0.7
|
187 |
+
|
188 |
+
# Extract documents and similarity scores
|
189 |
+
documents = results.get("documents", [[]])[0] # Ensure we get the first list
|
190 |
+
distances = results.get("distances", [[]])[0] # Ensure we get the first list
|
191 |
+
|
192 |
+
# Filter results based on similarity threshold
|
193 |
+
filtered_results = [
|
194 |
+
doc for doc, score in zip(documents, distances) if score >= SIMILARITY_THRESHOLD
|
195 |
+
]
|
196 |
+
|
197 |
+
# Return filtered results or indicate no match found
|
198 |
+
if filtered_results:
|
199 |
+
return {"results": filtered_results}
|
200 |
+
else:
|
201 |
+
return {"results": "No relevant match found in ChromaDB."}
|
202 |
except Exception as e:
|
203 |
return {"error": str(e)}
|