|
|
|
|
|
|
|
|
import PyPDF2 |
|
|
from langchain import LLMChain |
|
|
from langchain.embeddings import OpenAIEmbeddings |
|
|
from langchain.vectorstores import FAISS |
|
|
from langchain.llms import Llama |
|
|
|
|
|
|
|
|
def extract_text_from_pdfs(pdf_files): |
|
|
pdf_texts = [] |
|
|
for pdf_file in pdf_files: |
|
|
with open(pdf_file, 'rb') as file: |
|
|
reader = PyPDF2.PdfFileReader(file) |
|
|
text = "" |
|
|
for page_num in range(reader.numPages): |
|
|
text += reader.getPage(page_num).extractText() |
|
|
pdf_texts.append(text) |
|
|
return pdf_texts |
|
|
|
|
|
|
|
|
def create_vector_store(texts): |
|
|
embeddings = OpenAIEmbeddings() |
|
|
vector_store = FAISS.from_texts(texts, embeddings) |
|
|
return vector_store |
|
|
|
|
|
|
|
|
def create_rag_application(pdf_files): |
|
|
|
|
|
pdf_texts = extract_text_from_pdfs(pdf_files) |
|
|
|
|
|
|
|
|
vector_store = create_vector_store(pdf_texts) |
|
|
|
|
|
|
|
|
llm = Llama(model_name="llama-3.1") |
|
|
|
|
|
|
|
|
rag_chain = LLMChain(llm=llm, vector_store=vector_store) |
|
|
|
|
|
return rag_chain |
|
|
|
|
|
|
|
|
pdf_files = ["/Users/teagardan/Documents/Teagardan/Mission USA/CURSOR AI/LANGCHAIN LLM RAG"] |
|
|
rag_application = create_rag_application(pdf_files) |
|
|
|
|
|
import ollama |
|
|
|
|
|
|
|
|
def create_rag_application_with_ollama(pdf_files): |
|
|
|
|
|
pdf_texts = extract_text_from_pdfs(pdf_files) |
|
|
|
|
|
|
|
|
vector_store = create_vector_store(pdf_texts) |
|
|
|
|
|
|
|
|
llm = ollama.Llama(model_name="llama-3.1") |
|
|
|
|
|
|
|
|
rag_chain = LLMChain(llm=llm, vector_store=vector_store) |
|
|
|
|
|
return rag_chain |
|
|
|
|
|
|
|
|
pdf_files = ["/Users/teagardan/Documents/Teagardan/Mission USA/CURSOR AI/LANGCHAIN LLM RAG"] |
|
|
|
|
|
|
|
|
|