AjiNiktech commited on
Commit
7123cd4
1 Parent(s): a14d14a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")
13
+
14
+ # Streamlit app header
15
+ st.title("Tbank Customer Support Chatbot")
16
+
17
+ # Sidebar for API Key input
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
+ @st.cache_resource
24
+ def initialize_components():
25
+ chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2,api_key=api_key)
26
+
27
+ loader = WebBaseLoader("https://www.tbankltd.com/about-us")
28
+ data = loader.load()
29
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
30
+ all_splits = text_splitter.split_documents(data)
31
+ vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
32
+ retriever = vectorstore.as_retriever(k=4)
33
+
34
+ SYSTEM_TEMPLATE = """
35
+ You are a helpful assistant chatbot for Tbank. Your knowledge comes exclusively from the content of our website. Please follow these guidelines:
36
+
37
+ 1. When user Greets start by greeting the user warmly. For example: "Hello! Welcome to Tbank. How can I assist you today?"
38
+ 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.
39
+ 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.
40
+ 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:
41
+ "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?"
42
+ 5. Always maintain a friendly and professional tone.
43
+ 6. If you're unsure about an answer, it's okay to say so. You can respond with:
44
+ "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."
45
+ 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.
46
+ 8. End each interaction by asking if there's anything else you can help with related to Tbank.
47
+
48
+ 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.
49
+ If the user query is not in context, simply tell `We are sorry, we don't have information on this
50
+
51
+ <context>
52
+ {context}
53
+ </context>
54
+ """
55
+
56
+ question_answering_prompt = ChatPromptTemplate.from_messages(
57
+ [
58
+ (
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
+ retriever, document_chain = initialize_components()
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
+ st.session_state.messages = []
81
+
82
+ # Display chat messages from history on app rerun
83
+ for message in st.session_state.messages:
84
+ with st.chat_message(message["role"]):
85
+ st.markdown(message["content"])
86
+
87
+ # React to user input
88
+ # React to user input
89
+ if prompt := st.chat_input("What would you like to know about Tbank?"):
90
+ # Display user message in chat message container
91
+ st.chat_message("user").markdown(prompt)
92
+ # Add user message to chat history
93
+ st.session_state.messages.append({"role": "user", "content": prompt})
94
+
95
+ with st.chat_message("assistant"):
96
+ message_placeholder = st.empty()
97
+
98
+ # Retrieve relevant documents
99
+ docs = retriever.get_relevant_documents(prompt)
100
+
101
+ # Generate response
102
+ response = document_chain.invoke(
103
+ {
104
+ "context": docs,
105
+ "messages": [
106
+ HumanMessage(content=prompt)
107
+ ],
108
+ }
109
+ )
110
+
111
+ # The response is already a string, so we can use it directly
112
+ full_response = response
113
+ message_placeholder.markdown(full_response)
114
+
115
+ # Add assistant response to chat history
116
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
117
+
118
+
119
+
120
+ # Add a footer
121
+ st.markdown("---")
122
+ st.markdown("By AI Planet")