Stream-режим AI-агента: agent.py
This commit is contained in:
@@ -5,7 +5,7 @@ from langchain_ollama import ChatOllama
|
||||
from nomic_embed_text import NomicEmbedText
|
||||
from langchain.tools import BaseTool
|
||||
from langchain.schema import HumanMessage, AIMessage, SystemMessage
|
||||
from langchain.agents import ToolExecutor, AgentExecutor, create_openai_tools_agent
|
||||
from langchain.agents import AgentExecutor, initialize_agent, load_tools
|
||||
from langchain.prompts import ChatPromptTemplate
|
||||
|
||||
# ---------- Настройки LLM и эмбеддингов ----------
|
||||
@@ -29,7 +29,6 @@ class DummyTool(BaseTool):
|
||||
|
||||
tool = DummyTool()
|
||||
tools = [tool]
|
||||
tools_dict = {t.name: t for t in tools}
|
||||
|
||||
# ---------- Создание агента ----------
|
||||
prompt_template = ChatPromptTemplate.from_messages(
|
||||
@@ -39,8 +38,13 @@ prompt_template = ChatPromptTemplate.from_messages(
|
||||
]
|
||||
)
|
||||
|
||||
agent = create_openai_tools_agent(llm, tools, prompt=prompt_template)
|
||||
executor = AgentExecutor(agent=agent, tools=tools, verbose=False)
|
||||
agent_executor = initialize_agent(
|
||||
tools,
|
||||
llm,
|
||||
agent="openai-tools",
|
||||
verbose=False,
|
||||
prompt=prompt_template,
|
||||
)
|
||||
|
||||
# ---------- Функции форматирования ----------
|
||||
def format_message(message) -> str:
|
||||
@@ -66,8 +70,9 @@ def run_agent(user_input: str):
|
||||
global step
|
||||
step = 1
|
||||
# Инициализируем состояние с пользовательским сообщением
|
||||
init_state = {"messages": [HumanMessage(content=user_input)]}
|
||||
stream = executor.stream(init_state, stream_mode=["messages", "updates"])
|
||||
init_state = {"input": user_input}
|
||||
# В LangChain 0.2 AgentExecutor имеет метод stream()
|
||||
stream = agent_executor.stream(init_state, stream_mode=["messages", "updates"])
|
||||
|
||||
for chunk_type, chunk_data in stream:
|
||||
if chunk_type == "messages":
|
||||
|
||||
Reference in New Issue
Block a user