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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 2 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 3 |
+
from langchain.retrievers.document_compressors import DocumentCompressorPipeline
|
| 4 |
+
from langchain_community.document_transformers import EmbeddingsRedundantFilter
|
| 5 |
+
from langchain.retrievers.document_compressors import EmbeddingsFilter
|
| 6 |
+
#from langchain_text_splitters import CharacterTextSplitter
|
| 7 |
+
from langchain.retrievers import ContextualCompressionRetriever
|
| 8 |
+
from langchain_groq import ChatGroq
|
| 9 |
+
#from langchain.document_loaders import HuggingFaceDatasetLoader
|
| 10 |
+
# from langchain_community.document_loaders import UnstructuredExcelLoader
|
| 11 |
+
# from langchain.document_loaders import CSVLoader
|
| 12 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 13 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 14 |
+
from langchain.vectorstores import FAISS
|
| 15 |
+
# from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
| 16 |
+
# from transformers import AutoTokenizer, pipeline
|
| 17 |
+
# from langchain import HuggingFacePipeline
|
| 18 |
+
import re
|
| 19 |
+
import os
|
| 20 |
+
import streamlit as st
|
| 21 |
+
import requests
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Define the path to the pre-trained model you want to use
|
| 25 |
+
modelPath = "sentence-transformers/all-MiniLM-l6-v2"
|
| 26 |
+
|
| 27 |
+
# Create a dictionary with model configuration options, specifying to use the CPU for computations
|
| 28 |
+
model_kwargs = {'device': 'cpu'}
|
| 29 |
+
|
| 30 |
+
# Create a dictionary with encoding options, specifically setting 'normalize_embeddings' to False
|
| 31 |
+
encode_kwargs = {'normalize_embeddings': False}
|
| 32 |
+
|
| 33 |
+
# Initialize an instance of HuggingFaceEmbeddings with the specified parameters
|
| 34 |
+
embeddings = HuggingFaceEmbeddings(
|
| 35 |
+
model_name=modelPath, # Provide the pre-trained model's path
|
| 36 |
+
model_kwargs=model_kwargs, # Pass the model configuration options
|
| 37 |
+
encode_kwargs=encode_kwargs # Pass the encoding options
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Initialize the HuggingFaceEmbeddings
|
| 42 |
+
model_path = "sentence-transformers/all-MiniLM-l6-v2"
|
| 43 |
+
model_kwargs = {'device': 'cpu'}
|
| 44 |
+
encode_kwargs = {'normalize_embeddings': False}
|
| 45 |
+
embeddings = HuggingFaceEmbeddings(
|
| 46 |
+
model_name=model_path,
|
| 47 |
+
model_kwargs=model_kwargs,
|
| 48 |
+
encode_kwargs=encode_kwargs
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Load the FAISS index
|
| 52 |
+
db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 53 |
+
|
| 54 |
+
retriever = db.as_retriever(search_kwargs={"k": 2})
|
| 55 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
| 56 |
+
redundant_filter = EmbeddingsRedundantFilter(embeddings=embeddings)
|
| 57 |
+
relevant_filter = EmbeddingsFilter(embeddings=embeddings)
|
| 58 |
+
pipeline_compressor = DocumentCompressorPipeline(transformers=[text_splitter, redundant_filter, relevant_filter])
|
| 59 |
+
compression_retriever = ContextualCompressionRetriever(base_compressor=pipeline_compressor, base_retriever=retriever)
|
| 60 |
+
|
| 61 |
+
chat = ChatGroq(temperature=0, groq_api_key="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",
|
| 62 |
+
model_name="llama3-70b-8192")
|
| 63 |
+
|
| 64 |
+
rag_template_str = ("""
|
| 65 |
+
Answer the following query based on the context given.
|
| 66 |
+
Stylization:
|
| 67 |
+
1)Don't say "According to context provided" or "Here is the answer to the query"
|
| 68 |
+
2)Include the source URLs
|
| 69 |
+
3)Include the Category it belongs to
|
| 70 |
+
Formatting:
|
| 71 |
+
1)Start answer with "ANSR:"
|
| 72 |
+
2)Use bullet points
|
| 73 |
+
Restriction:
|
| 74 |
+
1)Only use context to answer the question
|
| 75 |
+
2)If you don't know the answer,reply with "No answer found, you can contact us on https://www.i2econsulting.com/contact-us/"
|
| 76 |
+
context: {context}
|
| 77 |
+
query:{query}
|
| 78 |
+
""")
|
| 79 |
+
|
| 80 |
+
rag_prompt = ChatPromptTemplate.from_template(rag_template_str)
|
| 81 |
+
rag_chain = rag_prompt | chat | StrOutputParser()
|
| 82 |
+
|
| 83 |
+
llm = ChatGroq(groq_api_key="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",
|
| 84 |
+
model_name="mixtral-8x7b-32768")
|
| 85 |
+
|
| 86 |
+
# prompt = ChatPromptTemplate.from_template(
|
| 87 |
+
# """
|
| 88 |
+
# Answer the questions based on the provided context only.
|
| 89 |
+
# Please provide the most accurate response based on the question
|
| 90 |
+
# <context>
|
| 91 |
+
# {context}
|
| 92 |
+
# <context>
|
| 93 |
+
# Questions:{input}
|
| 94 |
+
# """
|
| 95 |
+
# )
|
| 96 |
+
rag_prompt = ChatPromptTemplate.from_template(rag_template_str)
|
| 97 |
+
rag_chain = rag_prompt | chat | StrOutputParser()
|
| 98 |
+
|
| 99 |
+
col1, col2 = st.columns([1, 7])
|
| 100 |
+
|
| 101 |
+
# Display the robot image
|
| 102 |
+
with col1:
|
| 103 |
+
st.image(image="image.png", width=80)
|
| 104 |
+
|
| 105 |
+
# Display the title
|
| 106 |
+
with col2:
|
| 107 |
+
st.title("ANSR Chatbot")
|
| 108 |
+
|
| 109 |
+
#st.title("I2E Enterprise Chatbot")
|
| 110 |
+
# Initialize chat history
|
| 111 |
+
if "messages" not in st.session_state:
|
| 112 |
+
st.session_state.messages = []
|
| 113 |
+
intro = {"role": "assistant",
|
| 114 |
+
"content": """Hello there! Welcome to i2e, your intelligent guide to unlocking productivity and efficiency! Whether you're seeking quick answers, expert assistance, or simply exploring our services, I'm here to assist you every step of the way. Let's dive in and discover how i2e can empower you to achieve your goals effortlessly. How can I assist you today?"""}
|
| 115 |
+
st.session_state.messages.append(intro)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# Display chat messages from history on app rerun
|
| 119 |
+
for message in st.session_state.messages:
|
| 120 |
+
with st.chat_message(message["role"]):
|
| 121 |
+
st.markdown(message["content"])
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# st.title("i2e Enterprise Chatbot")
|
| 125 |
+
|
| 126 |
+
# prompt = st.text_input("Ask Question")
|
| 127 |
+
|
| 128 |
+
def api_py_function(query):
|
| 129 |
+
context = compression_retriever.get_relevant_documents(query)
|
| 130 |
+
#print(context)
|
| 131 |
+
l = []
|
| 132 |
+
for documents in context[:5]:
|
| 133 |
+
if documents.state['query_similarity_score'] > 0.1:
|
| 134 |
+
content = documents.page_content + str(documents.metadata)
|
| 135 |
+
l.append(content)
|
| 136 |
+
final_context = ''.join(l)
|
| 137 |
+
if l != []:
|
| 138 |
+
response = rag_chain.invoke({"query": query, "context": final_context})
|
| 139 |
+
else:
|
| 140 |
+
response = "No answer found, Please rephrase your question or you can contact us on https://www.i2econsulting.com/contact-us/"
|
| 141 |
+
for word in response.split():
|
| 142 |
+
yield word + " "
|
| 143 |
+
time.sleep(0.05)
|
| 144 |
+
# return yield word + " "
|
| 145 |
+
|
| 146 |
+
# def response_generator():
|
| 147 |
+
|
| 148 |
+
# response=response.replace("\\n\\n"," \\n")
|
| 149 |
+
# for word in response.split():
|
| 150 |
+
# yield word + " "
|
| 151 |
+
# time.sleep(0.05)
|
| 152 |
+
# print(response)
|
| 153 |
+
# if prompt:
|
| 154 |
+
# print("processing request")
|
| 155 |
+
# full_response=api_py_function(prompt)
|
| 156 |
+
# # full_response = response.text.replace(u"\u2000", "")
|
| 157 |
+
# # full_response=response.text.replace("\\n\\n"," \\n")
|
| 158 |
+
# # full_response = full_response.replace("\\n", " \\n")
|
| 159 |
+
# st.write(full_response)
|
| 160 |
+
# Accept user input
|
| 161 |
+
if prompt := st.chat_input("ANSR is ready to answer your question"):
|
| 162 |
+
# Add user message to chat history
|
| 163 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 164 |
+
# Display user message in chat message container
|
| 165 |
+
with st.chat_message("user"):
|
| 166 |
+
st.markdown(prompt)
|
| 167 |
+
|
| 168 |
+
# Display assistant response in chat message container
|
| 169 |
+
with st.chat_message("assistant"):
|
| 170 |
+
response = st.write_stream(api_py_function(prompt))
|
| 171 |
+
# Add assistant response to chat history
|
| 172 |
+
print(st.session_state.messages)
|
| 173 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|