Update agent.py
Browse files
agent.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# agent.py —
|
2 |
|
3 |
import asyncio
|
4 |
from llama_index.llms.openai import OpenAI
|
@@ -9,7 +9,7 @@ from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
|
|
9 |
from langchain_experimental.tools.python.tool import PythonREPLTool
|
10 |
from langchain_community.document_loaders import YoutubeLoader
|
11 |
|
12 |
-
#
|
13 |
def search_duckduckgo(query: str) -> str:
|
14 |
return DuckDuckGoSearchRun().run(query)
|
15 |
|
@@ -24,7 +24,6 @@ def get_youtube_transcript(url: str) -> str:
|
|
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,10 +31,8 @@ TOOLS = [
|
|
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,
|
@@ -43,39 +40,22 @@ agent = ReActAgent.from_tools(
|
|
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
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
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 |
-
|
74 |
-
async def answer_question(question: str) -> str:
|
75 |
try:
|
76 |
-
|
77 |
-
return
|
78 |
-
except asyncio.TimeoutError:
|
79 |
-
return "[TIMEOUT]"
|
80 |
except Exception as e:
|
81 |
-
return f"[ERROR] {e}"
|
|
|
|
|
|
|
|
|
|
1 |
+
# agent.py — final version with working ReActAgent
|
2 |
|
3 |
import asyncio
|
4 |
from llama_index.llms.openai import OpenAI
|
|
|
9 |
from langchain_experimental.tools.python.tool import PythonREPLTool
|
10 |
from langchain_community.document_loaders import YoutubeLoader
|
11 |
|
12 |
+
# Tool wrappers
|
13 |
def search_duckduckgo(query: str) -> str:
|
14 |
return DuckDuckGoSearchRun().run(query)
|
15 |
|
|
|
24 |
docs = loader.load()
|
25 |
return " ".join(doc.page_content for doc in docs)
|
26 |
|
|
|
27 |
TOOLS = [
|
28 |
FunctionTool.from_defaults(search_duckduckgo),
|
29 |
FunctionTool.from_defaults(search_wikipedia),
|
|
|
31 |
FunctionTool.from_defaults(get_youtube_transcript),
|
32 |
]
|
33 |
|
|
|
34 |
llm = OpenAI(model="gpt-4")
|
35 |
|
|
|
36 |
agent = ReActAgent.from_tools(
|
37 |
tools=TOOLS,
|
38 |
llm=llm,
|
|
|
40 |
system_prompt="""
|
41 |
You are an expert AI assistant participating in the GAIA benchmark.
|
42 |
|
43 |
+
Your task is to answer questions as precisely and exactly as possible. Each answer is evaluated automatically—strict formatting matters.
|
44 |
|
45 |
+
Rules:
|
46 |
+
1. Output ONLY the final answer—NO explanation or commentary.
|
47 |
+
2. Format exactly as requested (lists, names, numbers, chess moves, currency).
|
48 |
+
3. Use tools as needed—no guessing.
|
49 |
+
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
)
|
51 |
|
52 |
+
def answer_question_sync(question: str) -> str:
|
|
|
53 |
try:
|
54 |
+
response = agent.chat(question)
|
55 |
+
return response.response.content.strip()
|
|
|
|
|
56 |
except Exception as e:
|
57 |
+
return f"[ERROR] {e}"
|
58 |
+
|
59 |
+
async def answer_question(question: str) -> str:
|
60 |
+
# wrap synchronous call in asyncio to keep compatibility
|
61 |
+
return answer_question_sync(question)
|