Files
MultiAgent/agent/executor.py

37 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# executor.py
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_ollama import ChatOllama
from langchain_core.prompts import ChatPromptTemplate
class ToolExecutor:
def __init__(self, tools):
self.tools = tools
self.llm = llm or ChatOllama(
model=MODEL_NAME,
temperature=0.0
)
self.agent = self._create_agent()
def _create_agent(self):
prompt = ChatPromptTemplate.from_messages([
("system", "Ты - исполнитель. У тебя есть доступ к инструментам. Выполняй шаги плана последовательно."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}")
])
agent = create_tool_calling_agent(self.llm, self.tools, prompt)
return AgentExecutor(agent=agent, tools=self.tools, verbose=True)
def execute_plan(self, plan: List[Dict]) -> List[Dict]:
results = []
for step in plan:
tool_name = step["tool"]
args = step["args"]
# Превращаем шаг в текстовую команду для агента
command = f"Вызови инструмент {tool_name} с аргументами {args}"
result = self.agent.invoke({"input": command})
results.append({
"step": step,
"output": result["output"],
"success": True # можно анализировать ошибки
})
return results