pydanticc_remove
Browse files
app.py
CHANGED
@@ -3,10 +3,9 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
-
from typing import Dict, Any, List, TypedDict
|
7 |
from langgraph.graph import Graph, StateGraph
|
8 |
from langgraph.prebuilt import ToolNode
|
9 |
-
from pydantic import BaseModel, Field
|
10 |
from tools import create_calculator_tool, create_search_tool
|
11 |
print("trial")
|
12 |
# (Keep Constants as is)
|
@@ -15,15 +14,14 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
15 |
MODEL_API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-32B-Instruct"
|
16 |
HF_TOKEN = os.getenv("HF_TOKEN") # Make sure to set this environment variable
|
17 |
|
18 |
-
class AgentState(
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
search_query: str = Field(default="", description="Current search query if any")
|
27 |
|
28 |
class BasicAgent:
|
29 |
def __init__(self):
|
@@ -52,7 +50,7 @@ class BasicAgent:
|
|
52 |
"""Create the agent workflow using LangGraph."""
|
53 |
# Create the workflow with state schema
|
54 |
print("Creating Stategraph : error happens here?")
|
55 |
-
workflow = StateGraph(state_schema=
|
56 |
print("Stategraph created")
|
57 |
# Add nodes
|
58 |
workflow.add_node("analyze", self._analyze_question)
|
@@ -68,12 +66,12 @@ class BasicAgent:
|
|
68 |
workflow.add_edge("search", "final_answer")
|
69 |
|
70 |
# Define conditional edges
|
71 |
-
def router(state:
|
72 |
-
if state
|
73 |
return 'calculator'
|
74 |
-
elif state
|
75 |
return 'search'
|
76 |
-
elif state
|
77 |
return 'final_answer'
|
78 |
return 'analyze'
|
79 |
|
@@ -107,9 +105,9 @@ class BasicAgent:
|
|
107 |
print(f"Error calling LLM API: {e}")
|
108 |
return f"Error getting response from LLM: {str(e)}"
|
109 |
|
110 |
-
def _analyze_question(self, state:
|
111 |
"""Analyze the question and determine the next step."""
|
112 |
-
prompt = f"""Analyze this question and determine what needs to be done: {state
|
113 |
Return your analysis in this format:
|
114 |
{{
|
115 |
"needs_calculation": true/false,
|
@@ -123,71 +121,71 @@ class BasicAgent:
|
|
123 |
"""
|
124 |
|
125 |
analysis = eval(self._call_llm_api(prompt))
|
126 |
-
state
|
127 |
-
state
|
128 |
|
129 |
if analysis.get('needs_calculation', False):
|
130 |
-
state
|
131 |
-
state
|
132 |
elif analysis.get('needs_search', False):
|
133 |
-
state
|
134 |
else:
|
135 |
-
state
|
136 |
|
137 |
return state
|
138 |
|
139 |
-
def _use_calculator(self, state:
|
140 |
"""Use the calculator tool."""
|
141 |
try:
|
142 |
# Create calculator state with input from tool_output
|
143 |
-
calc_input = eval(state
|
144 |
-
result = self.calculator.invoke(
|
145 |
|
146 |
-
state
|
147 |
'step': 'calculator',
|
148 |
-
'input': state
|
149 |
-
'output': str(result['output']
|
150 |
})
|
151 |
-
state
|
152 |
except Exception as e:
|
153 |
-
state
|
154 |
'step': 'calculator_error',
|
155 |
'error': str(e)
|
156 |
})
|
157 |
-
state
|
158 |
return state
|
159 |
|
160 |
-
def _use_search(self, state:
|
161 |
"""Use the search tool."""
|
162 |
try:
|
163 |
# Create search state with input from search_query
|
164 |
-
search_input =
|
165 |
-
query
|
166 |
-
max_results
|
167 |
-
|
168 |
-
result = self.search_tool.invoke(
|
169 |
|
170 |
-
state
|
171 |
'step': 'search',
|
172 |
-
'query': state
|
173 |
-
'results': [str(r) for r in result['output']
|
174 |
})
|
175 |
-
state
|
176 |
-
state
|
177 |
except Exception as e:
|
178 |
-
state
|
179 |
'step': 'search_error',
|
180 |
'error': str(e)
|
181 |
})
|
182 |
-
state
|
183 |
return state
|
184 |
|
185 |
-
def _generate_final_answer(self, state:
|
186 |
"""Generate the final answer based on all gathered information."""
|
187 |
history_str = "\n".join([f"{h['step']}: {h.get('output', h.get('results', h.get('error', '')))}"
|
188 |
-
for h in state
|
189 |
|
190 |
-
prompt = f"""Based on the following information and history, provide a final answer to the question: {state
|
191 |
|
192 |
History of steps taken:
|
193 |
{history_str}
|
@@ -195,7 +193,7 @@ class BasicAgent:
|
|
195 |
Provide a clear, concise answer that addresses the original question.
|
196 |
"""
|
197 |
|
198 |
-
state
|
199 |
return state
|
200 |
|
201 |
def __call__(self, question: str) -> str:
|
@@ -204,19 +202,19 @@ class BasicAgent:
|
|
204 |
|
205 |
try:
|
206 |
# Initialize the state
|
207 |
-
initial_state =
|
208 |
-
question
|
209 |
-
current_step
|
210 |
-
tool_output
|
211 |
-
final_answer
|
212 |
-
history
|
213 |
-
needs_more_info
|
214 |
-
search_query
|
215 |
-
|
216 |
|
217 |
# Run the workflow
|
218 |
final_state = self.workflow.invoke(initial_state)
|
219 |
-
return final_state
|
220 |
|
221 |
except Exception as e:
|
222 |
print(f"Error in agent processing: {e}")
|
@@ -289,15 +287,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
289 |
|
290 |
try:
|
291 |
# Initialize the state for this question
|
292 |
-
initial_state =
|
293 |
-
question
|
294 |
-
current_step
|
295 |
-
tool_output
|
296 |
-
final_answer
|
297 |
-
history
|
298 |
-
needs_more_info
|
299 |
-
search_query
|
300 |
-
|
301 |
|
302 |
# Run the workflow for this question
|
303 |
print(f"\nProcessing question {task_id}: {question_text[:50]}...")
|
@@ -308,11 +306,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
308 |
f"Step: {h['step']}\n" +
|
309 |
f"Input: {h.get('input', h.get('query', ''))}\n" +
|
310 |
f"Output: {h.get('output', h.get('results', h.get('error', '')))}"
|
311 |
-
for h in final_state
|
312 |
])
|
313 |
|
314 |
# Add to results
|
315 |
-
submitted_answer = final_state
|
316 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
317 |
results_log.append({
|
318 |
"Task ID": task_id,
|
@@ -321,7 +319,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
321 |
"Workflow History": workflow_history
|
322 |
})
|
323 |
|
324 |
-
print(f"Completed question {task_id} with {len(final_state
|
325 |
|
326 |
except Exception as e:
|
327 |
print(f"Error running agent workflow on task {task_id}: {e}")
|
|
|
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
|
|
|
9 |
from tools import create_calculator_tool, create_search_tool
|
10 |
print("trial")
|
11 |
# (Keep Constants as is)
|
|
|
14 |
MODEL_API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-32B-Instruct"
|
15 |
HF_TOKEN = os.getenv("HF_TOKEN") # Make sure to set this environment variable
|
16 |
|
17 |
+
class AgentState(TypedDict):
|
18 |
+
question: str
|
19 |
+
current_step: str
|
20 |
+
tool_output: str
|
21 |
+
final_answer: str
|
22 |
+
history: List[Dict[str, str]]
|
23 |
+
needs_more_info: bool
|
24 |
+
search_query: str
|
|
|
25 |
|
26 |
class BasicAgent:
|
27 |
def __init__(self):
|
|
|
50 |
"""Create the agent workflow using LangGraph."""
|
51 |
# Create the workflow with state schema
|
52 |
print("Creating Stategraph : error happens here?")
|
53 |
+
workflow = StateGraph(state_schema=Dict[str, Any])
|
54 |
print("Stategraph created")
|
55 |
# Add nodes
|
56 |
workflow.add_node("analyze", self._analyze_question)
|
|
|
66 |
workflow.add_edge("search", "final_answer")
|
67 |
|
68 |
# Define conditional edges
|
69 |
+
def router(state: Dict[str, Any]) -> str:
|
70 |
+
if state["current_step"] == 'calculator':
|
71 |
return 'calculator'
|
72 |
+
elif state["current_step"] == 'search':
|
73 |
return 'search'
|
74 |
+
elif state["current_step"] == 'final_answer':
|
75 |
return 'final_answer'
|
76 |
return 'analyze'
|
77 |
|
|
|
105 |
print(f"Error calling LLM API: {e}")
|
106 |
return f"Error getting response from LLM: {str(e)}"
|
107 |
|
108 |
+
def _analyze_question(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
109 |
"""Analyze the question and determine the next step."""
|
110 |
+
prompt = f"""Analyze this question and determine what needs to be done: {state['question']}
|
111 |
Return your analysis in this format:
|
112 |
{{
|
113 |
"needs_calculation": true/false,
|
|
|
121 |
"""
|
122 |
|
123 |
analysis = eval(self._call_llm_api(prompt))
|
124 |
+
state["needs_more_info"] = analysis.get('needs_search', False)
|
125 |
+
state["search_query"] = analysis.get('search_query', '')
|
126 |
|
127 |
if analysis.get('needs_calculation', False):
|
128 |
+
state["current_step"] = 'calculator'
|
129 |
+
state["tool_output"] = str(analysis['calculation'])
|
130 |
elif analysis.get('needs_search', False):
|
131 |
+
state["current_step"] = 'search'
|
132 |
else:
|
133 |
+
state["current_step"] = 'final_answer'
|
134 |
|
135 |
return state
|
136 |
|
137 |
+
def _use_calculator(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
138 |
"""Use the calculator tool."""
|
139 |
try:
|
140 |
# Create calculator state with input from tool_output
|
141 |
+
calc_input = eval(state["tool_output"])
|
142 |
+
result = self.calculator.invoke({"input": calc_input})
|
143 |
|
144 |
+
state["history"].append({
|
145 |
'step': 'calculator',
|
146 |
+
'input': state["tool_output"],
|
147 |
+
'output': str(result['output']['result'])
|
148 |
})
|
149 |
+
state["current_step"] = 'final_answer'
|
150 |
except Exception as e:
|
151 |
+
state["history"].append({
|
152 |
'step': 'calculator_error',
|
153 |
'error': str(e)
|
154 |
})
|
155 |
+
state["current_step"] = 'final_answer'
|
156 |
return state
|
157 |
|
158 |
+
def _use_search(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
159 |
"""Use the search tool."""
|
160 |
try:
|
161 |
# Create search state with input from search_query
|
162 |
+
search_input = {
|
163 |
+
"query": state["search_query"],
|
164 |
+
"max_results": 3
|
165 |
+
}
|
166 |
+
result = self.search_tool.invoke({"input": search_input})
|
167 |
|
168 |
+
state["history"].append({
|
169 |
'step': 'search',
|
170 |
+
'query': state["search_query"],
|
171 |
+
'results': [str(r) for r in result['output']['results']]
|
172 |
})
|
173 |
+
state["needs_more_info"] = False
|
174 |
+
state["current_step"] = 'final_answer'
|
175 |
except Exception as e:
|
176 |
+
state["history"].append({
|
177 |
'step': 'search_error',
|
178 |
'error': str(e)
|
179 |
})
|
180 |
+
state["current_step"] = 'final_answer'
|
181 |
return state
|
182 |
|
183 |
+
def _generate_final_answer(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
184 |
"""Generate the final answer based on all gathered information."""
|
185 |
history_str = "\n".join([f"{h['step']}: {h.get('output', h.get('results', h.get('error', '')))}"
|
186 |
+
for h in state["history"]])
|
187 |
|
188 |
+
prompt = f"""Based on the following information and history, provide a final answer to the question: {state['question']}
|
189 |
|
190 |
History of steps taken:
|
191 |
{history_str}
|
|
|
193 |
Provide a clear, concise answer that addresses the original question.
|
194 |
"""
|
195 |
|
196 |
+
state["final_answer"] = self._call_llm_api(prompt)
|
197 |
return state
|
198 |
|
199 |
def __call__(self, question: str) -> str:
|
|
|
202 |
|
203 |
try:
|
204 |
# Initialize the state
|
205 |
+
initial_state = {
|
206 |
+
"question": question,
|
207 |
+
"current_step": "analyze",
|
208 |
+
"tool_output": "",
|
209 |
+
"final_answer": "",
|
210 |
+
"history": [],
|
211 |
+
"needs_more_info": False,
|
212 |
+
"search_query": ""
|
213 |
+
}
|
214 |
|
215 |
# Run the workflow
|
216 |
final_state = self.workflow.invoke(initial_state)
|
217 |
+
return final_state["final_answer"]
|
218 |
|
219 |
except Exception as e:
|
220 |
print(f"Error in agent processing: {e}")
|
|
|
287 |
|
288 |
try:
|
289 |
# Initialize the state for this question
|
290 |
+
initial_state = {
|
291 |
+
"question": question_text,
|
292 |
+
"current_step": "analyze",
|
293 |
+
"tool_output": "",
|
294 |
+
"final_answer": "",
|
295 |
+
"history": [],
|
296 |
+
"needs_more_info": False,
|
297 |
+
"search_query": ""
|
298 |
+
}
|
299 |
|
300 |
# Run the workflow for this question
|
301 |
print(f"\nProcessing question {task_id}: {question_text[:50]}...")
|
|
|
306 |
f"Step: {h['step']}\n" +
|
307 |
f"Input: {h.get('input', h.get('query', ''))}\n" +
|
308 |
f"Output: {h.get('output', h.get('results', h.get('error', '')))}"
|
309 |
+
for h in final_state["history"]
|
310 |
])
|
311 |
|
312 |
# Add to results
|
313 |
+
submitted_answer = final_state["final_answer"]
|
314 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
315 |
results_log.append({
|
316 |
"Task ID": task_id,
|
|
|
319 |
"Workflow History": workflow_history
|
320 |
})
|
321 |
|
322 |
+
print(f"Completed question {task_id} with {len(final_state['history'])} workflow steps")
|
323 |
|
324 |
except Exception as e:
|
325 |
print(f"Error running agent workflow on task {task_id}: {e}")
|
tools.py
CHANGED
@@ -1,71 +1,60 @@
|
|
1 |
-
from typing import Dict, Any, List
|
2 |
from langgraph.graph import Graph, StateGraph
|
3 |
from langgraph.prebuilt import ToolNode
|
4 |
-
from pydantic import BaseModel, Field
|
5 |
from duckduckgo_search import DDGS
|
6 |
|
7 |
-
class CalculatorInput(
|
8 |
-
operation: str
|
9 |
-
numbers: List[float]
|
10 |
|
11 |
-
class CalculatorOutput(
|
12 |
-
result: float
|
13 |
-
operation: str
|
14 |
|
15 |
-
class
|
16 |
-
|
17 |
-
|
|
|
18 |
|
19 |
-
class
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
class SearchResult(BaseModel):
|
24 |
-
title: str = Field(..., description="Title of the search result")
|
25 |
-
link: str = Field(..., description="URL of the search result")
|
26 |
-
snippet: str = Field(..., description="Brief description of the search result")
|
27 |
-
|
28 |
-
class SearchOutput(BaseModel):
|
29 |
-
results: List[SearchResult] = Field(..., description="List of search results")
|
30 |
-
query: str = Field(..., description="The original search query")
|
31 |
-
|
32 |
-
class SearchState(BaseModel):
|
33 |
-
input: SearchInput
|
34 |
-
output: SearchOutput = None
|
35 |
|
36 |
def create_calculator_tool() -> Graph:
|
37 |
"""Creates a calculator tool using LangGraph that can perform basic arithmetic operations."""
|
38 |
print("Creating calculator tool")
|
39 |
-
|
|
|
40 |
print("Calculator function called")
|
41 |
-
|
|
|
42 |
raise ValueError("At least two numbers are required for calculation")
|
43 |
|
44 |
-
result =
|
45 |
|
46 |
-
for num in
|
47 |
-
if
|
48 |
result += num
|
49 |
-
elif
|
50 |
result -= num
|
51 |
-
elif
|
52 |
result *= num
|
53 |
-
elif
|
54 |
if num == 0:
|
55 |
raise ValueError("Cannot divide by zero")
|
56 |
result /= num
|
57 |
else:
|
58 |
-
raise ValueError(f"Unsupported operation: {
|
59 |
|
60 |
return {
|
61 |
-
"output":
|
62 |
-
result
|
63 |
-
operation
|
64 |
-
|
65 |
}
|
66 |
|
67 |
# Create the graph with state schema
|
68 |
-
workflow = StateGraph(state_schema=
|
69 |
print("Calculator graph for workflow created")
|
70 |
# Add the calculator tool node
|
71 |
workflow.add_node("calculator", ToolNode(calculator_function))
|
@@ -79,34 +68,34 @@ def create_calculator_tool() -> Graph:
|
|
79 |
def create_search_tool() -> Graph:
|
80 |
"""Creates a search tool using DuckDuckGo that can search for information online."""
|
81 |
|
82 |
-
def search_function(state:
|
83 |
with DDGS() as ddgs:
|
84 |
# Run search
|
85 |
raw_results = list(ddgs.text(
|
86 |
-
state
|
87 |
-
max_results=state
|
88 |
))
|
89 |
|
90 |
results = []
|
91 |
for r in raw_results:
|
92 |
try:
|
93 |
-
results.append(
|
94 |
-
title
|
95 |
-
link
|
96 |
-
snippet
|
97 |
-
)
|
98 |
except Exception as e:
|
99 |
print("Skipping malformed search result:", r, "Error:", e)
|
100 |
|
101 |
return {
|
102 |
-
"output":
|
103 |
-
results
|
104 |
-
query
|
105 |
-
|
106 |
}
|
107 |
|
108 |
# Create the graph with state schema
|
109 |
-
workflow = StateGraph(state_schema=
|
110 |
|
111 |
# Add the search tool node
|
112 |
workflow.add_node("search", ToolNode(search_function))
|
|
|
1 |
+
from typing import Dict, Any, List, TypedDict, Optional
|
2 |
from langgraph.graph import Graph, StateGraph
|
3 |
from langgraph.prebuilt import ToolNode
|
|
|
4 |
from duckduckgo_search import DDGS
|
5 |
|
6 |
+
class CalculatorInput(TypedDict):
|
7 |
+
operation: str
|
8 |
+
numbers: List[float]
|
9 |
|
10 |
+
class CalculatorOutput(TypedDict):
|
11 |
+
result: float
|
12 |
+
operation: str
|
13 |
|
14 |
+
class SearchResult(TypedDict):
|
15 |
+
title: str
|
16 |
+
link: str
|
17 |
+
snippet: str
|
18 |
|
19 |
+
class SearchOutput(TypedDict):
|
20 |
+
results: List[SearchResult]
|
21 |
+
query: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def create_calculator_tool() -> Graph:
|
24 |
"""Creates a calculator tool using LangGraph that can perform basic arithmetic operations."""
|
25 |
print("Creating calculator tool")
|
26 |
+
|
27 |
+
def calculator_function(state: Dict[str, Any]) -> dict:
|
28 |
print("Calculator function called")
|
29 |
+
input_data = state["input"]
|
30 |
+
if len(input_data["numbers"]) < 2:
|
31 |
raise ValueError("At least two numbers are required for calculation")
|
32 |
|
33 |
+
result = input_data["numbers"][0]
|
34 |
|
35 |
+
for num in input_data["numbers"][1:]:
|
36 |
+
if input_data["operation"] == "add":
|
37 |
result += num
|
38 |
+
elif input_data["operation"] == "subtract":
|
39 |
result -= num
|
40 |
+
elif input_data["operation"] == "multiply":
|
41 |
result *= num
|
42 |
+
elif input_data["operation"] == "divide":
|
43 |
if num == 0:
|
44 |
raise ValueError("Cannot divide by zero")
|
45 |
result /= num
|
46 |
else:
|
47 |
+
raise ValueError(f"Unsupported operation: {input_data['operation']}")
|
48 |
|
49 |
return {
|
50 |
+
"output": {
|
51 |
+
"result": result,
|
52 |
+
"operation": input_data["operation"]
|
53 |
+
}
|
54 |
}
|
55 |
|
56 |
# Create the graph with state schema
|
57 |
+
workflow = StateGraph(state_schema=Dict[str, Any])
|
58 |
print("Calculator graph for workflow created")
|
59 |
# Add the calculator tool node
|
60 |
workflow.add_node("calculator", ToolNode(calculator_function))
|
|
|
68 |
def create_search_tool() -> Graph:
|
69 |
"""Creates a search tool using DuckDuckGo that can search for information online."""
|
70 |
|
71 |
+
def search_function(state: Dict[str, Any]) -> dict:
|
72 |
with DDGS() as ddgs:
|
73 |
# Run search
|
74 |
raw_results = list(ddgs.text(
|
75 |
+
state["input"]["query"],
|
76 |
+
max_results=state["input"].get("max_results", 3)
|
77 |
))
|
78 |
|
79 |
results = []
|
80 |
for r in raw_results:
|
81 |
try:
|
82 |
+
results.append({
|
83 |
+
"title": r.get("title", ""),
|
84 |
+
"link": r.get("href", r.get("link", "")), # DuckDuckGo sometimes uses "href"
|
85 |
+
"snippet": r.get("body", r.get("snippet", ""))
|
86 |
+
})
|
87 |
except Exception as e:
|
88 |
print("Skipping malformed search result:", r, "Error:", e)
|
89 |
|
90 |
return {
|
91 |
+
"output": {
|
92 |
+
"results": results,
|
93 |
+
"query": state["input"]["query"]
|
94 |
+
}
|
95 |
}
|
96 |
|
97 |
# Create the graph with state schema
|
98 |
+
workflow = StateGraph(state_schema=Dict[str, Any])
|
99 |
|
100 |
# Add the search tool node
|
101 |
workflow.add_node("search", ToolNode(search_function))
|