├── .gitignore ├── README.md ├── main.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | venv -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LangChain-Streamlit Template 2 | 3 | This repo serves as a template for how to deploy a [LangGraph](https://langchain-ai.github.io/langgraph/) agent on Streamlit. 4 | 5 | This repo contains an `main.py` file which has a template for a chatbot implementation. 6 | 7 | ## Adding your chain 8 | 9 | To add your chain, you need to change the `load_chain` function in `main.py`. 10 | Depending on the type of your chain, you may also need to change the inputs/outputs that occur later on. 11 | 12 | ## Run locally 13 | 14 | After installing dependencies with e.g. `$ pip install -r requirements.txt`, you can run this project locally with the following command: 15 | 16 | ```sh 17 | $ streamlit run main.py 18 | ``` 19 | 20 | ## Deploy on Streamlit 21 | 22 | This is easily deployable on the Streamlit platform. 23 | Note that when setting up your Streamlit app you should make sure to add `OPENAI_API_KEY` as a secret environment variable. 24 | 25 | ## Setting up LangSmith 26 | 27 | To quickly spot issues and improve the performance of your LangGraph projects, sign up for [LangSmith](https://docs.smith.langchain.com/). LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started [here](https://docs.smith.langchain.com/). 28 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | """Python file to serve as the frontend""" 2 | import uuid 3 | 4 | import streamlit as st 5 | from streamlit_chat import message 6 | 7 | from langgraph.prebuilt import create_react_agent 8 | from langchain.chat_models import init_chat_model 9 | from langgraph.checkpoint.memory import MemorySaver 10 | 11 | def load_agent(): 12 | """Logic for loading the agent you want to use should go here.""" 13 | llm = init_chat_model("gpt-4o-mini", model_provider="openai", temperature=0) 14 | # You can declare tools here 15 | # https://python.langchain.com/docs/how_to/custom_tools/ 16 | tools = [] 17 | prompt = "Always respond like a pirate." 18 | checkpointer = MemorySaver() 19 | # https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.chat_agent_executor.create_react_agent 20 | graph = create_react_agent( 21 | llm, 22 | tools=tools, 23 | state_modifier=prompt, 24 | checkpointer=checkpointer 25 | ) 26 | return graph 27 | 28 | # From here down is all the StreamLit UI. 29 | st.set_page_config(page_title="LangChain Demo", page_icon=":robot:") 30 | st.header("LangChain Demo") 31 | 32 | if "generated" not in st.session_state: 33 | st.session_state["generated"] = [] 34 | 35 | if "past" not in st.session_state: 36 | st.session_state["past"] = [] 37 | 38 | # Add thread_id and checkpointer initialization 39 | if "thread_id" not in st.session_state: 40 | st.session_state["thread_id"] = str(uuid.uuid4()) 41 | 42 | if "agent" not in st.session_state: 43 | st.session_state["agent"] = load_agent() 44 | 45 | def get_text(): 46 | input_text = st.text_input("You: ", "Hello, how are you?", key="input") 47 | return input_text 48 | 49 | 50 | user_input = get_text() 51 | 52 | if user_input: 53 | output = st.session_state["agent"].invoke({ 54 | "messages": [{"role": "user", "content": user_input}] 55 | }, { 56 | "configurable": {"thread_id": st.session_state.thread_id} 57 | }) 58 | 59 | st.session_state.past.append(user_input) 60 | # Final state in the messages are the output 61 | st.session_state.generated.append(output["messages"][-1].content) 62 | 63 | if st.session_state["generated"]: 64 | 65 | for i in range(len(st.session_state["generated"]) - 1, -1, -1): 66 | message(st.session_state["generated"][i], key=str(i)) 67 | message(st.session_state["past"][i], is_user=True, key=str(i) + "_user") 68 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | langchain 2 | langgraph 3 | langchain_openai 4 | streamlit 5 | streamlit-chat 6 | --------------------------------------------------------------------------------