AjiNiktech commited on
Commit
025429e
·
verified ·
1 Parent(s): ad0163a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -62
app.py CHANGED
@@ -16,7 +16,7 @@ from langchain.document_loaders import PyPDFLoader
16
  st.set_page_config(page_title="Enterprise document search + chat", layout="wide")
17
 
18
  # Streamlit app header
19
- st.title("Enterprise document Helpdesk")
20
 
21
  # Sidebar for API Key input
22
  with st.sidebar:
@@ -33,49 +33,48 @@ if "OPENAI_API_KEY" in os.environ:
33
  dotenv.load_dotenv()
34
  chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2)
35
 
 
36
  # loader1 = PyPDFLoader("Tbank resources.pdf")
37
  # loader2 = PyPDFLoader("International Banking Services.pdf")
38
  # data1 = loader1.load()
39
  # data2 = loader2.load()
40
  # data = data1 + data2
41
  st.header('Multiple File Upload')
42
- uploaded_files = st.file_uploader('Upload your files',accept_multiple_files=True, type=['txt', 'pdf','csv','ppt','doc','xls','pptx','xlsx'])
43
  if uploaded_files:
44
  all_documents = []
45
  for file in uploaded_files:
46
  documents = load_document(file)
47
  all_documents.extend(documents)
48
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
49
- all_splits = text_splitter.split_documents(all_documents)
50
  embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
51
  vectorstore = Chroma.from_documents(documents=all_splits, embedding=embeddings)
52
  retriever = vectorstore.as_retriever(k=4)
53
 
54
  SYSTEM_TEMPLATE = """
55
- You are Tbank's AI assistant, a chatbot whose knowledge comes exclusively from Tbank's website content and provided PDF documents. Follow these guidelines:
56
-
57
- 1. Greet users warmly, e.g., "Hello! Welcome to Tbank. How can I assist you today?"
58
- 2. If asked about your identity, state you're Tbank's AI assistant and ask how you can help.
59
- 3. Use only information from the website content and provided PDFs. Do not infer or make up information.
60
- 4. Provide clear, concise responses using only the given information. Keep answers brief and relevant.
61
- 5. For questions outside your knowledge base, respond:
62
- "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?"
63
- 6. Maintain a friendly, professional tone.
64
- 7. If unsure, say:
65
- "I'm not certain about that. For accurate information, please check our website or contact our customer support team."
66
- 8. For requests for opinions or subjective information, remind users you're an AI that provides only factual information from Tbank sources.
67
- 9. End each interaction by asking if there's anything else you can help with regarding Tbank.
68
- 10. Do not hallucinate or provide information from sources other than the website and provided PDFs.
69
- 11. If the information isn't in your knowledge base, clearly state that you don't have that information rather than guessing.
70
- 12. Regularly refer to the provided PDFs for accurate, up-to-date information about Tbank's products and services.
71
- 13. Check for the basic Grammar and Spellings and understand if the spellings or grammar is slightly incorrect.
72
- 14. Understand the user query with different angle, analyze properly, check through the possible answers and then give the answer.
73
- 15. Be forgiving of minor spelling mistakes and grammatical errors in user queries. Try to understand the intent behind the question.
74
- 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.
75
- 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.
76
- 18. When answering questions about specific people, provide their full name and title if available.
77
-
78
- 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.
79
 
80
  <context>
81
  {context}
@@ -99,59 +98,61 @@ if "OPENAI_API_KEY" in os.environ:
99
  document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
100
 
101
  return retriever, document_chain
 
 
102
 
103
- # Load components
104
  with st.spinner("Initializing Assistant..."):
105
  retriever, document_chain = initialize_components()
106
 
107
- # Initialize memory for each session
108
  if "memory" not in st.session_state:
109
  st.session_state.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
110
 
111
- # Chat interface
112
- st.subheader("Chat AI Assistant")
113
 
114
- # Initialize chat history
115
  if "messages" not in st.session_state:
116
  st.session_state.messages = []
117
 
118
- # Display chat messages from history on app rerun
119
  for message in st.session_state.messages:
120
- with st.chat_message(message["role"]):
121
  st.markdown(message["content"])
122
 
123
- # React to user input
124
  if prompt := st.chat_input("What would you like to know about Document?"):
125
- # Display user message in chat message container
126
- st.chat_message("user").markdown(prompt)
127
- # Add user message to chat history
128
- st.session_state.messages.append({"role": "user", "content": prompt})
129
-
130
- with st.chat_message("assistant"):
131
- message_placeholder = st.empty()
132
-
133
- # Retrieve relevant documents
134
- docs = retriever.get_relevant_documents(prompt)
135
-
136
- # Generate response
137
- response = document_chain.invoke(
138
- {
139
- "context": docs,
140
- "chat_history": st.session_state.memory.load_memory_variables({})["chat_history"],
141
- "messages": [
142
- HumanMessage(content=prompt)
143
- ],
144
- }
145
- )
146
-
147
- # The response is already a string, so we can use it directly
148
  full_response = response
149
  message_placeholder.markdown(full_response)
150
-
151
- # Add assistant response to chat history
152
  st.session_state.messages.append({"role": "assistant", "content": full_response})
153
-
154
- # Update memory
155
  st.session_state.memory.save_context({"input": prompt}, {"output": full_response})
156
 
157
  else:
 
16
  st.set_page_config(page_title="Enterprise document search + chat", layout="wide")
17
 
18
  # Streamlit app header
19
+ st.title("Enterprise document helpdesk")
20
 
21
  # Sidebar for API Key input
22
  with st.sidebar:
 
33
  dotenv.load_dotenv()
34
  chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2)
35
 
36
+ # #loader1 = WebBaseLoader("https://www.tbankltd.com/")
37
  # loader1 = PyPDFLoader("Tbank resources.pdf")
38
  # loader2 = PyPDFLoader("International Banking Services.pdf")
39
  # data1 = loader1.load()
40
  # data2 = loader2.load()
41
  # data = data1 + data2
42
  st.header('Multiple File Upload')
43
+ uploaded_files = st.file_uploader('Upload your files',accept_multiple_files=True, type=['txt', 'pdf', 'csv', 'ppt', 'doc', 'xls', 'pptx', 'xlsx'])
44
  if uploaded_files:
45
  all_documents = []
46
  for file in uploaded_files:
47
  documents = load_document(file)
48
  all_documents.extend(documents)
49
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
50
+ all_splits = text_splitter.split_documents(data)
51
  embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
52
  vectorstore = Chroma.from_documents(documents=all_splits, embedding=embeddings)
53
  retriever = vectorstore.as_retriever(k=4)
54
 
55
  SYSTEM_TEMPLATE = """
