Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,80 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from streamlit_chat import message
|
| 3 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
-
from langchain.chains import ConversationChain
|
| 5 |
-
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
| 6 |
-
from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, MessagesPlaceholder
|
| 7 |
-
from utils import *
|
| 8 |
-
import pickle
|
| 9 |
-
import concurrent.futures
|
| 10 |
-
|
| 11 |
-
# Load embeddings and documents only once
|
| 12 |
-
# @st.cache_resource
|
| 13 |
-
# def load_embeddings():
|
| 14 |
-
# embedding = pickle.load(open('embedding.pkl', 'rb'))
|
| 15 |
-
# document = pickle.load(open('document.pkl', 'rb'))
|
| 16 |
-
# return embedding, document
|
| 17 |
-
|
| 18 |
-
# embedding, document = load_embeddings()
|
| 19 |
-
|
| 20 |
-
st.subheader("Chatbot with Langchain, Gemini, Chromadb, and Streamlit")
|
| 21 |
-
|
| 22 |
-
if 'responses' not in st.session_state:
|
| 23 |
-
st.session_state['responses'] = ["How can I assist you?"]
|
| 24 |
-
|
| 25 |
-
if 'requests' not in st.session_state:
|
| 26 |
-
st.session_state['requests'] = []
|
| 27 |
-
|
| 28 |
-
if 'buffer_memory' not in st.session_state:
|
| 29 |
-
st.session_state.buffer_memory = ConversationBufferWindowMemory(k=3, return_messages=True)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
#
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
# db
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from streamlit_chat import message
|
| 3 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
+
from langchain.chains import ConversationChain
|
| 5 |
+
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
| 6 |
+
from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, MessagesPlaceholder
|
| 7 |
+
from utils import *
|
| 8 |
+
import pickle
|
| 9 |
+
import concurrent.futures
|
| 10 |
+
|
| 11 |
+
# Load embeddings and documents only once
|
| 12 |
+
# @st.cache_resource
|
| 13 |
+
# def load_embeddings():
|
| 14 |
+
# embedding = pickle.load(open('embedding.pkl', 'rb'))
|
| 15 |
+
# document = pickle.load(open('document.pkl', 'rb'))
|
| 16 |
+
# return embedding, document
|
| 17 |
+
|
| 18 |
+
# embedding, document = load_embeddings()
|
| 19 |
+
|
| 20 |
+
st.subheader("Chatbot with Langchain, Gemini, Chromadb, and Streamlit")
|
| 21 |
+
|
| 22 |
+
if 'responses' not in st.session_state:
|
| 23 |
+
st.session_state['responses'] = ["How can I assist you?"]
|
| 24 |
+
|
| 25 |
+
if 'requests' not in st.session_state:
|
| 26 |
+
st.session_state['requests'] = []
|
| 27 |
+
|
| 28 |
+
if 'buffer_memory' not in st.session_state:
|
| 29 |
+
st.session_state.buffer_memory = ConversationBufferWindowMemory(k=3, return_messages=True)
|
| 30 |
+
|
| 31 |
+
GOOGLE_API_KEY = os.getenv('Google_API_KEY')
|
| 32 |
+
|
| 33 |
+
llm = ChatGoogleGenerativeAI(model="gemini-pro",google_api_key=GOOGLE_API_KEY)
|
| 34 |
+
|
| 35 |
+
system_msg_template = SystemMessagePromptTemplate.from_template(
|
| 36 |
+
template="""Answer the question as truthfully as possible using the provided context,
|
| 37 |
+
and if the answer is not contained within the text below, say 'I don't know'"""
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
human_msg_template = HumanMessagePromptTemplate.from_template(template="{input}")
|
| 41 |
+
prompt_template = ChatPromptTemplate.from_messages([system_msg_template, MessagesPlaceholder(variable_name="history"), human_msg_template])
|
| 42 |
+
conversation = ConversationChain(memory=st.session_state.buffer_memory, prompt=prompt_template, llm=llm, verbose=True)
|
| 43 |
+
|
| 44 |
+
response_container = st.container()
|
| 45 |
+
text_container = st.container()
|
| 46 |
+
|
| 47 |
+
# @st.cache_resource
|
| 48 |
+
# def setup_db():
|
| 49 |
+
# from langchain.vectorstores import Chroma
|
| 50 |
+
# db = Chroma.from_documents(document, embedding)
|
| 51 |
+
# return db
|
| 52 |
+
|
| 53 |
+
# db = setup_db()
|
| 54 |
+
|
| 55 |
+
with text_container:
|
| 56 |
+
query = st.text_input("Query: ", key="input")
|
| 57 |
+
if query:
|
| 58 |
+
with st.spinner("typing..."):
|
| 59 |
+
conversation_string = get_conversation_string()
|
| 60 |
+
|
| 61 |
+
# Use a thread pool to run query_refiner and find_match in parallel
|
| 62 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 63 |
+
refined_query_future = executor.submit(query_refiner, conversation_string, query)
|
| 64 |
+
refined_query = refined_query_future.result()
|
| 65 |
+
|
| 66 |
+
st.subheader("Refined Query:")
|
| 67 |
+
st.write(refined_query)
|
| 68 |
+
|
| 69 |
+
context = find_match(refined_query)
|
| 70 |
+
response = conversation.predict(input=f"Context:\n {context} \n\n Query:\n{refined_query}")
|
| 71 |
+
|
| 72 |
+
st.session_state.requests.append(query)
|
| 73 |
+
st.session_state.responses.append(response)
|
| 74 |
+
|
| 75 |
+
with response_container:
|
| 76 |
+
if st.session_state['responses']:
|
| 77 |
+
for i in range(len(st.session_state['responses'])):
|
| 78 |
+
message(st.session_state['responses'][i], key=str(i))
|
| 79 |
+
if i < len(st.session_state['requests']):
|
| 80 |
+
message(st.session_state["requests"][i], is_user=True, key=str(i) + '_user')
|