mirror of
https://github.com/hwchase17/langchain.git
synced 2026-04-03 10:55:08 +00:00
Use docusaurus versioning with a callout, merged master as well @hwchase17 @baskaryan --------- Signed-off-by: Weichen Xu <weichen.xu@databricks.com> Signed-off-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: Leonid Ganeline <leo.gan.57@gmail.com> Co-authored-by: Leonid Kuligin <lkuligin@yandex.ru> Co-authored-by: Averi Kitsch <akitsch@google.com> Co-authored-by: Erick Friis <erick@langchain.dev> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Nuno Campos <nuno@boringbits.io> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Martín Gotelli Ferenaz <martingotelliferenaz@gmail.com> Co-authored-by: Fayfox <admin@fayfox.com> Co-authored-by: Eugene Yurtsev <eugene@langchain.dev> Co-authored-by: Dawson Bauer <105886620+djbauer2@users.noreply.github.com> Co-authored-by: Ravindu Somawansa <ravindu.somawansa@gmail.com> Co-authored-by: Dhruv Chawla <43818888+Dominastorm@users.noreply.github.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: WeichenXu <weichen.xu@databricks.com> Co-authored-by: Benito Geordie <89472452+benitoThree@users.noreply.github.com> Co-authored-by: kartikTAI <129414343+kartikTAI@users.noreply.github.com> Co-authored-by: Kartik Sarangmath <kartik@thirdai.com> Co-authored-by: Sevin F. Varoglu <sfvaroglu@octoml.ai> Co-authored-by: MacanPN <martin.triska@gmail.com> Co-authored-by: Prashanth Rao <35005448+prrao87@users.noreply.github.com> Co-authored-by: Hyeongchan Kim <kozistr@gmail.com> Co-authored-by: sdan <git@sdan.io> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: Rahul Triptahi <rahul.psit.ec@gmail.com> Co-authored-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: pjb157 <84070455+pjb157@users.noreply.github.com> Co-authored-by: Eun Hye Kim <ehkim1440@gmail.com> Co-authored-by: kaijietti <43436010+kaijietti@users.noreply.github.com> Co-authored-by: Pengcheng Liu <pcliu.fd@gmail.com> Co-authored-by: Tomer Cagan <tomer@tomercagan.com> Co-authored-by: Christophe Bornet <cbornet@hotmail.com>
81 lines
3.4 KiB
Markdown
81 lines
3.4 KiB
Markdown
# Streamlit
|
||
|
||
> **[Streamlit](https://streamlit.io/) 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](https://streamlit.io/generative-ai).
|
||
|
||
[](https://codespaces.new/langchain-ai/streamlit-agent?quickstart=1)
|
||
|
||
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:
|
||
|
||
<iframe loading="lazy" src="https://langchain-mrkl.streamlit.app/?embed=true&embed_options=light_theme"
|
||
style={{ width: 100 + '%', border: 'none', marginBottom: 1 + 'rem', height: 600 }}
|
||
allow="camera;clipboard-read;clipboard-write;"
|
||
></iframe>
|
||
|
||
## Installation and Setup
|
||
|
||
```bash
|
||
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](https://docs.streamlit.io/library/get-started).
|
||
|
||
## Display thoughts and actions
|
||
|
||
To create a `StreamlitCallbackHandler`, you just need to provide a parent container to render the output.
|
||
|
||
```python
|
||
from langchain_community.callbacks.streamlit 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](https://api.python.langchain.com/en/latest/callbacks/langchain.callbacks.streamlit.streamlit_callback_handler.StreamlitCallbackHandler.html).
|
||
|
||
### 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.
|
||
|
||
```python
|
||
import streamlit as st
|
||
from langchain import hub
|
||
from langchain.agents import AgentExecutor, create_react_agent, load_tools
|
||
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](https://docs.streamlit.io/library/advanced-features/secrets-management),
|
||
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](/docs/integrations/memory/streamlit_chat_message_history) for LangChain.
|