-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
81 lines (70 loc) · 2.65 KB
/
agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
from typing import List
import traceback
import os
from dotenv import load_dotenv
from query_data import setup_qa_chain
# Load environment variables
load_dotenv()
# Set up the QA chain
qa_chain = setup_qa_chain()
# Set up the search tool
search = DuckDuckGoSearchAPIWrapper()
def is_medical_question(query: str) -> bool:
# simple judge
medical_keywords = ['disease', 'symptom', 'treatment', 'medicine', 'doctor', 'hospital', 'health', 'medical', 'pain']
return any(keyword in query.lower() for keyword in medical_keywords)
def run_agent(query: str):
try:
print(f"Query: {query}")
if is_medical_question(query):
print("Using MedicalQA for this query.")
# MedicalQA
result = qa_chain({"query": query})
print("MedicalQA result:", result)
else:
print("Using WebSearch for this query.")
# WebSearch
result = search.run(query)
print("WebSearch result:", result)
sources = []
if isinstance(result, dict): # dictionary (for MedicalQA)
if 'result' in result:
answer = result['result']
elif 'answer' in result:
answer = result['answer']
else:
answer = str(result)
if 'source_documents' in result:
for doc in result['source_documents']:
source = doc.page_content[:200] + "..."
sources.append(source)
print(f"Source: {source}")
elif isinstance(result, str): # string (for WebSearch or simple MedicalQA response)
answer = result
sources.append(result[:200] + "...")
else:
answer = str(result)
print("Answer:", answer)
print("Sources found:", len(sources))
return {"answer": answer, "sources": sources}
except Exception as e:
print(f"An error occurred: {str(e)}")
traceback.print_exc()
return {
"answer": "I'm sorry, I encountered an error while processing your request. Could you please try rephrasing your question?",
"sources": []
}
# Main
if __name__ == "__main__":
while True:
user_input = input("Ask a question (or type 'quit' to exit): ")
if user_input.lower() == 'quit':
break
response = run_agent(user_input)
print("Agent's response:", response["answer"])
if response["sources"]:
print("Sources:")
for source in response["sources"]:
print(f"- {source}")
print("\n")