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