Spaces:
Sleeping
Sleeping
please_work
Browse files
agent.py
CHANGED
@@ -1,116 +1,54 @@
|
|
1 |
-
from __future__ import annotations
|
2 |
-
import os
|
3 |
from langchain_openai import ChatOpenAI
|
4 |
-
from
|
5 |
-
from
|
6 |
-
import json
|
7 |
-
|
8 |
-
# βββββββββββββββββββββββββββ External tools ββββββββββββββββββββββββββββββ
|
9 |
from tools import (
|
10 |
-
wikipedia_search_tool,
|
11 |
-
|
12 |
-
|
13 |
-
excel_tool,
|
14 |
-
analyze_code_tool,
|
15 |
-
image_tool,
|
16 |
-
add_tool,
|
17 |
-
subtract_tool,
|
18 |
-
multiply_tool,
|
19 |
-
divide_tool
|
20 |
)
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
"""Simple tool executor that maps tool names to functions."""
|
27 |
-
|
28 |
-
def __init__(self, tools: List):
|
29 |
-
self.tools_map = {tool.name: tool for tool in tools}
|
30 |
-
|
31 |
-
def run(self, tool_name: str, tool_input: str) -> str:
|
32 |
-
"""Execute a tool with the given input."""
|
33 |
-
if tool_name not in self.tools_map:
|
34 |
-
return f"Tool '{tool_name}' not found. Available tools: {list(self.tools_map.keys())}"
|
35 |
-
|
36 |
-
tool = self.tools_map[tool_name]
|
37 |
-
try:
|
38 |
-
# Handle different input formats
|
39 |
-
if tool_name in ['add_tool', 'subtract_tool', 'multiply_tool', 'divide_tool']:
|
40 |
-
# Math tools expect two numbers
|
41 |
-
# Try to parse the input as two numbers
|
42 |
-
import re
|
43 |
-
numbers = re.findall(r'-?\d+(?:\.\d+)?', tool_input)
|
44 |
-
if len(numbers) >= 2:
|
45 |
-
a, b = float(numbers[0]), float(numbers[1])
|
46 |
-
return tool.run(a=a, b=b)
|
47 |
-
else:
|
48 |
-
return f"Math tool requires two numbers. Got: {tool_input}"
|
49 |
-
else:
|
50 |
-
# Other tools expect a string input
|
51 |
-
# Clean the input - remove quotes if present
|
52 |
-
clean_input = tool_input.strip().strip('"').strip("'")
|
53 |
-
if "search" in tool_name:
|
54 |
-
return tool.run(clean_input)
|
55 |
-
elif tool_name in ['audio_transcriber_tool', 'excel_tool', 'analyze_code_tool', 'image_tool']:
|
56 |
-
return tool.run(task_id=clean_input)
|
57 |
-
else:
|
58 |
-
return tool.run(clean_input)
|
59 |
-
except Exception as e:
|
60 |
-
return f"Error executing {tool_name}: {str(e)}"
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
self.llm = ChatOpenAI(model_name=model_name, temperature=0.3)
|
65 |
-
self.tool_executor = SimpleToolExecutor(tools)
|
66 |
|
67 |
-
|
68 |
-
messages = [SystemMessage(content=system_prompt)]
|
69 |
-
if task_id:
|
70 |
-
messages[0].content += f"\n\nIMPORTANT: Your current task_id is: {task_id}."
|
71 |
-
messages.append(HumanMessage(content=question))
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
messages.append(ai_response)
|
76 |
-
print("AI said:", ai_response.content)
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
94 |
|
95 |
-
|
|
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
llm_tools = [
|
101 |
-
wikipedia_search_tool,
|
102 |
-
arxiv_search_tool,
|
103 |
-
audio_transcriber_tool,
|
104 |
-
excel_tool,
|
105 |
-
analyze_code_tool,
|
106 |
-
image_tool,
|
107 |
-
add_tool,
|
108 |
-
subtract_tool,
|
109 |
-
multiply_tool,
|
110 |
-
divide_tool
|
111 |
-
]
|
112 |
-
|
113 |
-
# Create the custom react agent
|
114 |
-
agent = CustomReActAgent(tools=llm_tools)
|
115 |
|
116 |
-
|
|
|
|
|
|
|
1 |
from langchain_openai import ChatOpenAI
|
2 |
+
from langchain.schema import SystemMessage, HumanMessage
|
3 |
+
from langgraph.prebuilt import create_react_agent
|
|
|
|
|
|
|
4 |
from tools import (
|
5 |
+
wikipedia_search_tool, arxiv_search_tool,
|
6 |
+
audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool,
|
7 |
+
add_tool, subtract_tool, multiply_tool, divide_tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
)
|
9 |
|
10 |
+
# ββββββββββββββββββββββββββββββββ Config ββββββββββββββββββββββββββββββββ
|
11 |
+
SYSTEM_PROMPT = """
|
12 |
+
You are a smart AI assistant using a tool-augmented reasoning strategy. Follow this loop:
|
13 |
+
Thought: ...
|
14 |
+
Action: ...
|
15 |
+
Action Input: ...
|
16 |
|
17 |
+
You will get an Observation, and then continue reasoning.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
Only end the loop with:
|
20 |
+
FINAL ANSWER: [your short final answer here].
|
|
|
|
|
21 |
|
22 |
+
DO NOT answer directly without using this loop at least once, unless the answer is trivially obvious.
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
When using task-based tools (audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool), ONLY use the 'task_id' value.
|
25 |
+
"""
|
|
|
|
|
26 |
|
27 |
+
TOOLS = [
|
28 |
+
wikipedia_search_tool, arxiv_search_tool,
|
29 |
+
audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool,
|
30 |
+
add_tool, subtract_tool, multiply_tool, divide_tool
|
31 |
+
]
|
32 |
|
33 |
+
# βββββββββββββββββββββββββββββ Agent Wrapper βββββββββββββββββββββββββββββ
|
34 |
+
class SimpleLangGraphAgent:
|
35 |
+
def __init__(self, model_name="gpt-4o-mini"):
|
36 |
+
self.agent = create_react_agent(
|
37 |
+
model=ChatOpenAI(model_name=model_name, temperature=0),
|
38 |
+
tools=TOOLS,
|
39 |
+
)
|
40 |
+
|
41 |
+
def run(self, question: str, task_id: str = None, max_steps: int = 10) -> str:
|
42 |
+
messages = [SystemMessage(content=SYSTEM_PROMPT)]
|
43 |
+
if task_id:
|
44 |
+
messages[0].content += f"\n\nYour task_id is: {task_id}. Use it only with the tools that require it."
|
45 |
+
messages.append(HumanMessage(content=question))
|
46 |
|
47 |
+
state = {"messages": messages}
|
48 |
+
final_state = self.agent.invoke(state, config={"recursion_limit": max_steps})
|
49 |
|
50 |
+
for msg in final_state["messages"][::-1]:
|
51 |
+
if "FINAL ANSWER:" in msg.content.upper():
|
52 |
+
return msg.content.split("FINAL ANSWER:")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
return "No FINAL ANSWER found."
|
app.py
CHANGED
@@ -7,7 +7,7 @@ from langchain.schema import HumanMessage, SystemMessage
|
|
7 |
from typing import Optional
|
8 |
import re
|
9 |
|
10 |
-
from agent import
|
11 |
from state import AgentState
|
12 |
|
13 |
# --- Constants ---
|
@@ -51,24 +51,16 @@ IMPORTANT:
|
|
51 |
class BasicAgent:
|
52 |
def __init__(self):
|
53 |
print("BasicAgent initialized.")
|
54 |
-
|
55 |
|
56 |
def __call__(self, question: str, task_id: Optional[str] = None) -> str:
|
57 |
"""Run the agent and return whatever FINAL_ANSWER the agent produces."""
|
58 |
print(f"Agent received question: {question}")
|
59 |
-
|
60 |
-
try:
|
61 |
-
# Run the custom react agent
|
62 |
-
result = self.graph.run(question=question, task_id=task_id, max_turns=15, system_prompt=SYSTEM_PROMPT)
|
63 |
-
print("Final result: ", result)
|
64 |
-
print("\n\n\n\n")
|
65 |
-
return result
|
66 |
-
|
67 |
-
except Exception as e:
|
68 |
-
print(f"Agent execution error: {e}")
|
69 |
-
return f"Unable to process the question due to an error. Please try again."
|
70 |
-
|
71 |
|
|
|
|
|
|
|
72 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
73 |
"""
|
74 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
7 |
from typing import Optional
|
8 |
import re
|
9 |
|
10 |
+
from agent import SimpleLangGraphAgent
|
11 |
from state import AgentState
|
12 |
|
13 |
# --- Constants ---
|
|
|
51 |
class BasicAgent:
|
52 |
def __init__(self):
|
53 |
print("BasicAgent initialized.")
|
54 |
+
|
55 |
|
56 |
def __call__(self, question: str, task_id: Optional[str] = None) -> str:
|
57 |
"""Run the agent and return whatever FINAL_ANSWER the agent produces."""
|
58 |
print(f"Agent received question: {question}")
|
59 |
+
print("\n\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
agent = SimpleLangGraphAgent()
|
62 |
+
return agent.run(question, task_id)
|
63 |
+
|
64 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
65 |
"""
|
66 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|