-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
66 lines (51 loc) · 2.01 KB
/
main.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
import logging
from agent import create_agent
from utils.retriever import create_retriever
from utils.tools import create_web_search_tool
from utils.config import Config
from utils.state import GraphState
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def main():
try:
# Load configuration
config = Config()
logger.info(f"Loaded configuration: {config}")
# Initialize components
retriever = create_retriever(config.INDEX_NAME)
web_search_tool = create_web_search_tool(config.TAVILY_API_KEY)
# Create agent
agent = create_agent(retriever, web_search_tool)
while True:
# Prompt user for question
question = input("Please enter your question (or type 'exit' to quit): ")
if question.lower() == 'exit':
print("Exiting the program. Goodbye!")
break
logger.info(f"Running agent with question: {question}")
# Initialize the GraphState with all required fields
initial_state = GraphState(
question=question,
context=[],
current_step="",
final_answer="",
retriever=retriever,
web_search_tool=web_search_tool,
error=None,
selected_namespaces=[],
web_search_results=[]
)
result = agent.invoke(initial_state)
logger.info(f"Agent result: {result}")
# Print the final answer
if result.get("final_answer"):
print(f"\nAnswer: {result['final_answer']}")
elif result.get("error"):
print(f"\nError occurred: {result['error']}")
else:
print("\nNo answer or error was returned.")
print("\n" + "-" * 50 + "\n") # Add a separator between questions
except Exception as e:
logger.error(f"An error occurred: {str(e)}")
if __name__ == '__main__':
main()