Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ from langchain_openai import OpenAI, OpenAIEmbeddings
|
|
8 |
from langchain.prompts import PromptTemplate
|
9 |
from langchain.chains import LLMChain
|
10 |
from langchain.memory import ConversationBufferWindowMemory
|
11 |
-
from langchain.text_splitter import
|
12 |
from langchain_community.vectorstores import FAISS
|
13 |
|
14 |
|
@@ -57,6 +57,7 @@ def get_text_chunks(text: str):
|
|
57 |
|
58 |
def processing(pdf):
|
59 |
"""This function divides the PDF into smaller chunks and saves these segmented chunks in a vector database. And return the Vector Database"""
|
|
|
60 |
# getting all the raw text from the PDF
|
61 |
raw_text = get_pdf_text(pdf)
|
62 |
|
@@ -68,9 +69,11 @@ def processing(pdf):
|
|
68 |
|
69 |
return vectorDB
|
70 |
|
71 |
-
def get_response(query: str)
|
|
|
|
|
72 |
# getting the context from the database that is similar to the user query
|
73 |
-
query_context = st.session_state.vectorDB.similarity_search(query=query,k=
|
74 |
# calling the chain to get the output from the LLM
|
75 |
response = st.session_state.chain.invoke({'human_input':query,'context':query_context,'name':st.session_state.bot_name})['text']
|
76 |
# Iterate through each word in the 'response' string after splitting it based on whitespace
|
@@ -82,13 +85,15 @@ def get_response(query: str) -> str:
|
|
82 |
time.sleep(0.05)
|
83 |
|
84 |
def get_conversation_chain(vectorDB):
|
|
|
|
|
85 |
# using OPENAI LLM
|
86 |
llm = OpenAI(temperature=0.4)
|
87 |
|
88 |
# creating a template to pass into LLM
|
89 |
-
template = """You are a Personalized ChatBot with a name: {name} for a company's customer support system, aiming to enhance the customer experience by providing tailored assistance and information.
|
90 |
|
91 |
-
Answer the question as detailed as possible and to the point from the context: {context}\n , and make sure to provide all the information, if the answer is not in the provided context just say, "answer is not available in the context",
|
92 |
|
93 |
{chat_history}
|
94 |
Human: {human_input}
|
@@ -139,7 +144,7 @@ if __name__ =='__main__':
|
|
139 |
with st.chat_message(message["role"]):
|
140 |
st.write(message["content"])
|
141 |
|
142 |
-
# taking the input i.e. query from the user (
|
143 |
if prompt := st.chat_input(f"Message {st.session_state.bot_name}"):
|
144 |
# Add user message to chat history
|
145 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
8 |
from langchain.prompts import PromptTemplate
|
9 |
from langchain.chains import LLMChain
|
10 |
from langchain.memory import ConversationBufferWindowMemory
|
11 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
12 |
from langchain_community.vectorstores import FAISS
|
13 |
|
14 |
|
|
|
57 |
|
58 |
def processing(pdf):
|
59 |
"""This function divides the PDF into smaller chunks and saves these segmented chunks in a vector database. And return the Vector Database"""
|
60 |
+
|
61 |
# getting all the raw text from the PDF
|
62 |
raw_text = get_pdf_text(pdf)
|
63 |
|
|
|
69 |
|
70 |
return vectorDB
|
71 |
|
72 |
+
def get_response(query: str):
|
73 |
+
"""This function will return the output of the user query! """
|
74 |
+
|
75 |
# getting the context from the database that is similar to the user query
|
76 |
+
query_context = st.session_state.vectorDB.similarity_search(query=query,k=4)
|
77 |
# calling the chain to get the output from the LLM
|
78 |
response = st.session_state.chain.invoke({'human_input':query,'context':query_context,'name':st.session_state.bot_name})['text']
|
79 |
# Iterate through each word in the 'response' string after splitting it based on whitespace
|
|
|
85 |
time.sleep(0.05)
|
86 |
|
87 |
def get_conversation_chain(vectorDB):
|
88 |
+
""" This function will create and return a LLM-Chain"""
|
89 |
+
|
90 |
# using OPENAI LLM
|
91 |
llm = OpenAI(temperature=0.4)
|
92 |
|
93 |
# creating a template to pass into LLM
|
94 |
+
template = """You are a Personalized ChatBot with a name: {name} for a company's customer support system, aiming to enhance the customer experience by providing tailored assistance and information.
|
95 |
|
96 |
+
Answer the question as detailed as possible and to the point from the context: {context}\n , and make sure to provide all the information, if the answer is not in the provided context just say, "answer is not available in the context", do not provide the wrong answer\n\n
|
97 |
|
98 |
{chat_history}
|
99 |
Human: {human_input}
|
|
|
144 |
with st.chat_message(message["role"]):
|
145 |
st.write(message["content"])
|
146 |
|
147 |
+
# taking the input i.e. query from the user (walrus operator)
|
148 |
if prompt := st.chat_input(f"Message {st.session_state.bot_name}"):
|
149 |
# Add user message to chat history
|
150 |
st.session_state.messages.append({"role": "user", "content": prompt})
|