File size: 7,227 Bytes
f8522bf 52d4f49 f8522bf bff81b1 f8522bf 2142374 938a9a6 d349362 2142374 6a4790d f8522bf 6a4790d f8522bf 52d4f49 81d7480 bff81b1 0945673 9f8913a 52d4f49 145f602 209d293 523a632 f7abdbb 0e47f27 c98dd30 2142374 8284d9b a241b28 209d293 f8522bf 145f602 705a5b8 f8522bf 6a4790d f8522bf 6a4790d f8522bf 3aacff9 f8522bf 3baf6b7 f8522bf 705a5b8 f8522bf fb1d0f7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# 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.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
load_dotenv() #
# groq_api_key = os.environ['GROQ_API_KEY']
groq_api_key = "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" # os.environ['GROQ_API_KEY']
print("groq_api_key: ", groq_api_key)
if "vector" not in st.session_state:
# 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
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)
#
# 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
# 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.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)
prompt = st.text_input("Input your prompt here")
# 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(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.page_content)
st.write("--------------------------------")
|