Update agent.py
Browse files
agent.py
CHANGED
@@ -1,15 +1,14 @@
|
|
1 |
-
# agent.py β
|
2 |
|
3 |
import os
|
4 |
import asyncio
|
5 |
from llama_index.llms.openai import OpenAI
|
6 |
-
from llama_index.core.agent
|
7 |
from llama_index.core.tools import FunctionTool
|
8 |
|
9 |
-
from langchain_community.tools.wikipedia.tool import WikipediaQueryRun
|
10 |
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
11 |
-
from langchain_experimental.tools.python.tool import PythonREPLTool
|
12 |
from langchain_community.document_loaders import YoutubeLoader
|
|
|
13 |
|
14 |
import whisper
|
15 |
import openpyxl
|
@@ -20,34 +19,51 @@ if os.getenv("OPENAI_API_KEY"):
|
|
20 |
else:
|
21 |
print("β οΈ Missing OPENAI_API_KEY β LLM may fail")
|
22 |
|
23 |
-
#
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
if "print(" not in code:
|
31 |
-
code = f"print({code})"
|
32 |
-
return PythonREPLTool().run(code)
|
33 |
|
34 |
def get_youtube_transcript(url: str) -> str:
|
|
|
35 |
try:
|
36 |
loader = YoutubeLoader.from_youtube_url(url, add_video_info=False)
|
37 |
docs = loader.load()
|
38 |
return " ".join(d.page_content for d in docs)
|
39 |
except Exception as e:
|
40 |
-
return "[YOUTUBE ERROR] "
|
|
|
|
|
41 |
|
42 |
def transcribe_audio(file_path: str) -> str:
|
|
|
43 |
try:
|
44 |
model = whisper.load_model("base")
|
45 |
res = model.transcribe(file_path)
|
46 |
return res["text"]
|
47 |
except Exception as e:
|
48 |
-
return "[AUDIO ERROR] "
|
|
|
|
|
49 |
|
50 |
def extract_excel_total_food_sales(file_path: str) -> str:
|
|
|
51 |
try:
|
52 |
wb = openpyxl.load_workbook(file_path)
|
53 |
sheet = wb.active
|
@@ -57,31 +73,41 @@ def extract_excel_total_food_sales(file_path: str) -> str:
|
|
57 |
total += float(amount or 0)
|
58 |
return f"${total:.2f}"
|
59 |
except Exception as e:
|
60 |
-
return "[EXCEL ERROR] "
|
61 |
|
62 |
-
# Assemble tools
|
63 |
TOOLS = [
|
64 |
-
FunctionTool.from_defaults(
|
65 |
-
FunctionTool.from_defaults(
|
66 |
-
FunctionTool.from_defaults(get_youtube_transcript),
|
67 |
-
FunctionTool.from_defaults(transcribe_audio),
|
68 |
-
FunctionTool.from_defaults(extract_excel_total_food_sales)
|
69 |
]
|
70 |
|
71 |
-
#
|
72 |
llm = OpenAI(model="gpt-4")
|
73 |
-
|
|
|
74 |
tools=TOOLS,
|
75 |
llm=llm,
|
76 |
verbose=True,
|
77 |
system_prompt="""
|
78 |
-
You are
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
If tool fails,
|
84 |
-
|
|
|
|
|
85 |
)
|
86 |
|
87 |
def answer_question_sync(question: str) -> str:
|
|
|
1 |
+
# agent.py β GAIA-ready with FunctionCallingAgent and improved tools
|
2 |
|
3 |
import os
|
4 |
import asyncio
|
5 |
from llama_index.llms.openai import OpenAI
|
6 |
+
from llama_index.core.agent import FunctionCallingAgent
|
7 |
from llama_index.core.tools import FunctionTool
|
8 |
|
|
|
9 |
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
|
|
10 |
from langchain_community.document_loaders import YoutubeLoader
|
11 |
+
from langchain_experimental.tools.python.tool import PythonREPLTool
|
12 |
|
13 |
import whisper
|
14 |
import openpyxl
|
|
|
19 |
else:
|
20 |
print("β οΈ Missing OPENAI_API_KEY β LLM may fail")
|
21 |
|
22 |
+
# Tool 1 β Wikipedia
|
23 |
+
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 |
+
return wiki_api.run(query)
|
28 |
|
29 |
+
# Tool 2 β Python with output
|
30 |
+
python_tool = PythonREPLTool()
|
31 |
+
|
32 |
+
def run_python_code(code: str) -> str:
|
33 |
+
"""Run Python code and return printed result."""
|
34 |
+
try:
|
35 |
+
if "print(" not in code:
|
36 |
+
code = f"print({code})"
|
37 |
+
return python_tool.run(code)
|
38 |
+
except Exception as e:
|
39 |
+
return f"[PYTHON ERROR] {e}"
|
40 |
|
41 |
+
# Tool 3 β YouTube transcript
|
|
|
|
|
|
|
42 |
|
43 |
def get_youtube_transcript(url: str) -> str:
|
44 |
+
"""Get transcript from YouTube video."""
|
45 |
try:
|
46 |
loader = YoutubeLoader.from_youtube_url(url, add_video_info=False)
|
47 |
docs = loader.load()
|
48 |
return " ".join(d.page_content for d in docs)
|
49 |
except Exception as e:
|
50 |
+
return f"[YOUTUBE ERROR] {e}"
|
51 |
+
|
52 |
+
# Tool 4 β Whisper transcription
|
53 |
|
54 |
def transcribe_audio(file_path: str) -> str:
|
55 |
+
"""Transcribe an MP3 file to text using Whisper."""
|
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"[AUDIO ERROR] {e}"
|
62 |
+
|
63 |
+
# Tool 5 β Excel parser
|
64 |
|
65 |
def extract_excel_total_food_sales(file_path: str) -> str:
|
66 |
+
"""Sum sales from Excel where category is 'food'."""
|
67 |
try:
|
68 |
wb = openpyxl.load_workbook(file_path)
|
69 |
sheet = wb.active
|
|
|
73 |
total += float(amount or 0)
|
74 |
return f"${total:.2f}"
|
75 |
except Exception as e:
|
76 |
+
return f"[EXCEL ERROR] {e}"
|
77 |
|
78 |
+
# Assemble tools with proper descriptions
|
79 |
TOOLS = [
|
80 |
+
FunctionTool.from_defaults(search_wikipedia, name="search_wikipedia", description="Search Wikipedia for facts and lists."),
|
81 |
+
FunctionTool.from_defaults(run_python_code, name="run_python", description="Run Python code for logic, math, or set processing."),
|
82 |
+
FunctionTool.from_defaults(get_youtube_transcript, name="get_youtube_transcript", description="Fetch transcript from YouTube video by URL."),
|
83 |
+
FunctionTool.from_defaults(transcribe_audio, name="transcribe_audio", description="Transcribe MP3 audio file using Whisper."),
|
84 |
+
FunctionTool.from_defaults(extract_excel_total_food_sales, name="extract_excel_total_food_sales", description="Sum total sales from Excel where category is 'food'.")
|
85 |
]
|
86 |
|
87 |
+
# Create agent with improved system prompt
|
88 |
llm = OpenAI(model="gpt-4")
|
89 |
+
|
90 |
+
agent = FunctionCallingAgent.from_tools(
|
91 |
tools=TOOLS,
|
92 |
llm=llm,
|
93 |
verbose=True,
|
94 |
system_prompt="""
|
95 |
+
You are a highly capable AI agent taking the GAIA benchmark test.
|
96 |
+
|
97 |
+
You have access to the following tools:
|
98 |
+
- Wikipedia search for factual lookups
|
99 |
+
- Python runner for math, logic, or text analysis
|
100 |
+
- YouTube transcript fetcher (via URL)
|
101 |
+
- Audio transcriber (Whisper, MP3)
|
102 |
+
- Excel food sales analyzer
|
103 |
|
104 |
+
Rules:
|
105 |
+
1. Always try to use a tool if relevant.
|
106 |
+
2. Return ONLY the final answer in the requested format.
|
107 |
+
3. Do not guess. If a tool fails, say "Tool not available".
|
108 |
+
4. Follow formats strictly: comma-separated lists, numeric values, chess notation, names only, etc.
|
109 |
+
5. Avoid all explanation unless requested.
|
110 |
+
"""
|
111 |
)
|
112 |
|
113 |
def answer_question_sync(question: str) -> str:
|