File size: 3,581 Bytes
d360421
 
e1ecc57
 
 
 
 
9b01eac
e1ecc57
 
 
 
9b01eac
e1ecc57
d360421
 
 
 
 
e1ecc57
 
 
 
 
 
 
 
 
 
 
 
9b01eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11ac4ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b01eac
e1ecc57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11ac4ee
e1ecc57
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import AnyMessage, HumanMessage
from langchain_core.runnables import RunnableConfig
from langgraph.prebuilt import create_react_agent
from langgraph.prebuilt.chat_agent_executor import AgentState
from smolagents import VisitWebpageTool, PythonInterpreterTool, SpeechToTextTool
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_experimental.utilities import PythonREPL
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_core.tools import tool

load_dotenv()

BASE_URL = os.getenv("BASE_URL")
OKTA_ACCESS_TOKEN = os.getenv("OKTA_ACCESS_TOKEN")
SUBSCRIPTION_KEY = os.getenv("SUBSCRIPTION_KEY")

chat = ChatOpenAI(
    api_key=OKTA_ACCESS_TOKEN,
    base_url=BASE_URL,
    model="GPT_4_1",
    default_headers={"Subscription-Key": SUBSCRIPTION_KEY},
)

duckduckgo = DuckDuckGoSearchRun()
python_repl = PythonREPL()
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())

# Instantiate smolagents tools
visit_webpage_tool = VisitWebpageTool()
python_interpreter_tool = PythonInterpreterTool()
speech_to_text_tool = SpeechToTextTool()

@tool
def visit_webpage(url: str) -> str:
    """
    Visit a webpage and return its content as a string.
    Args:
        url (str): The URL of the webpage to visit.
    Returns:
        str: The content of the webpage.
    """
    return visit_webpage_tool(url=url)

@tool
def python_interpreter(code: str) -> str:
    """
    Execute Python code and return the output as a string.
    Args:
        code (str): The Python code to execute.
    Returns:
        str: The output of the executed code.
    """
    return python_interpreter_tool(code=code)

@tool
def speech_to_text(audio_path: str) -> str:
    """
    Convert speech in an audio file to text.
    Args:
        audio_path (str): The path to the audio file.
    Returns:
        str: The transcribed text from the audio.
    """
    return speech_to_text_tool(audio_path=audio_path)

@tool
def read_file_contents(file_path: str) -> str:
    """
    Read and return the contents of a file as a string.
    Args:
        file_path (str): The path to the file to read.
    Returns:
        str: The contents of the file, or an error message if the file cannot be read.
    """
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            return f.read()
    except Exception as e:
        return f"Error reading file: {e}"


def prompt(state: AgentState, config: RunnableConfig) -> list[AnyMessage]:
    file_path = config["configurable"].get("file_path", "")
    system_msg = (
        f"You are an AI assistant evaluated on the GAIA benchmark. "
        f"Answer questions using only verified information. "
        f"Use the available tools to find answers when needed. "
        f"If you do not have enough information, reply: 'I do not have enough information to answer the question.' "
        f"Provide only the direct answer, with no extra explanation, formatting, or restating the question. "
        f"For example, if asked 'What is the capital of France?', answer 'Paris'. "
    )
    if file_path:
        system_msg += f"If a file is referenced, use the file path: {file_path}."
    return [{"role": "system", "content": system_msg}] + state["messages"]

react_agent = create_react_agent(
    model=chat,
    tools=[duckduckgo, wikipedia, visit_webpage, python_interpreter, speech_to_text, read_file_contents],
    prompt=prompt,
)