56
+ You are an advanced AI assistant designed for document search and chatbot functionality. Your primary functions are:
57
+
58
+ 1. Process and structure multiple documents in various formats, including:
59
+ .txt, .pdf, .csv, .ppt, .doc, .xls, .pptx, and .xlsx
60
+
61
+ 2. Extract and organize information from these unstructured documents into a coherent, searchable format.
62
+
63
+ 3. Retrieve relevant information from the processed documents based on user queries.
64
+
65
+ 4. Act as a chatbot, engaging in conversations about the content of the documents.
66
+
67
+ 5. Provide accurate and contextual responses to user questions, drawing solely from the information contained within the processed documents.
68
+
69
+ 6. If a user's question is not related to the content of the provided documents, politely inform them that you can only answer questions based on the information in the given documents.
70
+
71
+ 7. When answering, cite the specific document or section where the information was found, if possible.
72
+
73
+ 8. If there's ambiguity in a query, ask for clarification to ensure you provide the most relevant information.
74
+
75
+ 9. Maintain confidentiality and do not share or discuss information from one user's documents with other users.
76
+
77
+ Remember, your knowledge is limited to the content of the documents you've been given to process. Do not provide information or answer questions that are outside the scope of these documents. Always strive for accuracy and relevance in your responses.
 
 
78
 
79
  <context>
80
  {context}
 
98
  document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
99
 
100
  return retriever, document_chain
101
+ else:
102
+ st.warning("Please Upload File to Continue")
103
 
104
+ # Load components
105
  with st.spinner("Initializing Assistant..."):
106
  retriever, document_chain = initialize_components()
107
 
108
+ # Initialize memory for each session
109
  if "memory" not in st.session_state:
110
  st.session_state.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
111
 
112
+ # Chat interface
113
+ st.subheader("Chat with Tbank Assistant")
114
 
115
+ # Initialize chat history
116
  if "messages" not in st.session_state:
117
  st.session_state.messages = []
118
 
119
+ # Display chat messages from history on app rerun
120
  for message in st.session_state.messages:
121
+ with st.chat_message(message["role"]):
122
  st.markdown(message["content"])
123
 
124
+ # React to user input
125
  if prompt := st.chat_input("What would you like to know about Document?"):
126
+ # Display user message in chat message container
127
+ st.chat_message("user").markdown(prompt)
128
+ # Add user message to chat history
129
+ st.session_state.messages.append({"role": "user", "content": prompt})
130
+
131
+ with st.chat_message("assistant"):
132
+ message_placeholder = st.empty()
133
+
134
+ # Retrieve relevant documents
135
+ docs = retriever.get_relevant_documents(prompt)
136
+
137
+ # Generate response
138
+ response = document_chain.invoke(
139
+ {
140
+ "context": docs,
141
+ "chat_history": st.session_state.memory.load_memory_variables({})["chat_history"],
142
+ "messages": [
143
+ HumanMessage(content=prompt)
144
+ ],
145
+ }
146
+ )
147
+
148
+ # The response is already a string, so we can use it directly
149
  full_response = response
150
  message_placeholder.markdown(full_response)
151
+
152
+ # Add assistant response to chat history
153
  st.session_state.messages.append({"role": "assistant", "content": full_response})
154
+
155
+ # Update memory
156
  st.session_state.memory.save_context({"input": prompt}, {"output": full_response})
157
 
158
  else: