File size: 2,004 Bytes
e103f3d
59ff18d
5db119a
92b0d1a
59ff18d
2fcd193
92b0d1a
59ff18d
 
 
 
e103f3d
59ff18d
 
 
 
 
 
 
 
 
 
 
 
6c7c70b
59ff18d
 
 
 
 
 
 
 
92b0d1a
6c7c70b
2fcd193
5db119a
59ff18d
2fcd193
 
 
 
e103f3d
2fcd193
e103f3d
 
 
 
 
59ff18d
 
e103f3d
5db119a
e103f3d
 
5db119a
e103f3d
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# agent.py — final version with working ReActAgent

import asyncio
from llama_index.llms.openai import OpenAI
from llama_index.core.tools import FunctionTool
from llama_index.core.agent.react.base import ReActAgent

from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
from langchain_experimental.tools.python.tool import PythonREPLTool
from langchain_community.document_loaders import YoutubeLoader

# Tool wrappers
def search_duckduckgo(query: str) -> str:
    return DuckDuckGoSearchRun().run(query)

def search_wikipedia(query: str) -> str:
    return WikipediaQueryRun(api_wrapper=None).run(query)

def run_python(code: str) -> str:
    return PythonREPLTool().run(code)

def get_youtube_transcript(url: str) -> str:
    loader = YoutubeLoader.from_youtube_url(url, add_video_info=False)
    docs = loader.load()
    return " ".join(doc.page_content for doc in docs)

TOOLS = [
    FunctionTool.from_defaults(search_duckduckgo),
    FunctionTool.from_defaults(search_wikipedia),
    FunctionTool.from_defaults(run_python),
    FunctionTool.from_defaults(get_youtube_transcript),
]

llm = OpenAI(model="gpt-4")

agent = ReActAgent.from_tools(
    tools=TOOLS,
    llm=llm,
    verbose=False,
    system_prompt="""
You are an expert AI assistant participating in the GAIA benchmark.

Your task is to answer questions as precisely and exactly as possible. Each answer is evaluated automatically—strict formatting matters.

Rules:
1. Output ONLY the final answer—NO explanation or commentary.
2. Format exactly as requested (lists, names, numbers, chess moves, currency).
3. Use tools as needed—no guessing.
""",
)

def answer_question_sync(question: str) -> str:
    try:
        response = agent.chat(question)
        return response.response.content.strip()
    except Exception as e:
        return f"[ERROR] {e}"

async def answer_question(question: str) -> str:
    # wrap synchronous call in asyncio to keep compatibility
    return answer_question_sync(question)