Переделываю систему под агента-планировщика
This commit is contained in:
37
agent/executor.py
Normal file
37
agent/executor.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# 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
|
||||
Reference in New Issue
Block a user