naman1102 commited on
Commit
24639c3
Β·
1 Parent(s): 1a8a299

please_work

Browse files
Files changed (2) hide show
  1. agent.py +41 -103
  2. app.py +6 -14
agent.py CHANGED
@@ -1,116 +1,54 @@
1
- from __future__ import annotations
2
- import os
3
  from langchain_openai import ChatOpenAI
4
- from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
5
- from typing import Any, Dict, List, Optional
6
- import json
7
-
8
- # ─────────────────────────── External tools ──────────────────────────────
9
  from tools import (
10
- wikipedia_search_tool,
11
- arxiv_search_tool,
12
- audio_transcriber_tool,
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
- # ─────────────────────────── Configuration ───────────────────────────────
23
- MAX_TOOL_CALLS = 5
 
 
 
 
24
 
25
- class SimpleToolExecutor:
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
- class CustomReActAgent:
63
- def __init__(self, tools: List, model_name="gpt-4o-mini"):
64
- self.llm = ChatOpenAI(model_name=model_name, temperature=0.3)
65
- self.tool_executor = SimpleToolExecutor(tools)
66
 
67
- def run(self, question: str, task_id: Optional[str] = None, max_turns: int = 15, system_prompt: str = "") -> str:
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
- for _ in range(max_turns):
74
- ai_response = self.llm.invoke(messages)
75
- messages.append(ai_response)
76
- print("AI said:", ai_response.content)
77
 
78
- # Final answer?
79
- if "FINAL ANSWER:" in ai_response.content.upper():
80
- return ai_response.content.split("FINAL ANSWER:")[-1].strip()
 
 
81
 
82
- # Tool call expected
83
- if "Action:" in ai_response.content and "Action Input:" in ai_response.content:
84
- try:
85
- tool_name = ai_response.content.split("Action:")[1].split("Action Input:")[0].strip()
86
- tool_input = ai_response.content.split("Action Input:")[1].strip()
87
- tool_result = self.tool_executor.run(tool_name, tool_input)
88
- messages.append(HumanMessage(content=f"Observation: {tool_result}"))
89
- except Exception as e:
90
- messages.append(HumanMessage(content=f"Observation: Tool execution failed: {str(e)}"))
91
- else:
92
- # Instead of adding a confusing "observation"
93
- messages.append(HumanMessage(content="Observation: No action detected. If you are done, write FINAL ANSWER."))
 
94
 
95
- return "No FINAL ANSWER found within allowed steps."
 
96
 
97
- def build_graph():
98
- """Build and return a CustomReActAgent."""
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
- return agent
 
 
 
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 build_graph
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
- self.graph = build_graph()
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,