Spaces:
Sleeping
Sleeping
system
Browse files
agent.py
CHANGED
@@ -9,19 +9,25 @@ from tools import (
|
|
9 |
|
10 |
# ββββββββββββββββββββββββββββββββ Config ββββββββββββββββββββββββββββββββ
|
11 |
SYSTEM_PROMPT = """
|
12 |
-
You are a
|
13 |
-
Thought: ...
|
14 |
-
Action: ...
|
15 |
-
Action Input: ...
|
16 |
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
When using task-based tools (audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool), ONLY use the 'task_id' value.
|
25 |
"""
|
26 |
|
27 |
TOOLS = [
|
@@ -34,7 +40,7 @@ TOOLS = [
|
|
34 |
class SimpleLangGraphAgent:
|
35 |
def __init__(self, model_name="gpt-4o-mini"):
|
36 |
self.agent = create_react_agent(
|
37 |
-
model=ChatOpenAI(model_name=model_name, temperature=0),
|
38 |
tools=TOOLS,
|
39 |
)
|
40 |
|
@@ -49,6 +55,7 @@ class SimpleLangGraphAgent:
|
|
49 |
|
50 |
for msg in final_state["messages"][::-1]:
|
51 |
if "FINAL ANSWER:" in msg.content.upper():
|
|
|
52 |
return msg.content.split("FINAL ANSWER:")[-1].strip()
|
53 |
|
54 |
return "No FINAL ANSWER found."
|
|
|
9 |
|
10 |
# ββββββββββββββββββββββββββββββββ Config ββββββββββββββββββββββββββββββββ
|
11 |
SYSTEM_PROMPT = """
|
12 |
+
You are a helpful AI assistant. You will answer questions by thinking step-by-step, taking actions using tools when necessary, and finishing with a final answer.
|
|
|
|
|
|
|
13 |
|
14 |
+
When you want to use a tool, respond *exactly* in the following format:
|
15 |
|
16 |
+
Thought: [your reasoning]
|
17 |
+
Action: [tool_name]
|
18 |
+
Action Input: [input]
|
19 |
|
20 |
+
When you receive an observation, continue reasoning. When you are confident in your answer, write:
|
21 |
+
|
22 |
+
Thought: [final reasoning]
|
23 |
+
FINAL ANSWER: [your answer]
|
24 |
+
|
25 |
+
IMPORTANT:
|
26 |
+
- If using tools that require a `task_id`, only use the value provided.
|
27 |
+
- Do not make up tool names.
|
28 |
+
- Do not loop unnecessarily.
|
29 |
+
- Provide FINAL ANSWER as soon as you are confident.
|
30 |
|
|
|
31 |
"""
|
32 |
|
33 |
TOOLS = [
|
|
|
40 |
class SimpleLangGraphAgent:
|
41 |
def __init__(self, model_name="gpt-4o-mini"):
|
42 |
self.agent = create_react_agent(
|
43 |
+
model=ChatOpenAI(model_name=model_name, temperature=0.3),
|
44 |
tools=TOOLS,
|
45 |
)
|
46 |
|
|
|
55 |
|
56 |
for msg in final_state["messages"][::-1]:
|
57 |
if "FINAL ANSWER:" in msg.content.upper():
|
58 |
+
print(f"\n\n msg.content = {msg.content} \n\n")
|
59 |
return msg.content.split("FINAL ANSWER:")[-1].strip()
|
60 |
|
61 |
return "No FINAL ANSWER found."
|
app.py
CHANGED
@@ -14,38 +14,7 @@ from state import AgentState
|
|
14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
15 |
|
16 |
|
17 |
-
|
18 |
-
You are a helpful AI assistant using a ReAct pattern. You will answer questions by thinking step-by-step, taking actions using tools when necessary, and finishing with a final answer.
|
19 |
-
|
20 |
-
When you want to use a tool, respond *exactly* in the following format:
|
21 |
-
|
22 |
-
Thought: [your reasoning]
|
23 |
-
Action: [tool_name]
|
24 |
-
Action Input: [input]
|
25 |
-
|
26 |
-
When you receive an observation, continue reasoning. When you are confident in your answer, write:
|
27 |
-
|
28 |
-
Thought: [final reasoning]
|
29 |
-
FINAL ANSWER: [your answer]
|
30 |
-
|
31 |
-
Use only the available tools:
|
32 |
-
- wikipedia_search_tool
|
33 |
-
- arxiv_search_tool
|
34 |
-
- audio_transcriber_tool
|
35 |
-
- excel_tool
|
36 |
-
- analyze_code_tool
|
37 |
-
- image_tool
|
38 |
-
- add_tool
|
39 |
-
- subtract_tool
|
40 |
-
- multiply_tool
|
41 |
-
- divide_tool
|
42 |
-
|
43 |
-
IMPORTANT:
|
44 |
-
- If using tools that require a `task_id`, only use the value provided.
|
45 |
-
- Do not make up tool names.
|
46 |
-
- Do not loop unnecessarily.
|
47 |
-
- Provide FINAL ANSWER as soon as you are confident.
|
48 |
-
"""
|
49 |
|
50 |
|
51 |
class BasicAgent:
|
|
|
14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
15 |
|
16 |
|
17 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
class BasicAgent:
|
tools.py
CHANGED
@@ -34,7 +34,7 @@ def _download_file_for_task(task_id: str, ext: str) -> str:
|
|
34 |
try:
|
35 |
resp = requests.get(url, timeout=10)
|
36 |
if resp.status_code == 200 and resp.content:
|
37 |
-
print(f"Downloaded file from {url} to {local_path}")
|
38 |
with open(local_path, "wb") as f:
|
39 |
f.write(resp.content)
|
40 |
return local_path
|
@@ -179,6 +179,7 @@ def audio_transcriber_tool(task_id: str) -> str:
|
|
179 |
break
|
180 |
|
181 |
if not local_audio or not os.path.exists(local_audio):
|
|
|
182 |
return "Error: No audio file found (download failed)."
|
183 |
|
184 |
|
@@ -194,7 +195,7 @@ def audio_transcriber_tool(task_id: str) -> str:
|
|
194 |
model="whisper-1",
|
195 |
file=audio_file,
|
196 |
)
|
197 |
-
print("reached response")
|
198 |
text = response.text.strip()
|
199 |
except Exception as e:
|
200 |
text = f"Error during transcription: {e}"
|
@@ -213,12 +214,13 @@ def wikipedia_search_tool(wiki_query: str) -> str:
|
|
213 |
Purpose: When the user asks for historical, biographical, scientific, or factual information, use this tool.
|
214 |
|
215 |
Input: A string describing a topic to search on Wikipedia.
|
216 |
-
|
217 |
Example usage:
|
218 |
- "Who was Marie Curie?"
|
219 |
- "Explain quantum entanglement"
|
220 |
- "Tell me about the French Revolution"
|
221 |
"""
|
|
|
222 |
try:
|
223 |
docs = WikipediaLoader(query=wiki_query, load_max_docs=3).load() # Reduced from 5 to 3
|
224 |
|
@@ -253,6 +255,7 @@ def wikipedia_search_tool(wiki_query: str) -> str:
|
|
253 |
break
|
254 |
|
255 |
if not result.strip():
|
|
|
256 |
return "No Wikipedia results found for the given query. [END_OF_SEARCH]"
|
257 |
|
258 |
# Add clear end marker
|
@@ -279,12 +282,14 @@ def arxiv_search_tool(query: str) -> str:
|
|
279 |
- "What are recent studies on climate change?"
|
280 |
- "Search for papers on quantum computing"
|
281 |
"""
|
|
|
282 |
try:
|
283 |
# Search arXiv for the top result
|
284 |
search = arxiv.Search(query=query, max_results=1, sort_by=arxiv.SortCriterion.Relevance)
|
285 |
result = next(search.results(), None)
|
286 |
|
287 |
if not result:
|
|
|
288 |
return "No results found. [END_OF_SEARCH]"
|
289 |
|
290 |
# Download PDF
|
@@ -330,21 +335,19 @@ def analyze_code_tool(task_id: str) -> str:
|
|
330 |
- "Analyze the code file for bugs"
|
331 |
- "Explain the functions in this script"
|
332 |
"""
|
333 |
-
print("
|
334 |
code_txt = ""
|
335 |
if not task_id:
|
336 |
code_txt = "No code provided."
|
337 |
else:
|
338 |
path = _download_file_for_task(task_id, "py")
|
339 |
if not path:
|
|
|
340 |
return "Error: .py file not found for this task."
|
341 |
code_txt = Path(path).read_text(encoding="utf-8", errors="ignore")
|
342 |
-
# else:
|
343 |
-
# return "Error: neither snippet nor file provided."
|
344 |
|
345 |
-
|
346 |
-
|
347 |
-
code_sample = "\n".join(lines)[:10_000]
|
348 |
|
349 |
prompt = [
|
350 |
SystemMessage(content="You are a senior Python code reviewer."),
|
@@ -376,6 +379,7 @@ def add_tool(a: float, b: float) -> str:
|
|
376 |
- "Add 3.14 and 2.86"
|
377 |
- "Calculate the sum of 100 and 250"
|
378 |
"""
|
|
|
379 |
result = a + b
|
380 |
return f"Addition result: {a} + {b} = {result}"
|
381 |
|
@@ -393,6 +397,7 @@ def subtract_tool(a: float, b: float) -> str:
|
|
393 |
- "Subtract 15.5 from 40.2"
|
394 |
- "Calculate 1000 minus 347"
|
395 |
"""
|
|
|
396 |
result = a - b
|
397 |
return f"Subtraction result: {a} - {b} = {result}"
|
398 |
|
@@ -410,6 +415,7 @@ def multiply_tool(a: float, b: float) -> str:
|
|
410 |
- "Multiply 12.5 by 4"
|
411 |
- "Calculate the product of 15 and 20"
|
412 |
"""
|
|
|
413 |
result = a * b
|
414 |
return f"Multiplication result: {a} Γ {b} = {result}"
|
415 |
|
@@ -427,6 +433,7 @@ def divide_tool(a: float, b: float) -> str:
|
|
427 |
- "Divide 75 by 3"
|
428 |
- "Calculate 144 divided by 12"
|
429 |
"""
|
|
|
430 |
if b == 0:
|
431 |
return "Division error: Cannot divide by zero"
|
432 |
result = a / b
|
|
|
34 |
try:
|
35 |
resp = requests.get(url, timeout=10)
|
36 |
if resp.status_code == 200 and resp.content:
|
37 |
+
print(f"\n Downloaded file from {url} to {local_path} \n")
|
38 |
with open(local_path, "wb") as f:
|
39 |
f.write(resp.content)
|
40 |
return local_path
|
|
|
179 |
break
|
180 |
|
181 |
if not local_audio or not os.path.exists(local_audio):
|
182 |
+
print("Error: No audio file found (download failed).")
|
183 |
return "Error: No audio file found (download failed)."
|
184 |
|
185 |
|
|
|
195 |
model="whisper-1",
|
196 |
file=audio_file,
|
197 |
)
|
198 |
+
# print("reached response")
|
199 |
text = response.text.strip()
|
200 |
except Exception as e:
|
201 |
text = f"Error during transcription: {e}"
|
|
|
214 |
Purpose: When the user asks for historical, biographical, scientific, or factual information, use this tool.
|
215 |
|
216 |
Input: A string describing a topic to search on Wikipedia.
|
217 |
+
Tips: If you dont get enough information, try again with a different querry
|
218 |
Example usage:
|
219 |
- "Who was Marie Curie?"
|
220 |
- "Explain quantum entanglement"
|
221 |
- "Tell me about the French Revolution"
|
222 |
"""
|
223 |
+
print("Reached Wikipedia tool, with query = ", wiki_query)
|
224 |
try:
|
225 |
docs = WikipediaLoader(query=wiki_query, load_max_docs=3).load() # Reduced from 5 to 3
|
226 |
|
|
|
255 |
break
|
256 |
|
257 |
if not result.strip():
|
258 |
+
print("No wiki result found")
|
259 |
return "No Wikipedia results found for the given query. [END_OF_SEARCH]"
|
260 |
|
261 |
# Add clear end marker
|
|
|
282 |
- "What are recent studies on climate change?"
|
283 |
- "Search for papers on quantum computing"
|
284 |
"""
|
285 |
+
print("Reached ArXiv tool, with query = ", query)
|
286 |
try:
|
287 |
# Search arXiv for the top result
|
288 |
search = arxiv.Search(query=query, max_results=1, sort_by=arxiv.SortCriterion.Relevance)
|
289 |
result = next(search.results(), None)
|
290 |
|
291 |
if not result:
|
292 |
+
print("No arXiv result found")
|
293 |
return "No results found. [END_OF_SEARCH]"
|
294 |
|
295 |
# Download PDF
|
|
|
335 |
- "Analyze the code file for bugs"
|
336 |
- "Explain the functions in this script"
|
337 |
"""
|
338 |
+
print("Reached analyze_code_tool")
|
339 |
code_txt = ""
|
340 |
if not task_id:
|
341 |
code_txt = "No code provided."
|
342 |
else:
|
343 |
path = _download_file_for_task(task_id, "py")
|
344 |
if not path:
|
345 |
+
print("Error: .py file not found for this task.")
|
346 |
return "Error: .py file not found for this task."
|
347 |
code_txt = Path(path).read_text(encoding="utf-8", errors="ignore")
|
|
|
|
|
348 |
|
349 |
+
lines = code_txt.splitlines()
|
350 |
+
code_sample = "\n".join(lines)
|
|
|
351 |
|
352 |
prompt = [
|
353 |
SystemMessage(content="You are a senior Python code reviewer."),
|
|
|
379 |
- "Add 3.14 and 2.86"
|
380 |
- "Calculate the sum of 100 and 250"
|
381 |
"""
|
382 |
+
print("Reached add_tool")
|
383 |
result = a + b
|
384 |
return f"Addition result: {a} + {b} = {result}"
|
385 |
|
|
|
397 |
- "Subtract 15.5 from 40.2"
|
398 |
- "Calculate 1000 minus 347"
|
399 |
"""
|
400 |
+
print("Reached subtract_tool")
|
401 |
result = a - b
|
402 |
return f"Subtraction result: {a} - {b} = {result}"
|
403 |
|
|
|
415 |
- "Multiply 12.5 by 4"
|
416 |
- "Calculate the product of 15 and 20"
|
417 |
"""
|
418 |
+
print("Reached multiply_tool")
|
419 |
result = a * b
|
420 |
return f"Multiplication result: {a} Γ {b} = {result}"
|
421 |
|
|
|
433 |
- "Divide 75 by 3"
|
434 |
- "Calculate 144 divided by 12"
|
435 |
"""
|
436 |
+
print("Reached divide_tool")
|
437 |
if b == 0:
|
438 |
return "Division error: Cannot divide by zero"
|
439 |
result = a / b
|