50 lines
2.3 KiB
Python
50 lines
2.3 KiB
Python
# executor.py
|
||
from langchain_classic.agents import AgentExecutor
|
||
from langchain.agents import create_agent
|
||
from langchain_ollama import ChatOllama
|
||
from langchain_core.prompts import ChatPromptTemplate
|
||
from config import LLM_HOST, MODEL_NAME
|
||
|
||
from typing import List, Dict
|
||
|
||
class ExecutorAgent:
|
||
def __init__(self, tools):
|
||
self.tools = tools
|
||
self.llm = ChatOllama(
|
||
model=MODEL_NAME,
|
||
temperature=0.0,
|
||
base_url=LLM_HOST
|
||
)
|
||
self.agent = self._create_agent()
|
||
|
||
def _create_agent(self):
|
||
prompt = ChatPromptTemplate.from_messages([
|
||
("system", "Ты - исполнитель. У тебя есть доступ к инструментам. Выполняй шаги плана последовательно."),
|
||
("human", "{input}"),
|
||
("placeholder", "{agent_scratchpad}")
|
||
])
|
||
agent = create_agent(self.llm, self.tools, prompt)
|
||
return AgentExecutor(agent=agent, tools=self.tools, verbose=True)
|
||
|
||
"""Устаревший агент-исполнитель. Теперь используется DeveloperAgent напрямую."""
|
||
|
||
def __init__(self, tools=None):
|
||
# Этот класс больше не используется, оставлен для обратной совместимости
|
||
pass
|
||
|
||
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
|
||
"""Устаревший метод - теперь выполняется напрямую через DeveloperAgent"""
|
||
raise NotImplementedError("Этот метод больше не используется. Используйте DeveloperAgent.develop() напрямую.") |