Spaces:
Sleeping
Sleeping
AjiNiktech
commited on
Commit
•
b6e2c4e
1
Parent(s):
de786d5
Upload 2 files
Browse files- app.py +138 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
16 |
+
|
17 |
+
# Streamlit app header
|
18 |
+
st.title("Tbank Customer Support Chatbot")
|
19 |
+
|
20 |
+
# Sidebar for API Key input
|
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()
|
37 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
38 |
+
all_splits = text_splitter.split_documents(data)
|
39 |
+
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
|
40 |
+
retriever = vectorstore.as_retriever(k=4)
|
41 |
+
|
42 |
+
SYSTEM_TEMPLATE = """
|
43 |
+
You are a helpful assistant chatbot for Tbank. Your knowledge comes exclusively from the content of our website. Please follow these guidelines:
|
44 |
+
|
45 |
+
1. When user Greets start by greeting the user warmly. For example: "Hello! Welcome to Tbank. How can I assist you today?"
|
46 |
+
2. When answering questions, use only the information provided in the website content. Do not make up or infer information that isn't explicitly stated.
|
47 |
+
3. If a user asks a question that can be answered using the website content, provide a clear and concise response. Include relevant details, but try to keep answers brief and to the point.
|
48 |
+
4. If a user asks a question that cannot be answered using the website content, or if the question is unrelated to Tbank, respond politely with something like:
|
49 |
+
"I apologize, but I don't have information about that topic. My knowledge is limited to Tbank's products/services and the content on our website. Is there anything specific about [Company/Website Name] I can help you with?"
|
50 |
+
5. Always maintain a friendly and professional tone.
|
51 |
+
6. If you're unsure about an answer, it's okay to say so. You can respond with:
|
52 |
+
"I'm not entirely sure about that. To get the most accurate information, I'd recommend checking our website or contacting our customer support team."
|
53 |
+
7. If a user asks for personal opinions or subjective information, remind them that you're an AI assistant and can only provide factual information from the website.
|
54 |
+
8. End each interaction by asking if there's anything else you can help with related to Tbank.
|
55 |
+
|
56 |
+
Remember, your primary goal is to assist users with information directly related to Tbank and its website content. Stick to this information and avoid speculating or providing information from other sources.
|
57 |
+
If the user query is not in context, simply tell `We are sorry, we don't have information on this
|
58 |
+
|
59 |
+
<context>
|
60 |
+
{context}
|
61 |
+
</context>
|
62 |
+
|
63 |
+
Chat History:
|
64 |
+
{chat_history}
|
65 |
+
"""
|
66 |
+
|
67 |
+
question_answering_prompt = ChatPromptTemplate.from_messages(
|
68 |
+
[
|
69 |
+
(
|
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("---")
|
138 |
+
st.markdown("By AI Planet")
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sqlite-utils
|
2 |
+
langchain>=0.1.17
|
3 |
+
langchain-chroma>=0.1.1
|
4 |
+
langchain-community>=0.0.36
|
5 |
+
langchain-core>=0.2.9
|
6 |
+
langchain-openai>=0.1.9
|
7 |
+
langchain-text-splitters>=0.0.1
|
8 |
+
bs4
|