Update app.py
Browse files
app.py
CHANGED
@@ -184,7 +184,7 @@ def discover_attachment(task_id: str, api_url: str) -> Optional[str]:
|
|
184 |
class AgentState(TypedDict):
|
185 |
question: str
|
186 |
current_step: str
|
187 |
-
|
188 |
history: List[Dict[str, str]]
|
189 |
needs_search: bool
|
190 |
search_query: str
|
@@ -192,7 +192,7 @@ class AgentState(TypedDict):
|
|
192 |
logs: Dict[str, Any]
|
193 |
file_url: str
|
194 |
code_blocks: List[Dict[str, str]]
|
195 |
-
next_step: str
|
196 |
|
197 |
# -------------------------
|
198 |
# BasicAgent implementation
|
@@ -249,7 +249,7 @@ class BasicAgent:
|
|
249 |
state: AgentState = {
|
250 |
"question": question,
|
251 |
"current_step": "route",
|
252 |
-
"
|
253 |
"history": [],
|
254 |
"needs_search": False,
|
255 |
"search_query": "",
|
@@ -266,11 +266,9 @@ class BasicAgent:
|
|
266 |
|
267 |
try:
|
268 |
final_state = self.workflow.invoke(state)
|
269 |
-
answer
|
270 |
-
|
271 |
-
|
272 |
-
return "No answer could be generated"
|
273 |
-
return answer
|
274 |
except Exception as e:
|
275 |
print(f"Error in workflow execution: {str(e)}")
|
276 |
return f"Error processing question: {str(e)}"
|
@@ -332,16 +330,18 @@ class BasicAgent:
|
|
332 |
|
333 |
print(f"Generated answer: {answer}")
|
334 |
return {
|
335 |
-
"
|
336 |
"current_step": "done",
|
337 |
-
"next_step": "done"
|
|
|
338 |
}
|
339 |
except Exception as e:
|
340 |
print(f"\nError processing image {state['file_url']}: {str(e)}")
|
341 |
return {
|
342 |
-
"
|
343 |
"current_step": "done",
|
344 |
-
"next_step": "done"
|
|
|
345 |
}
|
346 |
|
347 |
def _process_video(self, state: AgentState) -> Dict[str, Any]:
|
@@ -358,16 +358,18 @@ class BasicAgent:
|
|
358 |
|
359 |
print(f"Generated answer: {answer}")
|
360 |
return {
|
361 |
-
"
|
362 |
"current_step": "done",
|
363 |
-
"next_step": "done"
|
|
|
364 |
}
|
365 |
except Exception as e:
|
366 |
print(f"\nError processing video {state['file_url']}: {str(e)}")
|
367 |
return {
|
368 |
-
"
|
369 |
"current_step": "done",
|
370 |
-
"next_step": "done"
|
|
|
371 |
}
|
372 |
|
373 |
def _process_spreadsheet(self, state: AgentState) -> Dict[str, Any]:
|
@@ -384,16 +386,18 @@ class BasicAgent:
|
|
384 |
|
385 |
print(f"Generated answer: {answer}")
|
386 |
return {
|
387 |
-
"
|
388 |
"current_step": "done",
|
389 |
-
"next_step": "done"
|
|
|
390 |
}
|
391 |
except Exception as e:
|
392 |
print(f"\nError processing spreadsheet {state['file_url']}: {str(e)}")
|
393 |
return {
|
394 |
-
"
|
395 |
"current_step": "done",
|
396 |
-
"next_step": "done"
|
|
|
397 |
}
|
398 |
|
399 |
def _process_python(self, state: AgentState) -> Dict[str, Any]:
|
@@ -410,16 +414,18 @@ class BasicAgent:
|
|
410 |
|
411 |
print(f"Generated answer: {answer}")
|
412 |
return {
|
413 |
-
"
|
414 |
"current_step": "done",
|
415 |
-
"next_step": "done"
|
|
|
416 |
}
|
417 |
except Exception as e:
|
418 |
print(f"\nError processing Python file {state['file_url']}: {str(e)}")
|
419 |
return {
|
420 |
-
"
|
421 |
"current_step": "done",
|
422 |
-
"next_step": "done"
|
|
|
423 |
}
|
424 |
|
425 |
def _process_text(self, state: AgentState) -> Dict[str, Any]:
|
@@ -438,16 +444,18 @@ QUESTION:
|
|
438 |
answer = self._safe_parse(raw)
|
439 |
print(f"Generated answer: {answer}")
|
440 |
return {
|
441 |
-
"
|
442 |
"current_step": "done",
|
443 |
-
"next_step": "done"
|
|
|
444 |
}
|
445 |
except Exception as e:
|
446 |
print(f"\nLLM Error in answer generation: {str(e)}")
|
447 |
return {
|
448 |
-
"
|
449 |
"current_step": "done",
|
450 |
-
"next_step": "done"
|
|
|
451 |
}
|
452 |
|
453 |
def _build_workflow(self) -> Graph:
|
@@ -477,9 +485,9 @@ QUESTION:
|
|
477 |
}
|
478 |
)
|
479 |
|
480 |
-
#
|
481 |
for node in ["process_image", "process_video", "process_spreadsheet", "process_python", "process_text"]:
|
482 |
-
sg.
|
483 |
|
484 |
return sg.compile()
|
485 |
|
|
|
184 |
class AgentState(TypedDict):
|
185 |
question: str
|
186 |
current_step: str
|
187 |
+
answer: str
|
188 |
history: List[Dict[str, str]]
|
189 |
needs_search: bool
|
190 |
search_query: str
|
|
|
192 |
logs: Dict[str, Any]
|
193 |
file_url: str
|
194 |
code_blocks: List[Dict[str, str]]
|
195 |
+
next_step: str
|
196 |
|
197 |
# -------------------------
|
198 |
# BasicAgent implementation
|
|
|
249 |
state: AgentState = {
|
250 |
"question": question,
|
251 |
"current_step": "route",
|
252 |
+
"answer": "",
|
253 |
"history": [],
|
254 |
"needs_search": False,
|
255 |
"search_query": "",
|
|
|
266 |
|
267 |
try:
|
268 |
final_state = self.workflow.invoke(state)
|
269 |
+
if "answer" not in final_state:
|
270 |
+
raise ValueError(f"No 'answer' in state: keys are {list(final_state.keys())}")
|
271 |
+
return final_state["answer"]
|
|
|
|
|
272 |
except Exception as e:
|
273 |
print(f"Error in workflow execution: {str(e)}")
|
274 |
return f"Error processing question: {str(e)}"
|
|
|
330 |
|
331 |
print(f"Generated answer: {answer}")
|
332 |
return {
|
333 |
+
"answer": answer,
|
334 |
"current_step": "done",
|
335 |
+
"next_step": "done",
|
336 |
+
"history": state["history"] + [{"role": "assistant", "content": answer}]
|
337 |
}
|
338 |
except Exception as e:
|
339 |
print(f"\nError processing image {state['file_url']}: {str(e)}")
|
340 |
return {
|
341 |
+
"answer": f"Error processing image: {str(e)}",
|
342 |
"current_step": "done",
|
343 |
+
"next_step": "done",
|
344 |
+
"history": state["history"] + [{"role": "assistant", "content": f"Error: {str(e)}"}]
|
345 |
}
|
346 |
|
347 |
def _process_video(self, state: AgentState) -> Dict[str, Any]:
|
|
|
358 |
|
359 |
print(f"Generated answer: {answer}")
|
360 |
return {
|
361 |
+
"answer": answer,
|
362 |
"current_step": "done",
|
363 |
+
"next_step": "done",
|
364 |
+
"history": state["history"] + [{"role": "assistant", "content": answer}]
|
365 |
}
|
366 |
except Exception as e:
|
367 |
print(f"\nError processing video {state['file_url']}: {str(e)}")
|
368 |
return {
|
369 |
+
"answer": f"Error processing video: {str(e)}",
|
370 |
"current_step": "done",
|
371 |
+
"next_step": "done",
|
372 |
+
"history": state["history"] + [{"role": "assistant", "content": f"Error: {str(e)}"}]
|
373 |
}
|
374 |
|
375 |
def _process_spreadsheet(self, state: AgentState) -> Dict[str, Any]:
|
|
|
386 |
|
387 |
print(f"Generated answer: {answer}")
|
388 |
return {
|
389 |
+
"answer": answer,
|
390 |
"current_step": "done",
|
391 |
+
"next_step": "done",
|
392 |
+
"history": state["history"] + [{"role": "assistant", "content": answer}]
|
393 |
}
|
394 |
except Exception as e:
|
395 |
print(f"\nError processing spreadsheet {state['file_url']}: {str(e)}")
|
396 |
return {
|
397 |
+
"answer": f"Error processing spreadsheet: {str(e)}",
|
398 |
"current_step": "done",
|
399 |
+
"next_step": "done",
|
400 |
+
"history": state["history"] + [{"role": "assistant", "content": f"Error: {str(e)}"}]
|
401 |
}
|
402 |
|
403 |
def _process_python(self, state: AgentState) -> Dict[str, Any]:
|
|
|
414 |
|
415 |
print(f"Generated answer: {answer}")
|
416 |
return {
|
417 |
+
"answer": answer,
|
418 |
"current_step": "done",
|
419 |
+
"next_step": "done",
|
420 |
+
"history": state["history"] + [{"role": "assistant", "content": answer}]
|
421 |
}
|
422 |
except Exception as e:
|
423 |
print(f"\nError processing Python file {state['file_url']}: {str(e)}")
|
424 |
return {
|
425 |
+
"answer": f"Error processing Python file: {str(e)}",
|
426 |
"current_step": "done",
|
427 |
+
"next_step": "done",
|
428 |
+
"history": state["history"] + [{"role": "assistant", "content": f"Error: {str(e)}"}]
|
429 |
}
|
430 |
|
431 |
def _process_text(self, state: AgentState) -> Dict[str, Any]:
|
|
|
444 |
answer = self._safe_parse(raw)
|
445 |
print(f"Generated answer: {answer}")
|
446 |
return {
|
447 |
+
"answer": answer,
|
448 |
"current_step": "done",
|
449 |
+
"next_step": "done",
|
450 |
+
"history": state["history"] + [{"role": "assistant", "content": answer}]
|
451 |
}
|
452 |
except Exception as e:
|
453 |
print(f"\nLLM Error in answer generation: {str(e)}")
|
454 |
return {
|
455 |
+
"answer": "I encountered an error while generating the answer.",
|
456 |
"current_step": "done",
|
457 |
+
"next_step": "done",
|
458 |
+
"history": state["history"] + [{"role": "assistant", "content": "Error generating answer"}]
|
459 |
}
|
460 |
|
461 |
def _build_workflow(self) -> Graph:
|
|
|
485 |
}
|
486 |
)
|
487 |
|
488 |
+
# Add edges from each processing node to END
|
489 |
for node in ["process_image", "process_video", "process_spreadsheet", "process_python", "process_text"]:
|
490 |
+
sg.add_edge(node, "END")
|
491 |
|
492 |
return sg.compile()
|
493 |
|