File size: 1,974 Bytes
5ed07e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import ChatMessage
from langchain_openai import ChatOpenAI
import streamlit as st

st.set_page_config(page_title="Streamlit + Langchain")

st.title("Basic Chatbot with Streamlit and Langchain")
st.caption("Features text streaming")


class StreamHandler(BaseCallbackHandler):
    def __init__(self, container, text=""):
        self.container = container
        self.text = text

    def on_llm_new_token(self, token: str, **kwargs) -> None:
        self.text += token
        self.container.markdown(self.text)


# Text input to enter OpenAI API key
with st.sidebar:
    OPENAI_API_KEY = st.text_input("Enter your OpenAI API Key", type="password")

# Streamlit session state
if "messages" not in st.session_state:
    st.session_state["messages"] = [
        ChatMessage(role="assistant", content="How can I help you?")
    ]
# Display all chat messages from session state
for message in st.session_state.messages:
    st.chat_message(message.role).write(message.content)
# If user submits a prompt in the text input, continue
if prompt := st.chat_input():
    if not OPENAI_API_KEY:
        st.error("Please add your OpenAI API key to continue.")
        st.stop()
    # Add user's prompt to the chat messages
    st.session_state.messages.append(ChatMessage(role="user", content=prompt))
    st.chat_message("user").write(prompt)
    # Display the assistant's response with langchain query
    with st.chat_message("assistant"):
        stream_handler = StreamHandler(st.empty())
        llm = ChatOpenAI(
            model="gpt-4o-mini",
            openai_api_key=OPENAI_API_KEY,
            streaming=True,
            callbacks=[stream_handler],
        )
        response = llm.invoke(st.session_state.messages)
        st.session_state.messages.append(
            ChatMessage(role="assistant", content=response.content)
        )