Upload 8 files
Browse files- EMA_ARCHIJHAM.ipynb +0 -0
- Procfile +1 -0
- app.py +78 -0
- document.pkl +3 -0
- embedding.pkl +3 -0
- requirements.txt +9 -0
- setup.sh +13 -0
- utils.py +56 -0
EMA_ARCHIJHAM.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Procfile
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
web: sh setup.sh && streamlit run app.py
|
app.py
ADDED
|
@@ -0,0 +1,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 |
+
llm = ChatGoogleGenerativeAI(model="gemini-pro",google_api_key='AIzaSyDXb4x7Ywl79ND7l_MY18OgJJZKkVDXEAg')
|
| 32 |
+
|
| 33 |
+
system_msg_template = SystemMessagePromptTemplate.from_template(
|
| 34 |
+
template="""Answer the question as truthfully as possible using the provided context,
|
| 35 |
+
and if the answer is not contained within the text below, say 'I don't know'"""
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
human_msg_template = HumanMessagePromptTemplate.from_template(template="{input}")
|
| 39 |
+
prompt_template = ChatPromptTemplate.from_messages([system_msg_template, MessagesPlaceholder(variable_name="history"), human_msg_template])
|
| 40 |
+
conversation = ConversationChain(memory=st.session_state.buffer_memory, prompt=prompt_template, llm=llm, verbose=True)
|
| 41 |
+
|
| 42 |
+
response_container = st.container()
|
| 43 |
+
text_container = st.container()
|
| 44 |
+
|
| 45 |
+
# @st.cache_resource
|
| 46 |
+
# def setup_db():
|
| 47 |
+
# from langchain.vectorstores import Chroma
|
| 48 |
+
# db = Chroma.from_documents(document, embedding)
|
| 49 |
+
# return db
|
| 50 |
+
|
| 51 |
+
# db = setup_db()
|
| 52 |
+
|
| 53 |
+
with text_container:
|
| 54 |
+
query = st.text_input("Query: ", key="input")
|
| 55 |
+
if query:
|
| 56 |
+
with st.spinner("typing..."):
|
| 57 |
+
conversation_string = get_conversation_string()
|
| 58 |
+
|
| 59 |
+
# Use a thread pool to run query_refiner and find_match in parallel
|
| 60 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 61 |
+
refined_query_future = executor.submit(query_refiner, conversation_string, query)
|
| 62 |
+
refined_query = refined_query_future.result()
|
| 63 |
+
|
| 64 |
+
st.subheader("Refined Query:")
|
| 65 |
+
st.write(refined_query)
|
| 66 |
+
|
| 67 |
+
context = find_match(refined_query)
|
| 68 |
+
response = conversation.predict(input=f"Context:\n {context} \n\n Query:\n{refined_query}")
|
| 69 |
+
|
| 70 |
+
st.session_state.requests.append(query)
|
| 71 |
+
st.session_state.responses.append(response)
|
| 72 |
+
|
| 73 |
+
with response_container:
|
| 74 |
+
if st.session_state['responses']:
|
| 75 |
+
for i in range(len(st.session_state['responses'])):
|
| 76 |
+
message(st.session_state['responses'][i], key=str(i))
|
| 77 |
+
if i < len(st.session_state['requests']):
|
| 78 |
+
message(st.session_state["requests"][i], is_user=True, key=str(i) + '_user')
|
document.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d1bb6200856c2753f9ec03dd8da16373e5eaea503d0ad5ca00a8158a04f9c684
|
| 3 |
+
size 528344
|
embedding.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b2a28a878e8f5d48e20b69de33f92842e1f82ca16595c8e7b1323c4861a629ba
|
| 3 |
+
size 91396509
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
sentence_transformers
|
| 3 |
+
chromadb
|
| 4 |
+
google-generativeai<0.6.0
|
| 5 |
+
langchain-google-genai==1.0.3
|
| 6 |
+
PyPDF2
|
| 7 |
+
unstructured
|
| 8 |
+
streamlit_chat
|
| 9 |
+
langchain-community
|
setup.sh
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
mkdir -p ~/.streamlit/
|
| 2 |
+
|
| 3 |
+
echo "\
|
| 4 |
+
[general]\n\
|
| 5 |
+
email = \"your-email@domain.com\"\n\
|
| 6 |
+
" > ~/.streamlit/credentials.toml
|
| 7 |
+
|
| 8 |
+
echo "\
|
| 9 |
+
[server]\n\
|
| 10 |
+
headless = true\n\
|
| 11 |
+
enableCORS=false\n\
|
| 12 |
+
port = $PORT\n\
|
| 13 |
+
" > ~/.streamlit/config.toml
|
utils.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import os
|
| 5 |
+
from langchain.vectorstores import Chroma
|
| 6 |
+
os.environ['Google_API_KEY'] ="AIzaSyDXb4x7Ywl79ND7l_MY18OgJJZKkVDXEAg"
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
genai.configure(api_key=os.environ['Google_API_KEY'])
|
| 10 |
+
model = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 11 |
+
def setup():
|
| 12 |
+
embedding = pickle.load(open('embedding.pkl','rb'))
|
| 13 |
+
document = pickle.load(open('document.pkl','rb'))
|
| 14 |
+
|
| 15 |
+
return embedding,document
|
| 16 |
+
|
| 17 |
+
embedding, document = setup()
|
| 18 |
+
def find_match(input):
|
| 19 |
+
# input_em = model.encode(input).tolist()
|
| 20 |
+
# result = index.query(input_em, top_k=2, includeMetadata=True)
|
| 21 |
+
# return result['matches'][0]['metadata']['text']+"\n"+result['matches'][1]['metadata']['text']
|
| 22 |
+
db = Chroma.from_documents(document, embedding)
|
| 23 |
+
matching_doc=db.similarity_search_with_score(input,k=2)
|
| 24 |
+
return matching_doc[0][0].page_content+matching_doc[1][0].page_content
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def query_refiner(conversation, query):
|
| 28 |
+
|
| 29 |
+
generation_config = {
|
| 30 |
+
"temperature": 1,
|
| 31 |
+
"top_p": 0.95,
|
| 32 |
+
"top_k": 64,
|
| 33 |
+
"max_output_tokens": 8192,
|
| 34 |
+
# "response_mime_type": "text/plain",
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
model = genai.GenerativeModel(
|
| 38 |
+
model_name="gemini-1.5-flash",
|
| 39 |
+
generation_config=generation_config,
|
| 40 |
+
system_instruction=f"Your name is Sam, given the following user query and conversation log, formulate a question that would be the most relevant to provide the user with an answer from a knowledge base.\\n\\nCONVERSATION LOG: \\n{conversation}\\n\\nQuery: {query}\\n\\nRefined Query:",
|
| 41 |
+
)
|
| 42 |
+
# refined_query=model.generate_content(conversation=conversation, query=query)
|
| 43 |
+
# return refined_query
|
| 44 |
+
chat_session = model.start_chat(
|
| 45 |
+
history=[])
|
| 46 |
+
response=chat_session.send_message("Conversation Log:{conversation} Query:{query}")
|
| 47 |
+
return response.text
|
| 48 |
+
|
| 49 |
+
def get_conversation_string():
|
| 50 |
+
|
| 51 |
+
conversation_string = ""
|
| 52 |
+
for i in range(len(st.session_state['responses'])-1):
|
| 53 |
+
|
| 54 |
+
conversation_string += "Human: "+st.session_state['requests'][i] + "\n"
|
| 55 |
+
conversation_string += "Bot: "+ st.session_state['responses'][i+1] + "\n"
|
| 56 |
+
return conversation_string
|