- **Description:** docs: update StreamlitCallbackHandler example. - **Issue:** None - **Dependencies:** None I have updated the example for StreamlitCallbackHandler in the documentation bellow. https://python.langchain.com/docs/integrations/callbacks/streamlit Previously, the example used `initialize_agent`, which has been deprecated, so I've updated it to use `create_react_agent` instead. Many langchain users are likely searching examples of combining `create_react_agent` or `openai_tools_agent_chain` with StreamlitCallbackHandler. I'm sure this update will be really helpful for them! Unfortunately, writing unit tests for this example is difficult, so I have not written any tests. I have run this code in a standalone Python script file and ensured it runs correctly.
3.4 KiB
Streamlit
Streamlit is a faster way to build and share data apps. Streamlit turns data scripts into shareable web apps in minutes. All in pure Python. No front‑end experience required. See more examples at streamlit.io/generative-ai.
In this guide we will demonstrate how to use StreamlitCallbackHandler to display the thoughts and actions of an agent in an
interactive Streamlit app. Try it out with the running app below using the MRKL agent:
Installation and Setup
pip install langchain streamlit
You can run streamlit hello to load a sample app and validate your install succeeded. See full instructions in Streamlit's
Getting started documentation.
Display thoughts and actions
To create a StreamlitCallbackHandler, you just need to provide a parent container to render the output.
from langchain_community.callbacks import StreamlitCallbackHandler
import streamlit as st
st_callback = StreamlitCallbackHandler(st.container())
Additional keyword arguments to customize the display behavior are described in the API reference.
Scenario 1: Using an Agent with Tools
The primary supported use case today is visualizing the actions of an Agent with Tools (or Agent Executor). You can create an
agent in your Streamlit app and simply pass the StreamlitCallbackHandler to agent.run() in order to visualize the
thoughts and actions live in your app.
import streamlit as st
from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent, load_tools
from langchain_community.callbacks import StreamlitCallbackHandler
from langchain_openai import OpenAI
llm = OpenAI(temperature=0, streaming=True)
tools = load_tools(["ddg-search"])
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
if prompt := st.chat_input():
st.chat_message("user").write(prompt)
with st.chat_message("assistant"):
st_callback = StreamlitCallbackHandler(st.container())
response = agent_executor.invoke(
{"input": prompt}, {"callbacks": [st_callback]}
)
st.write(response["output"])
Note: You will need to set OPENAI_API_KEY for the above app code to run successfully.
The easiest way to do this is via Streamlit secrets.toml,
or any other local ENV management tool.
Additional scenarios
Currently StreamlitCallbackHandler is geared towards use with a LangChain Agent Executor. Support for additional agent types,
use directly with Chains, etc will be added in the future.
You may also be interested in using StreamlitChatMessageHistory for LangChain.