# NonToxicGlazeAdvisor_Chat_with_Docs_Groq_Edition_1 - app.py - 26-03-2024 | |
# STREAMLIT: | |
# https://www.datacamp.com/tutorial/streamlit: | |
# | |
# st.title(): This function allows you to add the title of the app. | |
# st.header(): This function is used to set header of a section. | |
# st.markdown(): This function is used to set a markdown of a section. | |
# st.subheader(): This function is used to set sub-header of a section. | |
# st.caption(): This function is used to write caption. | |
# st.code(): This function is used to set a code. | |
# st.latex(): This function is used to display mathematical expressions formatted as LaTeX. | |
# | |
# st.title ("this is the app title") | |
# st.header("this is the header ") | |
# st.markdown("this is the markdown") | |
# st.subheader("this is the subheader") | |
# st.caption("this is the caption") | |
# st.code("x=2021") | |
# st.latex(r''' a+a r^1+a r^2+a r^3 ''') | |
# JB: | |
# LangChainDeprecationWarning: Importing embeddings from langchain is deprecated. | |
# Importing from langchain will no longer be supported as of langchain==0.2.0. | |
# Please import from langchain-community instead: | |
# `from langchain_community.embeddings import FastEmbedEmbeddings`. | |
# To install langchain-community run `pip install -U langchain-community`. | |
from langchain_community.embeddings import FastEmbedEmbeddings | |
import os | |
import streamlit as st | |
from langchain_groq import ChatGroq | |
from langchain_community.document_loaders import WebBaseLoader | |
# JB: | |
from langchain_community.document_loaders import PyPDFLoader | |
from langchain_community.embeddings import OllamaEmbeddings | |
# JB: | |
from langchain_community.embeddings import FastEmbedEmbeddings | |
from langchain_community.document_loaders import PyPDFDirectoryLoader | |
# JB: | |
# File Directory | |
# This covers how to load all documents in a directory. | |
# Under the hood, by default this uses the UnstructuredLoader. | |
from langchain_community.document_loaders import DirectoryLoader | |
from langchain_community.document_loaders import TextLoader | |
import chardet | |
from langchain_community.vectorstores import FAISS | |
# from langchain.vectorstores import Chroma | |
# from langchain_community.vectorstores import Chroma | |
from langchain.text_splitter import RecursiveCharacterTextSplitter | |
from langchain.chains.combine_documents import create_stuff_documents_chain | |
from langchain_core.prompts import ChatPromptTemplate | |
from langchain.chains import create_retrieval_chain | |
import time | |
from dotenv import load_dotenv | |
import glob | |
load_dotenv() # | |
groq_api_key = os.environ['GROQ_API_KEY'] | |
# groq_api_key = "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" # os.environ['GROQ_API_KEY'] | |
# groq_api_key = "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" # JB OK 24-03-2024 | |
# print("groq_api_key: ", groq_api_key) | |
# st.title("Chat with Docs - Groq Edition :) ") | |
# # st.title ("this is the app title") | |
# st.title("Non-Toxic Glaze Advisor:") | |
# st.subheader("A tool for getting advicgroqe on non-toxic ceramic glazes for earthenware temperature ranges.") | |
# st.subheader("Victor Benchuijsen : (Glaze techniques / Ceramics)") | |
# st.subheader("Jan Bours : Artificial Intelligence / Data Science / Natural Language Processing (ALL RIGHTS RESERVED)") | |
# st.write("---------------------------------") | |
# st.subheader("Chat with Docs - Using AI: 'mixtral-8x7b-32768' Groq Edition (Very Fast!) - VERSION 1 - March 18, 2024") | |
# st.write("---------------------------------") | |
st.title("Adviseur voor niet-giftige glazuren:") | |
st.subheader("Een gereedschap om advies te krijgen over niet-giftige keramische glazuren voor aardewerk temperatuur bereiken.") | |
st.write("---------------------------------") | |
st.subheader("Victor Benchuijsen : (Glazuur technieken / Keramiek)") | |
st.subheader("(ALL RIGHTS RESERVED)") | |
# st.subheader("---------------------------------") | |
# st.write("---------------------------------") | |
st.subheader("Jan Bours : Artificial Intelligence / Data Science / Natural Language Processing") | |
st.subheader("(ALL RIGHTS RESERVED)") | |
st.write("---------------------------------") | |
st.subheader("Chat with Docs - Using AI: 'mixtral-8x7b-32768' Groq Edition (Very Fast!) - VERSION 1 - March 26, 2024") | |
st.write("---------------------------------") | |
# st.header("LIST OF ALL THE LOADED DOCUMENTS: ") | |
st.header("LIJST MET ALLE ACTUEEL GELADEN DOCUMENTEN: ") | |
st.write("") | |
pdf_files = glob.glob("*.pdf") | |
# word_files = glob.glob("*.docx") | |
for file in pdf_files: | |
# for file in word_files: | |
st.subheader(file) | |
st.write("---------------------------------") | |
if "vector" not in st.session_state: | |
# st.header("Chunking, embedding, storing in FAISS vectorstore (Can take a long time!).") | |
# st.subheader("Wait till this hase been done before you can enter your query! .......") | |
# st.session_state.embeddings = OllamaEmbeddings() # ORIGINAL | |
st.session_state.embeddings = FastEmbedEmbeddings() # JB | |
# st.session_state.loader = WebBaseLoader("https://paulgraham.com/greatwork.html") # ORIGINAL | |
# st.session_state.docs = st.session_state.loader.load() # ORIGINAL | |
# https://api.python.langchain.com/en/latest/document_loaders/langchain_community.document_loaders.pdf.PyPDFLoader.html | |
# https://python.langchain.com/docs/integrations/document_loaders/merge_doc | |
# from langchain_community.document_loaders import PyPDFLoader | |
# loader_pdf = PyPDFLoader("../MachineLearning-Lecture01.pdf") | |
# | |
# https://stackoverflow.com/questions/60215731/pypdf-to-read-each-pdf-in-a-folder | |
# | |
# https://api.python.langchain.com/en/latest/document_loaders/langchain_community.document_loaders.pdf.PyPDFDirectoryLoader.html | |
# https://python.langchain.com/docs/modules/data_connection/document_loaders/pdf#pypdf-directory | |
# !!!!! | |
# PyPDF Directory | |
# Load PDFs from directory | |
# from langchain_community.document_loaders import PyPDFDirectoryLoader | |
# loader = PyPDFDirectoryLoader("example_data/") | |
# docs = loader.load() | |
# | |
# ZIE OOK: | |
# https://python.langchain.com/docs/modules/data_connection/document_loaders/pdf#using-pypdf | |
# Using MathPix | |
# Inspired by Daniel Gross's https://gist.github.com/danielgross/3ab4104e14faccc12b49200843adab21 | |
# from langchain_community.document_loaders import MathpixPDFLoader | |
# loader = MathpixPDFLoader("example_data/layout-parser-paper.pdf") | |
# data = loader.load() | |
# pdf_file_path = "*.pdf" # JB | |
# st.session_state.loader = PyPDFLoader(file_path=pdf_file_path).load() # JB | |
# st.session_state.loader = PyPDFLoader(*.pdf).load() # JB syntax error *.pdf ! | |
# st.session_state.loader = PyPDFDirectoryLoader("*.pdf") # JB PyPDFDirectoryLoader("example_data/") | |
# chunks = self.text_splitter.split_documents(docs) | |
# chunks = filter_complex_metadata(chunks) | |
# JB: | |
# https://python.langchain.com/docs/modules/data_connection/document_loaders/pdf#pypdf-directory | |
# st.session_state.docs = st.session_state.loader.load() | |
# loader = PyPDFDirectoryLoader(".") | |
# docs = loader.load() | |
# st.session_state.docs = docs | |
# JB: | |
# https://python.langchain.com/docs/modules/data_connection/document_loaders/file_directory | |
# text_loader_kwargs={'autodetect_encoding': True} | |
text_loader_kwargs={'autodetect_encoding': False} | |
path = '../' | |
# loader = DirectoryLoader(path, glob="**/*.pdf", loader_cls=TextLoader, loader_kwargs=text_loader_kwargs) | |
# PyPDFDirectoryLoader (TEST): | |
# loader = PyPDFDirectoryLoader(path, glob="**/*.pdf", loader_cls=TextLoader, loader_kwargs=text_loader_kwargs) | |
# loader = PyPDFDirectoryLoader(path, glob="**/*.pdf", loader_kwargs=text_loader_kwargs) | |
loader = PyPDFDirectoryLoader(path, glob="**/*.pdf") | |
docs = loader.load() | |
st.session_state.docs = docs | |
# JB 18-03-2024: | |
# https://python.langchain.com/docs/integrations/document_loaders/ | |
# MICROSOFT WORD: | |
# https://python.langchain.com/docs/integrations/document_loaders/microsoft_word | |
# 1 - Using Docx2txt | |
# Load .docx using Docx2txt into a document. | |
# %pip install --upgrade --quiet docx2txt | |
# from langchain_community.document_loaders import Docx2txtLoader | |
# loader = Docx2txtLoader("example_data/fake.docx") | |
# data = loader.load() | |
# data | |
# [Document(page_content='Lorem ipsum dolor sit amet.', metadata={'source': 'example_data/fake.docx'})] | |
# | |
# 2A - Using Unstructured | |
# from langchain_community.document_loaders import UnstructuredWordDocumentLoader | |
# loader = UnstructuredWordDocumentLoader("example_data/fake.docx") | |
# data = loader.load() | |
# data | |
# [Document(page_content='Lorem ipsum dolor sit amet.', lookup_str='', metadata={'source': 'fake.docx'}, lookup_index=0)] | |
# | |
# 2B - Retain Elements | |
# Under the hood, Unstructured creates different “elements” for different chunks of text. | |
# By default we combine those together, but you can easily keep that separation by specifying mode="elements". | |
# loader = UnstructuredWordDocumentLoader("example_data/fake.docx", mode="elements") | |
# data = loader.load() | |
# data[0] | |
# Document(page_content='Lorem ipsum dolor sit amet.', lookup_str='', metadata={'source': 'fake.docx', 'filename': 'fake.docx', 'category': 'Title'}, lookup_index=0) | |
# | |
# 2A - Using Unstructured | |
# from langchain_community.document_loaders import UnstructuredWordDocumentLoader | |
# loader = UnstructuredWordDocumentLoader(path, glob="**/*.docx") | |
# docs = loader.load() | |
# st.session_state.docs = docs | |
st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) | |
st.session_state.documents = st.session_state.text_splitter.split_documents(st.session_state.docs) | |
# https://python.langchain.com/docs/integrations/vectorstores/faiss | |
# docs_and_scores = db.similarity_search_with_score(query) | |
# Saving and loading | |
# You can also save and load a FAISS index. | |
# This is useful so you don’t have to recreate it everytime you use it. | |
# db.save_local("faiss_index") | |
# new_db = FAISS.load_local("faiss_index", embeddings) | |
# docs = new_db.similarity_search(query) | |
# docs[0] | |
# Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'source': '../../../state_of_the_union.txt'}) | |
# | |
st.session_state.vector = FAISS.from_documents(st.session_state.documents, st.session_state.embeddings) # ORIGINAL | |
# st.session_state.vector = FAISS.from_documents(st.session_state.documents, st.session_state.embeddings) # ORIGINAL | |
#st.session_state.vector.save_local("faiss_index") | |
# The de-serialization relies loading a pickle file. | |
# Pickle files can be modified to deliver a malicious payload that results in execution of arbitrary code on your machine. | |
# You will need to set `allow_dangerous_deserialization` to `True` to enable deserialization. If you do this, make sure that you trust the source of the data. | |
#st.session_state.vector = FAISS.load_local("faiss_index", st.session_state.embeddings, allow_dangerous_deserialization=True) | |
# ZIE: | |
# ZIE VOOR EEN APP MET CHROMADB: | |
# https://github.com/vndee/local-rag-example/blob/main/rag.py | |
# https://raw.githubusercontent.com/vndee/local-rag-example/main/rag.py | |
# Chroma.from_documents(documents=chunks, embedding=FastEmbedEmbeddings()) | |
# st.session_state.vector = Chroma.from_documents(st.session_state.documents, st.session_state.embeddings) # JB | |
st.write("---------------------------------") | |
# st.title("Chat with Docs - Groq Edition :) ") | |
# st.title("Literature Based Research (LBR) - A. Unzicker and J. Bours - Chat with Docs - Groq Edition (Very Fast!) - VERSION 3 - March 8 2024") | |
llm = ChatGroq( | |
temperature=0.2, | |
groq_api_key=groq_api_key, | |
model_name='mixtral-8x7b-32768' | |
) | |
prompt = ChatPromptTemplate.from_template(""" | |
Answer the following question based only on the provided context. | |
Think step by step before providing a detailed answer. | |
I will tip you $200 if the user finds the answer helpful. | |
<context> | |
{context} | |
</context> | |
Question: {input}""") | |
document_chain = create_stuff_documents_chain(llm, prompt) | |
retriever = st.session_state.vector.as_retriever() | |
retrieval_chain = create_retrieval_chain(retriever, document_chain) | |
i=0 | |
while True: | |
# data = ["input1", "input2", "input3"] | |
#for i, item in enumerate(data): | |
key = f"input_{i}" | |
# text_input = st.text_input(f"Enter value for {item}", key=key) | |
# Access the value directly | |
print(f"Value for key: {key}") | |
i=i+1 | |
prompt = st.text_input("Input your prompt here", key=key) | |
# If the user hits enter | |
if prompt: | |
# Then pass the prompt to the LLM | |
start = time.process_time() | |
response = retrieval_chain.invoke({"input": prompt}) | |
# print(f"Response time: {time.process_time() - start}") | |
st.write(f"Response time: {time.process_time() - start} seconds") | |
st.write(response["answer"]) | |
# With a streamlit expander | |
with st.expander("Document Similarity Search"): | |
# Find the relevant chunks | |
for i, doc in enumerate(response["context"]): | |
# print(doc) | |
# st.write(f"Source Document # {i+1} : {doc.metadata['source'].split('/')[-1]}") | |
st.write(doc) | |
st.write(f"Source Document # {i+1} : {doc.metadata['source'].split('/')[-1]}") | |
st.write(doc.page_content) | |
st.write("--------------------------------") | |
st.write("---------------------------------") |