Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -42,21 +42,50 @@ for row in all_rows:
|
|
| 42 |
@tool
|
| 43 |
def find_answer(query: str) -> str:
|
| 44 |
"""
|
| 45 |
-
|
| 46 |
-
Otherwise,
|
|
|
|
| 47 |
"""
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
return qa_dict[query]
|
| 51 |
similar_docs = vector_store.similarity_search(query, k=1)
|
| 52 |
if not similar_docs:
|
| 53 |
-
return "Sorry, I couldn
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
if "Final answer:
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
tools = [find_answer]
|
| 62 |
|
|
|
|
| 42 |
@tool
|
| 43 |
def find_answer(query: str) -> str:
|
| 44 |
"""
|
| 45 |
+
1) Do an exact dict lookup if possible (not shown here).
|
| 46 |
+
2) Otherwise, run an embedding search and extract ONLY the final‐answer line.
|
| 47 |
+
We look for “Final answer :” or “Answer:” and return just that tail.
|
| 48 |
"""
|
| 49 |
+
# … (you may have an in‐memory dict check here) …
|
| 50 |
|
| 51 |
+
# If you fall back to embedding search:
|
|
|
|
| 52 |
similar_docs = vector_store.similarity_search(query, k=1)
|
| 53 |
if not similar_docs:
|
| 54 |
+
return "Sorry, I couldn’t find that question in my database."
|
| 55 |
+
|
| 56 |
+
full_content = similar_docs[0].page_content
|
| 57 |
+
# Look for “Final answer :” first
|
| 58 |
+
if "Final answer :" in full_content:
|
| 59 |
+
# Extract everything after the first occurrence of “Final answer :”
|
| 60 |
+
answer_text = full_content.split("Final answer :", 1)[1].strip()
|
| 61 |
+
return f"Final answer : {answer_text}"
|
| 62 |
+
|
| 63 |
+
# Fallback if they used “Answer:” instead
|
| 64 |
+
if "Answer:" in full_content:
|
| 65 |
+
answer_text = full_content.split("Answer:", 1)[1].strip()
|
| 66 |
+
return f"Answer: {answer_text}"
|
| 67 |
+
|
| 68 |
+
# If neither tag exists, just return whatever is after the last newline
|
| 69 |
+
# (or the entire text, but without the question prefix).
|
| 70 |
+
lines = full_content.strip().splitlines()
|
| 71 |
+
return lines[-1].strip()
|
| 72 |
+
# def find_answer(query: str) -> str:
|
| 73 |
+
# """
|
| 74 |
+
# If 'query' exactly matches a key in qa_dict, return qa_dict[query].
|
| 75 |
+
# Otherwise, do an embedding search (k=1) in Supabase and return only the "Answer:" portion.
|
| 76 |
+
# """
|
| 77 |
+
|
| 78 |
+
# if query in qa_dict:
|
| 79 |
+
# return qa_dict[query]
|
| 80 |
+
# similar_docs = vector_store.similarity_search(query, k=1)
|
| 81 |
+
# if not similar_docs:
|
| 82 |
+
# return "Sorry, I couldn't find that question"
|
| 83 |
+
# top_doc = similar_docs[0].page_content
|
| 84 |
+
# if "Answer:" in top_doc:
|
| 85 |
+
# return top_doc.split("Answer:", 1)[1].strip()
|
| 86 |
+
# if "Final answer: " in top_doc:
|
| 87 |
+
# return top_doc.split("Final answer :", 1)[1].strip()
|
| 88 |
+
# return top_doc.strip()
|
| 89 |
|
| 90 |
tools = [find_answer]
|
| 91 |
|