Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
6 |
from typing import Dict, Any, List, TypedDict, Optional
|
7 |
from langgraph.graph import Graph, StateGraph
|
8 |
from langgraph.prebuilt import ToolNode
|
@@ -99,21 +100,31 @@ class BasicAgent:
|
|
99 |
def _analyze_question(self, state: AgentState) -> AgentState:
|
100 |
"""Analyze the question and determine the next step."""
|
101 |
prompt = f"""Analyze this question and determine what needs to be done: {state['question']}
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
state["search_query"] = analysis.get('search_query', '')
|
112 |
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
state["current_step"] = 'search'
|
115 |
-
else:
|
116 |
-
state["current_step"] = 'final_answer'
|
117 |
|
118 |
return state
|
119 |
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
import ast
|
7 |
from typing import Dict, Any, List, TypedDict, Optional
|
8 |
from langgraph.graph import Graph, StateGraph
|
9 |
from langgraph.prebuilt import ToolNode
|
|
|
100 |
def _analyze_question(self, state: AgentState) -> AgentState:
|
101 |
"""Analyze the question and determine the next step."""
|
102 |
prompt = f"""Analyze this question and determine what needs to be done: {state['question']}
|
103 |
+
|
104 |
+
Return only a valid Python dictionary in this exact format:
|
105 |
+
{{
|
106 |
+
"needs_search": true/false,
|
107 |
+
"search_query": "query if needed"
|
108 |
+
}}
|
109 |
+
|
110 |
+
Do not include any other text or explanation. Only return the dictionary.
|
111 |
+
"""
|
|
|
112 |
|
113 |
+
try:
|
114 |
+
analysis = ast.literal_eval(self._call_llm_api(prompt))
|
115 |
+
state["needs_more_info"] = analysis.get('needs_search', False)
|
116 |
+
state["search_query"] = analysis.get('search_query', '')
|
117 |
+
|
118 |
+
if analysis.get('needs_search', False):
|
119 |
+
state["current_step"] = 'search'
|
120 |
+
else:
|
121 |
+
state["current_step"] = 'final_answer'
|
122 |
+
except (ValueError, SyntaxError) as e:
|
123 |
+
print(f"Error parsing LLM response: {e}")
|
124 |
+
# Default to search if we can't parse the response
|
125 |
+
state["needs_more_info"] = True
|
126 |
+
state["search_query"] = state["question"]
|
127 |
state["current_step"] = 'search'
|
|
|
|
|
128 |
|
129 |
return state
|
130 |
|