Update agent.py
Browse files
agent.py
CHANGED
@@ -1,70 +1,66 @@
|
|
1 |
-
# agent.py — full GAIA-ready agent with
|
2 |
|
3 |
import os
|
4 |
import asyncio
|
|
|
5 |
from llama_index.llms.openai import OpenAI
|
6 |
from llama_index.core.agent.react.base import ReActAgent
|
7 |
from llama_index.core.tools import FunctionTool
|
8 |
|
9 |
-
from langchain_community.tools.wikipedia.tool import
|
|
|
10 |
from langchain_experimental.tools.python.tool import PythonREPLTool
|
11 |
from langchain_community.document_loaders import YoutubeLoader
|
12 |
|
13 |
-
import
|
14 |
import openpyxl
|
15 |
|
16 |
-
#
|
17 |
if os.getenv("OPENAI_API_KEY"):
|
18 |
-
print("✅ Detected OPENAI_API_KEY
|
19 |
else:
|
20 |
-
print("⚠️
|
|
|
|
|
21 |
|
22 |
-
|
23 |
def wikipedia_search(query: str) -> str:
|
24 |
-
return
|
25 |
|
26 |
-
# --- Python with output ---
|
27 |
def run_python_with_output(code: str) -> str:
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
return PythonREPLTool().run(code)
|
32 |
-
except Exception as e:
|
33 |
-
return f"[PYTHON ERROR] {e}"
|
34 |
|
35 |
-
# --- YouTube (fallback placeholder) ---
|
36 |
def get_youtube_transcript(url: str) -> str:
|
37 |
try:
|
38 |
loader = YoutubeLoader.from_youtube_url(url, add_video_info=False)
|
39 |
docs = loader.load()
|
40 |
-
return " ".join(
|
41 |
except Exception as e:
|
42 |
-
return
|
43 |
|
44 |
-
# --- Whisper transcription ---
|
45 |
def transcribe_audio(file_path: str) -> str:
|
46 |
try:
|
47 |
model = whisper.load_model("base")
|
48 |
-
|
49 |
-
return
|
50 |
except Exception as e:
|
51 |
-
return
|
52 |
|
53 |
-
# --- Excel sales extraction ---
|
54 |
def extract_excel_total_food_sales(file_path: str) -> str:
|
55 |
try:
|
56 |
wb = openpyxl.load_workbook(file_path)
|
57 |
sheet = wb.active
|
58 |
-
total = 0
|
59 |
-
for
|
60 |
-
category,
|
61 |
-
|
62 |
-
total += float(amount)
|
63 |
return f"${total:.2f}"
|
64 |
except Exception as e:
|
65 |
-
return
|
66 |
|
67 |
-
#
|
68 |
TOOLS = [
|
69 |
FunctionTool.from_defaults(wikipedia_search),
|
70 |
FunctionTool.from_defaults(run_python_with_output),
|
@@ -73,44 +69,31 @@ TOOLS = [
|
|
73 |
FunctionTool.from_defaults(extract_excel_total_food_sales),
|
74 |
]
|
75 |
|
76 |
-
#
|
77 |
llm = OpenAI(model="gpt-4")
|
78 |
-
|
79 |
agent = ReActAgent.from_tools(
|
80 |
tools=TOOLS,
|
81 |
llm=llm,
|
82 |
verbose=True,
|
83 |
system_prompt="""
|
84 |
-
You are an expert AI assistant
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
- Excel analysis
|
92 |
-
|
93 |
-
Rules:
|
94 |
-
1. Output only the FINAL answer. No explanations.
|
95 |
-
2. Format must match expected output exactly: comma-separated lists, plain names, numeric values, algebraic notation.
|
96 |
-
3. Use tools smartly. Don't guess when tools can help.
|
97 |
-
4. If tools fail (e.g., YouTube blocked), say clearly: "Tool not available".
|
98 |
-
"""
|
99 |
)
|
100 |
|
101 |
-
# --- Run function ---
|
102 |
def answer_question_sync(question: str) -> str:
|
103 |
try:
|
104 |
-
|
105 |
-
if hasattr(
|
106 |
-
return
|
107 |
-
|
108 |
-
return response.strip()
|
109 |
-
else:
|
110 |
-
return str(response)
|
111 |
except Exception as e:
|
112 |
-
print(
|
113 |
-
return
|
114 |
|
115 |
async def answer_question(question: str) -> str:
|
116 |
return answer_question_sync(question)
|
|
|
1 |
+
# agent.py — full GAIA-ready agent with working WikipediaQueryRun + tools
|
2 |
|
3 |
import os
|
4 |
import asyncio
|
5 |
+
|
6 |
from llama_index.llms.openai import OpenAI
|
7 |
from llama_index.core.agent.react.base import ReActAgent
|
8 |
from llama_index.core.tools import FunctionTool
|
9 |
|
10 |
+
from langchain_community.tools.wikipedia.tool import WikipediaQueryRun
|
11 |
+
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
12 |
from langchain_experimental.tools.python.tool import PythonREPLTool
|
13 |
from langchain_community.document_loaders import YoutubeLoader
|
14 |
|
15 |
+
import whisper
|
16 |
import openpyxl
|
17 |
|
18 |
+
# Check OpenAI key
|
19 |
if os.getenv("OPENAI_API_KEY"):
|
20 |
+
print("✅ Detected OPENAI_API_KEY")
|
21 |
else:
|
22 |
+
print("⚠️ Missing OPENAI_API_KEY – LLM may fail")
|
23 |
+
|
24 |
+
# Tools definitions
|
25 |
|
26 |
+
api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=1000)
|
27 |
def wikipedia_search(query: str) -> str:
|
28 |
+
return WikipediaQueryRun(api_wrapper=api_wrapper).run({"query": query})
|
29 |
|
|
|
30 |
def run_python_with_output(code: str) -> str:
|
31 |
+
if "print(" not in code:
|
32 |
+
code = f"print({code})"
|
33 |
+
return PythonREPLTool().run(code)
|
|
|
|
|
|
|
34 |
|
|
|
35 |
def get_youtube_transcript(url: str) -> str:
|
36 |
try:
|
37 |
loader = YoutubeLoader.from_youtube_url(url, add_video_info=False)
|
38 |
docs = loader.load()
|
39 |
+
return " ".join(d.page_content for d in docs)
|
40 |
except Exception as e:
|
41 |
+
return "[YOUTUBE ERROR] " + str(e)
|
42 |
|
|
|
43 |
def transcribe_audio(file_path: str) -> str:
|
44 |
try:
|
45 |
model = whisper.load_model("base")
|
46 |
+
res = model.transcribe(file_path)
|
47 |
+
return res["text"]
|
48 |
except Exception as e:
|
49 |
+
return "[AUDIO ERROR] " + str(e)
|
50 |
|
|
|
51 |
def extract_excel_total_food_sales(file_path: str) -> str:
|
52 |
try:
|
53 |
wb = openpyxl.load_workbook(file_path)
|
54 |
sheet = wb.active
|
55 |
+
total = 0.0
|
56 |
+
for _, category, amount in sheet.iter_rows(min_row=2, values_only=True):
|
57 |
+
if isinstance(category, str) and "food" in category.lower():
|
58 |
+
total += float(amount or 0)
|
|
|
59 |
return f"${total:.2f}"
|
60 |
except Exception as e:
|
61 |
+
return "[EXCEL ERROR] " + str(e)
|
62 |
|
63 |
+
# Assemble tools
|
64 |
TOOLS = [
|
65 |
FunctionTool.from_defaults(wikipedia_search),
|
66 |
FunctionTool.from_defaults(run_python_with_output),
|
|
|
69 |
FunctionTool.from_defaults(extract_excel_total_food_sales),
|
70 |
]
|
71 |
|
72 |
+
# LLM and Agent
|
73 |
llm = OpenAI(model="gpt-4")
|
|
|
74 |
agent = ReActAgent.from_tools(
|
75 |
tools=TOOLS,
|
76 |
llm=llm,
|
77 |
verbose=True,
|
78 |
system_prompt="""
|
79 |
+
You are an expert AI assistant on the GAIA benchmark.
|
80 |
+
|
81 |
+
Use available tools (Wikipedia, Python, YouTube transcript, audio, Excel).
|
82 |
+
Output ONLY the final answer. No reasoning or commentary.
|
83 |
+
Format exactly as requested (list, number, name, chess move, currency).
|
84 |
+
If tool fails, output "Tool not available".
|
85 |
+
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
)
|
87 |
|
|
|
88 |
def answer_question_sync(question: str) -> str:
|
89 |
try:
|
90 |
+
resp = agent.chat(question)
|
91 |
+
if hasattr(resp, "response") and hasattr(resp.response, "content"):
|
92 |
+
return resp.response.content.strip()
|
93 |
+
return str(resp).strip()
|
|
|
|
|
|
|
94 |
except Exception as e:
|
95 |
+
print("❌ Agent exception:", e)
|
96 |
+
return "[ERROR] " + str(e)
|
97 |
|
98 |
async def answer_question(question: str) -> str:
|
99 |
return answer_question_sync(question)
|