Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -2,7 +2,6 @@ from __future__ import annotations
|
|
| 2 |
import os
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
| 5 |
-
from langgraph.prebuilt import ToolExecutor
|
| 6 |
from typing import Any, Dict, List, Optional
|
| 7 |
import json
|
| 8 |
|
|
@@ -23,10 +22,47 @@ from tools import (
|
|
| 23 |
# βββββββββββββββββββββββββββ Configuration βββββββββββββββββββββββββββββββ
|
| 24 |
MAX_TOOL_CALLS = 5
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
class CustomReActAgent:
|
| 27 |
def __init__(self, tools: List, model_name="gpt-4o-mini"):
|
| 28 |
self.llm = ChatOpenAI(model_name=model_name, temperature=0.3)
|
| 29 |
-
self.tool_executor =
|
| 30 |
|
| 31 |
def run(self, question: str, task_id: Optional[str] = None, max_turns: int = 15, system_prompt: str = "") -> str:
|
| 32 |
messages = [SystemMessage(content=system_prompt)]
|
|
|
|
| 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 |
|
|
|
|
| 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)]
|