Update agent.py
Browse files
agent.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# agent.py —
|
2 |
|
3 |
import os
|
4 |
import asyncio
|
@@ -24,16 +24,19 @@ wiki_api = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=1000)
|
|
24 |
|
25 |
def search_wikipedia(query: str) -> str:
|
26 |
"""Search Wikipedia for a given query and return relevant summary."""
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
# Tool 2 — Python
|
30 |
python_tool = PythonREPLTool()
|
31 |
|
32 |
def run_python_code(code: str) -> str:
|
33 |
-
"""Run Python code
|
34 |
try:
|
35 |
-
if
|
36 |
-
code = f"print({code})"
|
37 |
return python_tool.run(code)
|
38 |
except Exception as e:
|
39 |
return f"[PYTHON ERROR] {e}"
|
@@ -47,23 +50,25 @@ def get_youtube_transcript(url: str) -> str:
|
|
47 |
docs = loader.load()
|
48 |
return " ".join(d.page_content for d in docs)
|
49 |
except Exception as e:
|
50 |
-
return f"
|
51 |
|
52 |
# Tool 4 — Whisper transcription
|
53 |
|
54 |
def transcribe_audio(file_path: str) -> str:
|
55 |
-
|
|
|
56 |
try:
|
57 |
model = whisper.load_model("base")
|
58 |
res = model.transcribe(file_path)
|
59 |
return res["text"]
|
60 |
except Exception as e:
|
61 |
-
return f"
|
62 |
|
63 |
-
# Tool 5 — Excel
|
64 |
|
65 |
def extract_excel_total_food_sales(file_path: str) -> str:
|
66 |
-
|
|
|
67 |
try:
|
68 |
wb = openpyxl.load_workbook(file_path)
|
69 |
sheet = wb.active
|
@@ -73,9 +78,9 @@ def extract_excel_total_food_sales(file_path: str) -> str:
|
|
73 |
total += float(amount or 0)
|
74 |
return f"${total:.2f}"
|
75 |
except Exception as e:
|
76 |
-
return f"
|
77 |
|
78 |
-
# Assemble tools
|
79 |
TOOLS = [
|
80 |
FunctionTool.from_defaults(search_wikipedia, return_direct=True, name="search_wikipedia"),
|
81 |
FunctionTool.from_defaults(run_python_code, return_direct=True, name="run_python"),
|
@@ -84,32 +89,25 @@ TOOLS = [
|
|
84 |
FunctionTool.from_defaults(extract_excel_total_food_sales, return_direct=True, name="extract_excel_total_food_sales")
|
85 |
]
|
86 |
|
87 |
-
#
|
88 |
llm = OpenAI(model="gpt-4")
|
89 |
|
90 |
agent = FunctionCallingAgent.from_tools(
|
91 |
tools=TOOLS,
|
92 |
llm=llm,
|
93 |
system_prompt="""
|
94 |
-
You are a highly capable AI agent
|
95 |
-
|
96 |
-
You
|
97 |
-
-
|
98 |
-
-
|
99 |
-
-
|
100 |
-
-
|
101 |
-
-
|
102 |
-
|
103 |
-
Rules:
|
104 |
-
1. Always try to use a tool if relevant.
|
105 |
-
2. Return ONLY the final answer in the requested format.
|
106 |
-
3. Do not guess. If a tool fails, say \"Tool not available\".
|
107 |
-
4. Follow formats strictly: comma-separated lists, numeric values, chess notation, names only, etc.
|
108 |
-
5. Avoid all explanation unless requested.
|
109 |
"""
|
110 |
)
|
111 |
|
112 |
-
# Agent
|
113 |
async def answer_question(question: str) -> str:
|
114 |
try:
|
115 |
response = await agent.achat(question)
|
|
|
1 |
+
# agent.py — zaktualizowany z obsługą błędów, braków plików i bezpiecznym Pythonem
|
2 |
|
3 |
import os
|
4 |
import asyncio
|
|
|
24 |
|
25 |
def search_wikipedia(query: str) -> str:
|
26 |
"""Search Wikipedia for a given query and return relevant summary."""
|
27 |
+
try:
|
28 |
+
return wiki_api.run(query)
|
29 |
+
except Exception as e:
|
30 |
+
return f"Tool not available. ({e})"
|
31 |
|
32 |
+
# Tool 2 — Python safe runner
|
33 |
python_tool = PythonREPLTool()
|
34 |
|
35 |
def run_python_code(code: str) -> str:
|
36 |
+
"""Run Python code safely."""
|
37 |
try:
|
38 |
+
if 'print' not in code:
|
39 |
+
code = f"print({repr(code)})"
|
40 |
return python_tool.run(code)
|
41 |
except Exception as e:
|
42 |
return f"[PYTHON ERROR] {e}"
|
|
|
50 |
docs = loader.load()
|
51 |
return " ".join(d.page_content for d in docs)
|
52 |
except Exception as e:
|
53 |
+
return f"Tool not available. ({e})"
|
54 |
|
55 |
# Tool 4 — Whisper transcription
|
56 |
|
57 |
def transcribe_audio(file_path: str) -> str:
|
58 |
+
if not os.path.exists(file_path):
|
59 |
+
return "Tool not available. (Missing file)"
|
60 |
try:
|
61 |
model = whisper.load_model("base")
|
62 |
res = model.transcribe(file_path)
|
63 |
return res["text"]
|
64 |
except Exception as e:
|
65 |
+
return f"Tool not available. ({e})"
|
66 |
|
67 |
+
# Tool 5 — Excel food sum
|
68 |
|
69 |
def extract_excel_total_food_sales(file_path: str) -> str:
|
70 |
+
if not os.path.exists(file_path):
|
71 |
+
return "Tool not available. (Missing file)"
|
72 |
try:
|
73 |
wb = openpyxl.load_workbook(file_path)
|
74 |
sheet = wb.active
|
|
|
78 |
total += float(amount or 0)
|
79 |
return f"${total:.2f}"
|
80 |
except Exception as e:
|
81 |
+
return f"Tool not available. ({e})"
|
82 |
|
83 |
+
# Assemble tools
|
84 |
TOOLS = [
|
85 |
FunctionTool.from_defaults(search_wikipedia, return_direct=True, name="search_wikipedia"),
|
86 |
FunctionTool.from_defaults(run_python_code, return_direct=True, name="run_python"),
|
|
|
89 |
FunctionTool.from_defaults(extract_excel_total_food_sales, return_direct=True, name="extract_excel_total_food_sales")
|
90 |
]
|
91 |
|
92 |
+
# Build agent
|
93 |
llm = OpenAI(model="gpt-4")
|
94 |
|
95 |
agent = FunctionCallingAgent.from_tools(
|
96 |
tools=TOOLS,
|
97 |
llm=llm,
|
98 |
system_prompt="""
|
99 |
+
You are a highly capable AI agent completing the GAIA benchmark.
|
100 |
+
|
101 |
+
You MUST:
|
102 |
+
- Always use tools when available.
|
103 |
+
- If a tool returns 'None' or fails, try another or say 'Tool not available'.
|
104 |
+
- Return only final answer in strict format (comma-separated, numbers, names, no explanations).
|
105 |
+
- Do not guess. If unsure, state 'Tool not available'.
|
106 |
+
- NEVER return raw tool errors, only result or fallback.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
"""
|
108 |
)
|
109 |
|
110 |
+
# Agent call wrapper
|
111 |
async def answer_question(question: str) -> str:
|
112 |
try:
|
113 |
response = await agent.achat(question)
|