Streaming Agent Not Really Work. #23621
Replies: 1 comment
-
To address the issue of the output not being streamed as expected, ensure that all components in your chain support streaming. Here are a few steps and considerations to help you troubleshoot and resolve the issue:
Here is an example of how to set up a chain that supports streaming: from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
parser = StrOutputParser()
chain = prompt | model | parser
async for chunk in chain.astream({"topic": "parrot"}):
print(chunk, end="|", flush=True)
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain.tools import tool
from langchain_core.callbacks import Callbacks
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
import asyncio
import random
model = ChatOpenAI(temperature=0, streaming=True)
@tool
async def where_cat_is_hiding() -> str:
return random.choice(["under the bed", "on the shelf"])
@tool
async def get_items(place: str) -> str:
if "bed" in place:
return "socks, shoes and dust bunnies"
if "shelf" in place:
return "books, pencils and pictures"
else:
return "cat snacks"
prompt = hub.pull("hwchase17/openai-tools-agent")
tools = [get_items, where_cat_is_hiding]
agent = create_openai_tools_agent(model.with_config({"tags": ["agent_llm"]}), tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools).with_config({"run_name": "Agent"})
async def test(query):
async for chunk in agent_executor.astream({"input": query}):
if "actions" in chunk:
for action in chunk["actions"]:
print(f"Calling Tool: `{action.tool}` with input `{action.tool_input}`")
elif "steps" in chunk:
for step in chunk["steps"]:
print(f"Tool Result: `{step.observation}`")
elif "output" in chunk:
print(f'Final Output: {chunk["output"]}')
else:
raise ValueError()
print("---")
asyncio.run(test("what's items are located where the cat is hiding?")) Ensure that all components in your chain are compatible with streaming to achieve the desired behavior. If the issue persists, you may need to review the implementation of each component to ensure they support streaming correctly [1][2]. |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I just followed the document.
The entire process will output in a streaming manner, but if you focus on a single step, such as "Output," you will notice that the Output is not streamed.
For example:
Get result without streaming:
System Info
System Information
Package Information
Packages not installed (Not Necessarily a Problem)
The following packages were not found:
Beta Was this translation helpful? Give feedback.
All reactions