Update app.py
Browse files
app.py
CHANGED
@@ -46,10 +46,58 @@ class BasicAgent:
|
|
46 |
"Content-Type": "application/json"
|
47 |
}
|
48 |
|
49 |
-
# Create the agent workflow
|
50 |
self.workflow = self._create_workflow()
|
51 |
print("BasicAgent initialization complete.")
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def _call_llm_api(self, prompt: str) -> str:
|
54 |
"""Call the Qwen model through the Hugging Face API."""
|
55 |
try:
|
@@ -151,53 +199,6 @@ class BasicAgent:
|
|
151 |
state.final_answer = self._call_llm_api(prompt)
|
152 |
return state
|
153 |
|
154 |
-
def _create_workflow(self) -> Graph:
|
155 |
-
"""Create the agent workflow using LangGraph."""
|
156 |
-
# Create the workflow with explicit input and output types
|
157 |
-
workflow = StateGraph(
|
158 |
-
input_type=AgentState,
|
159 |
-
output_type=AgentState
|
160 |
-
)
|
161 |
-
|
162 |
-
# Add nodes with explicit type casting
|
163 |
-
workflow.add_node("analyze", cast(Any, self._analyze_question))
|
164 |
-
workflow.add_node("calculator", cast(Any, self._use_calculator))
|
165 |
-
workflow.add_node("search", cast(Any, self._use_search))
|
166 |
-
workflow.add_node("final_answer", cast(Any, self._generate_final_answer))
|
167 |
-
|
168 |
-
# Define edges
|
169 |
-
workflow.add_edge("analyze", "calculator")
|
170 |
-
workflow.add_edge("analyze", "search")
|
171 |
-
workflow.add_edge("analyze", "final_answer")
|
172 |
-
workflow.add_edge("calculator", "final_answer")
|
173 |
-
workflow.add_edge("search", "final_answer")
|
174 |
-
|
175 |
-
# Define conditional edges
|
176 |
-
def router(state: AgentState) -> str:
|
177 |
-
if state.current_step == 'calculator':
|
178 |
-
return 'calculator'
|
179 |
-
elif state.current_step == 'search':
|
180 |
-
return 'search'
|
181 |
-
elif state.current_step == 'final_answer':
|
182 |
-
return 'final_answer'
|
183 |
-
return 'analyze'
|
184 |
-
|
185 |
-
workflow.add_conditional_edges(
|
186 |
-
"analyze",
|
187 |
-
router,
|
188 |
-
{
|
189 |
-
"calculator": "calculator",
|
190 |
-
"search": "search",
|
191 |
-
"final_answer": "final_answer"
|
192 |
-
}
|
193 |
-
)
|
194 |
-
|
195 |
-
# Set entry and exit points
|
196 |
-
workflow.set_entry_point("analyze")
|
197 |
-
workflow.set_finish_point("final_answer")
|
198 |
-
|
199 |
-
return workflow.compile()
|
200 |
-
|
201 |
def __call__(self, question: str) -> str:
|
202 |
"""Process a question through the agent workflow."""
|
203 |
print(f"Agent received question: {question[:50]}...")
|
|
|
46 |
"Content-Type": "application/json"
|
47 |
}
|
48 |
|
49 |
+
# Create the agent workflow with proper state schema
|
50 |
self.workflow = self._create_workflow()
|
51 |
print("BasicAgent initialization complete.")
|
52 |
|
53 |
+
def _create_workflow(self) -> Graph:
|
54 |
+
"""Create the agent workflow using LangGraph."""
|
55 |
+
# Create the workflow with proper state schema
|
56 |
+
workflow = StateGraph(
|
57 |
+
input_type=AgentState,
|
58 |
+
output_type=AgentState,
|
59 |
+
state_schema=AgentState
|
60 |
+
)
|
61 |
+
|
62 |
+
# Add nodes
|
63 |
+
workflow.add_node("analyze", self._analyze_question)
|
64 |
+
workflow.add_node("calculator", self._use_calculator)
|
65 |
+
workflow.add_node("search", self._use_search)
|
66 |
+
workflow.add_node("final_answer", self._generate_final_answer)
|
67 |
+
|
68 |
+
# Define edges
|
69 |
+
workflow.add_edge("analyze", "calculator")
|
70 |
+
workflow.add_edge("analyze", "search")
|
71 |
+
workflow.add_edge("analyze", "final_answer")
|
72 |
+
workflow.add_edge("calculator", "final_answer")
|
73 |
+
workflow.add_edge("search", "final_answer")
|
74 |
+
|
75 |
+
# Define conditional edges
|
76 |
+
def router(state: AgentState) -> str:
|
77 |
+
if state.current_step == 'calculator':
|
78 |
+
return 'calculator'
|
79 |
+
elif state.current_step == 'search':
|
80 |
+
return 'search'
|
81 |
+
elif state.current_step == 'final_answer':
|
82 |
+
return 'final_answer'
|
83 |
+
return 'analyze'
|
84 |
+
|
85 |
+
workflow.add_conditional_edges(
|
86 |
+
"analyze",
|
87 |
+
router,
|
88 |
+
{
|
89 |
+
"calculator": "calculator",
|
90 |
+
"search": "search",
|
91 |
+
"final_answer": "final_answer"
|
92 |
+
}
|
93 |
+
)
|
94 |
+
|
95 |
+
# Set entry and exit points
|
96 |
+
workflow.set_entry_point("analyze")
|
97 |
+
workflow.set_finish_point("final_answer")
|
98 |
+
|
99 |
+
return workflow.compile()
|
100 |
+
|
101 |
def _call_llm_api(self, prompt: str) -> str:
|
102 |
"""Call the Qwen model through the Hugging Face API."""
|
103 |
try:
|
|
|
199 |
state.final_answer = self._call_llm_api(prompt)
|
200 |
return state
|
201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
def __call__(self, question: str) -> str:
|
203 |
"""Process a question through the agent workflow."""
|
204 |
print(f"Agent received question: {question[:50]}...")
|