Spaces:
Sleeping
Sleeping
Commit
•
8d46f1b
1
Parent(s):
7123cd4
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,15 @@
|
|
1 |
import streamlit as st
|
2 |
from langchain_openai import ChatOpenAI
|
|
|
|
|
3 |
from langchain_community.document_loaders import WebBaseLoader
|
4 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
5 |
from langchain_chroma import Chroma
|
6 |
from langchain_openai import OpenAIEmbeddings
|
7 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
8 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
9 |
-
from langchain_core.messages import HumanMessage
|
|
|
10 |
|
11 |
# Set page config
|
12 |
st.set_page_config(page_title="Tbank Assistant", layout="wide")
|
@@ -18,11 +21,16 @@ st.title("Tbank Customer Support Chatbot")
|
|
18 |
with st.sidebar:
|
19 |
st.header("Configuration")
|
20 |
api_key = st.text_input("Enter your OpenAI API Key:", type="password")
|
|
|
|
|
21 |
|
22 |
# Main app logic
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
26 |
|
27 |
loader = WebBaseLoader("https://www.tbankltd.com/about-us")
|
28 |
data = loader.load()
|
@@ -51,6 +59,9 @@ def initialize_components():
|
|
51 |
<context>
|
52 |
{context}
|
53 |
</context>
|
|
|
|
|
|
|
54 |
"""
|
55 |
|
56 |
question_answering_prompt = ChatPromptTemplate.from_messages(
|
@@ -59,63 +70,68 @@ def initialize_components():
|
|
59 |
"system",
|
60 |
SYSTEM_TEMPLATE,
|
61 |
),
|
|
|
62 |
MessagesPlaceholder(variable_name="messages"),
|
63 |
]
|
64 |
)
|
65 |
|
|
|
66 |
document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
|
67 |
|
68 |
-
return retriever, document_chain
|
69 |
-
|
70 |
|
71 |
# Load components
|
72 |
-
with st.spinner("Initializing Tbank Assistant..."):
|
73 |
-
|
74 |
-
|
75 |
-
# Chat interface
|
76 |
-
st.subheader("Chat with Tbank Assistant")
|
77 |
-
|
78 |
-
# Initialize chat history
|
79 |
-
if "messages" not in st.session_state:
|
80 |
-
|
81 |
-
|
82 |
-
# Display chat messages from history on app rerun
|
83 |
-
for message in st.session_state.messages:
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
# React to user input
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
119 |
|
120 |
# Add a footer
|
121 |
st.markdown("---")
|
|
|
1 |
import streamlit as st
|
2 |
from langchain_openai import ChatOpenAI
|
3 |
+
import os
|
4 |
+
import dotenv
|
5 |
from langchain_community.document_loaders import WebBaseLoader
|
6 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
7 |
from langchain_chroma import Chroma
|
8 |
from langchain_openai import OpenAIEmbeddings
|
9 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
10 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
11 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
12 |
+
from langchain.memory import ConversationBufferMemory
|
13 |
|
14 |
# Set page config
|
15 |
st.set_page_config(page_title="Tbank Assistant", layout="wide")
|
|
|
21 |
with st.sidebar:
|
22 |
st.header("Configuration")
|
23 |
api_key = st.text_input("Enter your OpenAI API Key:", type="password")
|
24 |
+
if api_key:
|
25 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
26 |
|
27 |
# Main app logic
|
28 |
+
if "OPENAI_API_KEY" in os.environ:
|
29 |
+
# Initialize components
|
30 |
+
@st.cache_resource
|
31 |
+
def initialize_components():
|
32 |
+
dotenv.load_dotenv()
|
33 |
+
chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2)
|
34 |
|
35 |
loader = WebBaseLoader("https://www.tbankltd.com/about-us")
|
36 |
data = loader.load()
|
|
|
59 |
<context>
|
60 |
{context}
|
61 |
</context>
|
62 |
+
|
63 |
+
Chat History:
|
64 |
+
{chat_history}
|
65 |
"""
|
66 |
|
67 |
question_answering_prompt = ChatPromptTemplate.from_messages(
|
|
|
70 |
"system",
|
71 |
SYSTEM_TEMPLATE,
|
72 |
),
|
73 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
74 |
MessagesPlaceholder(variable_name="messages"),
|
75 |
]
|
76 |
)
|
77 |
|
78 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
79 |
document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
|
80 |
|
81 |
+
return retriever, document_chain, memory
|
|
|
82 |
|
83 |
# Load components
|
84 |
+
with st.spinner("Initializing Tbank Assistant..."):
|
85 |
+
retriever, document_chain, memory = initialize_components()
|
86 |
+
|
87 |
+
# Chat interface
|
88 |
+
st.subheader("Chat with Tbank Assistant")
|
89 |
+
|
90 |
+
# Initialize chat history
|
91 |
+
if "messages" not in st.session_state:
|
92 |
+
st.session_state.messages = []
|
93 |
+
|
94 |
+
# Display chat messages from history on app rerun
|
95 |
+
for message in st.session_state.messages:
|
96 |
+
with st.chat_message(message["role"]):
|
97 |
+
st.markdown(message["content"])
|
98 |
+
|
99 |
+
# React to user input
|
100 |
+
if prompt := st.chat_input("What would you like to know about Tbank?"):
|
101 |
+
# Display user message in chat message container
|
102 |
+
st.chat_message("user").markdown(prompt)
|
103 |
+
# Add user message to chat history
|
104 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
105 |
+
|
106 |
+
with st.chat_message("assistant"):
|
107 |
+
message_placeholder = st.empty()
|
108 |
+
|
109 |
+
# Retrieve relevant documents
|
110 |
+
docs = retriever.get_relevant_documents(prompt)
|
111 |
+
|
112 |
+
# Generate response
|
113 |
+
response = document_chain.invoke(
|
114 |
+
{
|
115 |
+
"context": docs,
|
116 |
+
"chat_history": memory.load_memory_variables({})["chat_history"],
|
117 |
+
"messages": [
|
118 |
+
HumanMessage(content=prompt)
|
119 |
+
],
|
120 |
+
}
|
121 |
+
)
|
122 |
+
|
123 |
+
# The response is already a string, so we can use it directly
|
124 |
+
full_response = response
|
125 |
+
message_placeholder.markdown(full_response)
|
126 |
+
|
127 |
+
# Add assistant response to chat history
|
128 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
129 |
+
|
130 |
+
# Update memory
|
131 |
+
memory.save_context({"input": prompt}, {"output": full_response})
|
132 |
+
|
133 |
+
else:
|
134 |
+
st.warning("Please enter your OpenAI API Key in the sidebar to start the chatbot.")
|
135 |
|
136 |
# Add a footer
|
137 |
st.markdown("---")
|