Spaces:
Sleeping
Sleeping
deepakaiplanet
commited on
Commit
•
9bc081e
1
Parent(s):
ee7ec49
Upload 5 files
Browse files- Tbank resources.pdf +0 -0
- Tbank_resources_1.pdf +0 -0
- Tbank_resources_2.pdf +0 -0
- app.py +148 -0
- requirements.txt +11 -0
Tbank resources.pdf
ADDED
Binary file (184 kB). View file
|
|
Tbank_resources_1.pdf
ADDED
Binary file (181 kB). View file
|
|
Tbank_resources_2.pdf
ADDED
Binary file (179 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from langchain.document_loaders import PyPDFLoader
|
14 |
+
|
15 |
+
# Set page config
|
16 |
+
st.set_page_config(page_title="Tbank Assistant", layout="wide")
|
17 |
+
|
18 |
+
# Streamlit app header
|
19 |
+
st.title("Tbank Customer Support Chatbot")
|
20 |
+
|
21 |
+
|
22 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
23 |
+
|
24 |
+
# Main app logic
|
25 |
+
if "OPENAI_API_KEY" in os.environ:
|
26 |
+
# Initialize components
|
27 |
+
@st.cache_resource
|
28 |
+
def initialize_components():
|
29 |
+
dotenv.load_dotenv()
|
30 |
+
chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2)
|
31 |
+
|
32 |
+
#loader1 = WebBaseLoader("https://www.tbankltd.com/")
|
33 |
+
loader1 = PyPDFLoader("Tbank_resources_1.pdf")
|
34 |
+
loader2 = PyPDFLoader("Tbank_resources_2.pdf")
|
35 |
+
data1 = loader1.load()
|
36 |
+
data2 = loader2.load()
|
37 |
+
data = data1 + data2
|
38 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
39 |
+
all_splits = text_splitter.split_documents(data)
|
40 |
+
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
|
41 |
+
retriever = vectorstore.as_retriever(k=4)
|
42 |
+
|
43 |
+
SYSTEM_TEMPLATE = """
|
44 |
+
You are Tbank's AI assistant, a chatbot whose knowledge comes exclusively from Tbank's website content and provided PDF documents. Follow these guidelines:
|
45 |
+
1. Greet users warmly, e.g., "Hello! Welcome to Tbank. How can I assist you today?"
|
46 |
+
2. If asked about your identity, state you're Tbank's AI assistant and ask how you can help.
|
47 |
+
3. Use only information from the website content and provided PDFs. Do not infer or make up information.
|
48 |
+
4. Provide clear, concise responses using only the given information. Keep answers brief and relevant.
|
49 |
+
5. For questions outside your knowledge base, respond:
|
50 |
+
"I apologize, but I don't have information about that. My knowledge is limited to Tbank's products/services and our website/document content. Is there anything specific about Tbank I can help with?"
|
51 |
+
6. Maintain a friendly, professional tone.
|
52 |
+
7. If unsure, say:
|
53 |
+
"I'm not certain about that. For accurate information, please check our website or contact our customer support team."
|
54 |
+
8. For requests for opinions or subjective information, remind users you're an AI that provides only factual information from Tbank sources.
|
55 |
+
9. End each interaction by asking if there's anything else you can help with regarding Tbank.
|
56 |
+
10. Do not hallucinate or provide information from sources other than the website and provided PDFs.
|
57 |
+
11. If the information isn't in your knowledge base, clearly state that you don't have that information rather than guessing.
|
58 |
+
12. Regularly refer to the provided PDFs for accurate, up-to-date information about Tbank's products and services.
|
59 |
+
13. Check for the basic Grammar and Spellings and understand if the spellings or grammar is slightly incorrect.
|
60 |
+
14. Understand the user query with different angle, analyze properly, check through the possible answers and then give the answer.
|
61 |
+
15. Be forgiving of minor spelling mistakes and grammatical errors in user queries. Try to understand the intent behind the question.
|
62 |
+
16. Maintain context from previous messages in the conversation. If a user asks about a person or topic mentioned earlier, refer back to that information.
|
63 |
+
17. If a user asks about a person using only a name or title, try to identify who they're referring to based on previous context or your knowledge base.
|
64 |
+
18. When answering questions about specific people, provide their full name and title if available.
|
65 |
+
|
66 |
+
Your primary goal is to assist users with information directly related to Tbank, using only the website content and provided PDF documents. Avoid speculation and stick strictly to the provided information.
|
67 |
+
<context>
|
68 |
+
{context}
|
69 |
+
</context>
|
70 |
+
Chat History:
|
71 |
+
{chat_history}
|
72 |
+
"""
|
73 |
+
|
74 |
+
question_answering_prompt = ChatPromptTemplate.from_messages(
|
75 |
+
[
|
76 |
+
(
|
77 |
+
"system",
|
78 |
+
SYSTEM_TEMPLATE,
|
79 |
+
),
|
80 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
81 |
+
MessagesPlaceholder(variable_name="messages"),
|
82 |
+
]
|
83 |
+
)
|
84 |
+
|
85 |
+
document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
|
86 |
+
|
87 |
+
return retriever, document_chain
|
88 |
+
|
89 |
+
# Load components
|
90 |
+
with st.spinner("Initializing Tbank Assistant..."):
|
91 |
+
retriever, document_chain = initialize_components()
|
92 |
+
|
93 |
+
# Initialize memory for each session
|
94 |
+
if "memory" not in st.session_state:
|
95 |
+
st.session_state.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
96 |
+
|
97 |
+
# Chat interface
|
98 |
+
st.subheader("Chat with Tbank Assistant")
|
99 |
+
|
100 |
+
# Initialize chat history
|
101 |
+
if "messages" not in st.session_state:
|
102 |
+
st.session_state.messages = []
|
103 |
+
|
104 |
+
# Display chat messages from history on app rerun
|
105 |
+
for message in st.session_state.messages:
|
106 |
+
with st.chat_message(message["role"]):
|
107 |
+
st.markdown(message["content"])
|
108 |
+
|
109 |
+
# React to user input
|
110 |
+
if prompt := st.chat_input("What would you like to know about Tbank?"):
|
111 |
+
# Display user message in chat message container
|
112 |
+
st.chat_message("user").markdown(prompt)
|
113 |
+
# Add user message to chat history
|
114 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
115 |
+
|
116 |
+
with st.chat_message("assistant"):
|
117 |
+
message_placeholder = st.empty()
|
118 |
+
|
119 |
+
# Retrieve relevant documents
|
120 |
+
docs = retriever.get_relevant_documents(prompt)
|
121 |
+
|
122 |
+
# Generate response
|
123 |
+
response = document_chain.invoke(
|
124 |
+
{
|
125 |
+
"context": docs,
|
126 |
+
"chat_history": st.session_state.memory.load_memory_variables({})["chat_history"],
|
127 |
+
"messages": [
|
128 |
+
HumanMessage(content=prompt)
|
129 |
+
],
|
130 |
+
}
|
131 |
+
)
|
132 |
+
|
133 |
+
# The response is already a string, so we can use it directly
|
134 |
+
full_response = response
|
135 |
+
message_placeholder.markdown(full_response)
|
136 |
+
|
137 |
+
# Add assistant response to chat history
|
138 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
139 |
+
|
140 |
+
# Update memory
|
141 |
+
st.session_state.memory.save_context({"input": prompt}, {"output": full_response})
|
142 |
+
|
143 |
+
else:
|
144 |
+
st.warning("Please enter your OpenAI API Key in the sidebar to start the chatbot.")
|
145 |
+
|
146 |
+
# Add a footer
|
147 |
+
st.markdown("---")
|
148 |
+
st.markdown("By AI Planet")
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
9 |
+
streamlit
|
10 |
+
langchain
|
11 |
+
pypdf
|