Langserve & AgentExecutor Output on streaming events using beta API #25201
-
Checked other resources
Commit to Help
Example Codeasync for event in self.agent_executor.astream_events(
input=input,
config=config,
version=version,
include_names=include_names,
include_types=include_types,
include_tags=include_tags,
exclude_names=exclude_names,
exclude_types=exclude_types,
exclude_tags=exclude_tags,
**kwargs
):
kind = event["event"]
if kind.startswith("on_chain"):
## Delete history here DescriptionHi, System InfoSystem Information
Package Information
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yes, you can control the output and remove from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o")
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant."),
("human", "{input}"),
# Remove the placeholder for agent_scratchpad
]
)
@tool
def magic_function(input: int) -> int:
"""Applies a magic function to an input."""
return input + 2
tools = [magic_function]
agent = create_tool_calling_agent(model, tools, prompt=prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
async for event in agent_executor.astream_events({"input": query}):
if event["event"] == "on_chat_model_stream":
event["data"].pop('history', None)
event["data"].pop('agent_scratchpad', None)
print(event["data"]) In this example, the |
Beta Was this translation helpful? Give feedback.
Yes, you can control the output and remove
history
andagent_scratchpad
fromAgentExecutor
when using theastream_events
method in Langserve by customizing the prompt and the agent's behavior. Here is an example of how you can achieve this: