Update agent.py
Browse files
agent.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
-
# agent.py
|
2 |
|
3 |
import asyncio
|
4 |
from llama_index.llms.openai import OpenAI
|
5 |
-
from llama_index.core.agent.react import ReActAgent
|
6 |
from llama_index.core.tools import FunctionTool
|
|
|
7 |
|
8 |
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
|
9 |
from langchain_experimental.tools.python.tool import PythonREPLTool
|
@@ -24,7 +24,7 @@ def get_youtube_transcript(url: str) -> str:
|
|
24 |
docs = loader.load()
|
25 |
return " ".join(doc.page_content for doc in docs)
|
26 |
|
27 |
-
# ---
|
28 |
TOOLS = [
|
29 |
FunctionTool.from_defaults(search_duckduckgo),
|
30 |
FunctionTool.from_defaults(search_wikipedia),
|
@@ -32,14 +32,42 @@ TOOLS = [
|
|
32 |
FunctionTool.from_defaults(get_youtube_transcript),
|
33 |
]
|
34 |
|
35 |
-
# --- LLM ---
|
36 |
llm = OpenAI(model="gpt-4")
|
37 |
|
38 |
-
# ---
|
39 |
-
agent = ReActAgent(
|
40 |
tools=TOOLS,
|
41 |
llm=llm,
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
)
|
44 |
|
45 |
# --- Runner with timeout ---
|
@@ -50,4 +78,4 @@ async def answer_question(question: str) -> str:
|
|
50 |
except asyncio.TimeoutError:
|
51 |
return "[TIMEOUT]"
|
52 |
except Exception as e:
|
53 |
-
return f"[ERROR] {e}"
|
|
|
1 |
+
# agent.py — updated with ReActAgent (new API) and improved system prompt
|
2 |
|
3 |
import asyncio
|
4 |
from llama_index.llms.openai import OpenAI
|
|
|
5 |
from llama_index.core.tools import FunctionTool
|
6 |
+
from llama_index.core.agent.react.base import ReActAgent
|
7 |
|
8 |
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
|
9 |
from langchain_experimental.tools.python.tool import PythonREPLTool
|
|
|
24 |
docs = loader.load()
|
25 |
return " ".join(doc.page_content for doc in docs)
|
26 |
|
27 |
+
# --- Define tools ---
|
28 |
TOOLS = [
|
29 |
FunctionTool.from_defaults(search_duckduckgo),
|
30 |
FunctionTool.from_defaults(search_wikipedia),
|
|
|
32 |
FunctionTool.from_defaults(get_youtube_transcript),
|
33 |
]
|
34 |
|
35 |
+
# --- Load LLM ---
|
36 |
llm = OpenAI(model="gpt-4")
|
37 |
|
38 |
+
# --- Create ReAct Agent (new API) ---
|
39 |
+
agent = ReActAgent.from_tools(
|
40 |
tools=TOOLS,
|
41 |
llm=llm,
|
42 |
+
verbose=False,
|
43 |
+
system_prompt="""
|
44 |
+
You are an expert AI assistant participating in the GAIA benchmark.
|
45 |
+
|
46 |
+
Your task is to answer questions as precisely, concisely, and correctly as possible. Each answer will be evaluated by an automated system that checks both correctness and strict formatting.
|
47 |
+
|
48 |
+
Follow these rules:
|
49 |
+
|
50 |
+
1. Focus only on the final answer — no explanation, no reasoning, no commentary.
|
51 |
+
2. Format your answer exactly as requested:
|
52 |
+
- If the answer must be a list, output a **comma-separated list** in alphabetical order with no extra words.
|
53 |
+
- If the answer is a **chess move**, return it in **algebraic notation** like `Qd1+` or `Rf3#` with no spaces or commentary.
|
54 |
+
- If the answer is a **number**, return **only the number** (e.g. `5`, `33`, `0`), not `five` or `The answer is 5`.
|
55 |
+
- If the answer is a **currency value**, return it as a USD amount formatted like `$10.25`.
|
56 |
+
- If the answer is a **name**, return only the first name or surname as required, with no quotes or additional text.
|
57 |
+
- If the answer must be from **an Excel file or audio or image**, analyze the file or transcript and return only the required value.
|
58 |
+
|
59 |
+
3. If the question is ambiguous, give your best guess using available tools. Do not leave answers blank.
|
60 |
+
|
61 |
+
You have access to tools such as:
|
62 |
+
- internet search (DuckDuckGo)
|
63 |
+
- Wikipedia lookup
|
64 |
+
- Python code interpreter
|
65 |
+
- YouTube transcript parser
|
66 |
+
|
67 |
+
Always choose the tool(s) most relevant to the question.
|
68 |
+
|
69 |
+
Be accurate, be concise, and never guess the format — follow it exactly.
|
70 |
+
"""
|
71 |
)
|
72 |
|
73 |
# --- Runner with timeout ---
|
|
|
78 |
except asyncio.TimeoutError:
|
79 |
return "[TIMEOUT]"
|
80 |
except Exception as e:
|
81 |
+
return f"[ERROR] {e}"
|