|
from langchain_google_genai import GoogleGenerativeAIEmbeddings |
|
from langchain_google_genai import ChatGoogleGenerativeAI |
|
from langchain_pinecone import PineconeVectorStore |
|
from langchain.prompts import PromptTemplate |
|
from langchain.chains.conversation.memory import ConversationBufferWindowMemory |
|
import streamlit as st |
|
from dotenv import load_dotenv |
|
import os |
|
load_dotenv() |
|
|
|
|
|
|
|
|
|
if "embedder" not in st.session_state: |
|
st.session_state.embedder= GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key= os.environ['GOOGLE_API_KEY']) |
|
if "db" not in st.session_state: |
|
st.session_state.db= PineconeVectorStore(index_name=os.environ['PINECONE_INDEX_NAME'], embedding=st.session_state.embedder) |
|
|
|
|
|
def retrieve_query(query,k=2): |
|
""" |
|
Funtion that takes the user query and returns top K matching text chunks from the Pinecone Vector DB using Cosine Similarity score. |
|
""" |
|
matching_results= st.session_state.db.similarity_search(query,k=k) |
|
return matching_results |
|
|
|
|
|
if "llm" not in st.session_state: |
|
st.session_state.llm= ChatGoogleGenerativeAI(google_api_key= os.environ['GOOGLE_API_KEY'], model="gemini-pro", temperature=0.5, convert_system_message_to_human=True) |
|
|
|
|
|
template = """You are a Colorado driving instructor named CRAB- Colorado Road Assistant Bot, who helps in answering any road rules related questions in English based on the information present in Colorado DMV handbook. Answer the questions concisely. |
|
Try to answer any question based on the below provided data, if not available, then use your pre-trained data. |
|
|
|
This is your previous chat history with this human who's asking the question. Use this information to answer any follow-up questions: {chat_history} |
|
|
|
Also use these texts as an additional reference to answer the questions: {relevant_docs} |
|
|
|
The question is: {question}""" |
|
|
|
prompt= PromptTemplate(input_variables=["relevant_docs", "chat_history", "question"], template=template) |
|
|
|
|
|
if "memory" not in st.session_state: |
|
num_int= 5 |
|
st.session_state.memory= ConversationBufferWindowMemory(k=num_int, memory_key='chat_history') |
|
|
|
|
|
def query_response_with_memory(query): |
|
""" |
|
Funtion that calls the LLM model to respond the query. |
|
""" |
|
|
|
matching_results= retrieve_query(query) |
|
relevant_docs="" |
|
for relevant_doc in matching_results: |
|
relevant_docs=relevant_docs + relevant_doc.page_content +" " |
|
|
|
|
|
question_prompt= prompt.format(relevant_docs= relevant_docs, chat_history= st.session_state.memory.buffer, question= query) |
|
|
|
|
|
try: |
|
response= st.session_state.llm.invoke(question_prompt).content |
|
except: |
|
response= "Please rephrase the question and try again." |
|
|
|
|
|
st.session_state.memory.save_context({"input": query}, {"output": str(response)}) |
|
|
|
return str(response) |
|
|
|
|
|
col1, col2, col3 = st.columns(3) |
|
with col1: |
|
st.write(' ') |
|
with col2: |
|
|
|
logo_link= "https://i.ibb.co/CBK8JPR/CRAB-Logo-With-Bg2.jpg" |
|
st.image(logo_link) |
|
with col3: |
|
st.write(' ') |
|
st.title("CRAB- Colorado Road Assistant Bot") |
|
st.caption("Ask any questions related to Colorado road rules, and I will provide accurate answers sourced from the Colorado Driver's Handbook.") |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages= [] |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message['role']): |
|
st.markdown(message['content']) |
|
|
|
|
|
query= st.chat_input("Type your question here.") |
|
if query: |
|
|
|
with st.chat_message("human"): |
|
st.markdown(query) |
|
|
|
|
|
st.session_state.messages.append({'role':'user', 'content':query}) |
|
|
|
|
|
response= query_response_with_memory(query) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
st.markdown(response) |
|
|
|
|
|
st.session_state.messages.append({'role':'assistant', 'content':response}) |