File size: 2,788 Bytes
d3b49b4
24639c3
 
d3b49b4
24639c3
 
e5782f0
d3b49b4
 
24639c3
 
8ae792e
6c9ca3f
8ae792e
aec3e3b
8ae792e
 
 
d3b49b4
6bdf8ce
8ae792e
 
 
 
 
 
 
 
6bdf8ce
760f1a8
9ea2377
2fb2b9c
24639c3
d3b49b4
24639c3
 
 
e5782f0
24639c3
d3b49b4
24639c3
 
 
 
8ae792e
24639c3
 
 
 
 
 
 
 
d3b49b4
24639c3
9ea2377
d3b49b4
24639c3
 
9ea2377
24639c3
14fa0cc
24639c3
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
from langchain_openai import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage
from langgraph.prebuilt import create_react_agent
from tools import (
    wikipedia_search_tool, arxiv_search_tool,
    audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool,
    add_tool, subtract_tool, multiply_tool, divide_tool, web_search_tool
)

# ──────────────────────────────── Config ────────────────────────────────
SYSTEM_PROMPT = """
You are a helpful AI assistant. You will answer questions by thinking step-by-step, taking actions using tools when necessary, and finishing with a final answer.

When you want to use a tool, respond *exactly* in the following format:

Thought: [your reasoning]
Action: [tool_name]
Action Input: [input]

When you receive an observation, continue reasoning. Write:

Thought: [final reasoning]
FINAL ANSWER: [your answer]

IMPORTANT:
- If using tools that require a `task_id`, only use the value provided.
- Do not make up tool names.
- Do not loop unnecessarily.
- Provide FINAL ANSWER fast.
- Try to use the web search tool first before using wiki and arxiv  tools.
- If wiki tool doesnt give you enough information, try again with a shorter query, or a more genral query. Dont use a query that is too specific.
- DOnt user wiki tool more than 3 times.
"""

TOOLS = [
    wikipedia_search_tool, arxiv_search_tool,
    audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool,
    add_tool, subtract_tool, multiply_tool, divide_tool, web_search_tool
]

# ───────────────────────────── Agent Wrapper ─────────────────────────────
class SimpleLangGraphAgent:
    def __init__(self, model_name="gpt-4o-mini"):
        self.agent = create_react_agent(
            model=ChatOpenAI(model_name=model_name, temperature=0.3),
            tools=TOOLS,
        )

    def run(self, question: str, task_id: str = None, max_steps: int = 10) -> str:
        messages = [SystemMessage(content=SYSTEM_PROMPT)]
        if task_id:
            messages[0].content += f"\n\nYour task_id is: {task_id}. Use it only with the tools that require it."
        messages.append(HumanMessage(content=question))

        state = {"messages": messages}
        final_state = self.agent.invoke(state)

        for msg in final_state["messages"][::-1]:
            if "FINAL ANSWER:" in msg.content.upper():
                print(f"\n\n thoughts = {msg.content.split('Thought:')[-1].strip()} \n\n")
                return msg.content.split("FINAL ANSWER:")[-1].strip()

        return "No FINAL ANSWER found."