AjiNiktech commited on
Commit
eb1752f
1 Parent(s): 3c557d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -19
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import streamlit as st
2
- from langchain_openai import ChatOpenAI, OpenAIEmbeddings
3
  import os
4
  import dotenv
 
5
  from langchain_text_splitters import RecursiveCharacterTextSplitter
6
  from langchain_chroma import Chroma
 
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, AIMessage
@@ -31,6 +33,7 @@ if "OPENAI_API_KEY" in os.environ:
31
  dotenv.load_dotenv()
32
  chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2)
33
 
 
34
  loader1 = PyPDFLoader("Tbank resources.pdf")
35
  loader2 = PyPDFLoader("International Banking Services.pdf")
36
  data1 = loader1.load()
@@ -38,10 +41,9 @@ if "OPENAI_API_KEY" in os.environ:
38
  data = data1 + data2
39
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
40
  all_splits = text_splitter.split_documents(data)
41
-
42
  embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
43
  vectorstore = Chroma.from_documents(documents=all_splits, embedding=embeddings)
44
- retriever = vectorstore.as_retriever(search_kwargs={"k": 6, "score_threshold": 0.5})
45
 
46
  SYSTEM_TEMPLATE = """
47
  You are Tbank's AI assistant, a chatbot whose knowledge comes exclusively from Tbank's website content and provided PDF documents. Follow these guidelines:
@@ -62,10 +64,6 @@ if "OPENAI_API_KEY" in os.environ:
62
  12. Regularly refer to the provided PDFs for accurate, up-to-date information about Tbank's products and services.
63
  13. Check for the basic Grammar and Spellings and understand if the spellings or grammar is slightly incorrect.
64
  14. Understand the user query with different angle, analyze properly, check through the possible answers and then give the answer.
65
- 15. Be forgiving of minor spelling mistakes and grammatical errors in user queries. Try to understand the intent behind the question.
66
- 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.
67
- 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.
68
- 18. When answering questions about specific people, provide their full name and title if available.
69
 
70
  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.
71
 
@@ -90,8 +88,6 @@ if "OPENAI_API_KEY" in os.environ:
90
 
91
  document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
92
 
93
-
94
-
95
  return retriever, document_chain
96
 
97
  # Load components
@@ -114,7 +110,6 @@ if "OPENAI_API_KEY" in os.environ:
114
  with st.chat_message(message["role"]):
115
  st.markdown(message["content"])
116
 
117
-
118
  # React to user input
119
  if prompt := st.chat_input("What would you like to know about Tbank?"):
120
  # Display user message in chat message container
@@ -125,26 +120,21 @@ if "OPENAI_API_KEY" in os.environ:
125
  with st.chat_message("assistant"):
126
  message_placeholder = st.empty()
127
 
128
- # # Fuzzy match important terms
129
- # matched_term = fuzzy_match(prompt.lower(), important_terms)
130
- # if matched_term:
131
- # prompt = f"{prompt} (Matched term: {matched_term})"
132
-
133
  # Retrieve relevant documents
134
  docs = retriever.get_relevant_documents(prompt)
135
 
136
- # Include previous messages for context
137
- previous_messages = st.session_state.messages[-5:] # Last 5 messages
138
-
139
  # Generate response
140
  response = document_chain.invoke(
141
  {
142
  "context": docs,
143
  "chat_history": st.session_state.memory.load_memory_variables({})["chat_history"],
144
- "messages": [HumanMessage(content=msg["content"]) for msg in previous_messages] + [HumanMessage(content=prompt)],
 
 
145
  }
146
  )
147
 
 
148
  full_response = response
149
  message_placeholder.markdown(full_response)
150
 
 
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
 
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()
 
41
  data = data1 + data2
42
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
43
  all_splits = text_splitter.split_documents(data)
 
44
  embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
45
  vectorstore = Chroma.from_documents(documents=all_splits, embedding=embeddings)
46
+ retriever = vectorstore.as_retriever(k=4)
47
 
48
  SYSTEM_TEMPLATE = """
49
  You are Tbank's AI assistant, a chatbot whose knowledge comes exclusively from Tbank's website content and provided PDF documents. Follow these guidelines:
 
64
  12. Regularly refer to the provided PDFs for accurate, up-to-date information about Tbank's products and services.
65
  13. Check for the basic Grammar and Spellings and understand if the spellings or grammar is slightly incorrect.
66
  14. Understand the user query with different angle, analyze properly, check through the possible answers and then give the answer.
 
 
 
 
67
 
68
  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.
69
 
 
88
 
89
  document_chain = create_stuff_documents_chain(chat, question_answering_prompt)
90
 
 
 
91
  return retriever, document_chain
92
 
93
  # Load components
 
110
  with st.chat_message(message["role"]):
111
  st.markdown(message["content"])
112
 
 
113
  # React to user input
114
  if prompt := st.chat_input("What would you like to know about Tbank?"):
115
  # Display user message in chat message container
 
120
  with st.chat_message("assistant"):
121
  message_placeholder = st.empty()
122
 
 
 
 
 
 
123
  # Retrieve relevant documents
124
  docs = retriever.get_relevant_documents(prompt)
125
 
 
 
 
126
  # Generate response
127
  response = document_chain.invoke(
128
  {
129
  "context": docs,
130
  "chat_history": st.session_state.memory.load_memory_variables({})["chat_history"],
131
+ "messages": [
132
+ HumanMessage(content=prompt)
133
+ ],
134
  }
135
  )
136
 
137
+ # The response is already a string, so we can use it directly
138
  full_response = response
139
  message_placeholder.markdown(full_response)
140