File size: 1,463 Bytes
7dfd89d |
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 |
import os
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceBgeEmbeddings
from langchain.document_loaders import PyPDFLoader
model_name = "BAAI/bge-large-en"
model_kwargs = {'device': 'cpu'}
encode_kwargs = {'normalize_embeddings': False}
embeddings = HuggingFaceBgeEmbeddings(
model_name=model_name,
model_kwargs=model_kwargs,
encode_kwargs=encode_kwargs
)
pdf_folder = "./pdf_folder" # Ruta a la carpeta que contiene los archivos PDF
output_folder = "stores/ConserGPT" # Carpeta de salida para los vector stores
# Crear el directorio de salida si no existe
os.makedirs(output_folder, exist_ok=True)
# Iterar a través de los archivos PDF en la carpeta
for pdf_file in os.listdir(pdf_folder):
if pdf_file.endswith(".pdf"):
pdf_path = os.path.join(pdf_folder, pdf_file)
loader = PyPDFLoader(pdf_path)
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000, chunk_overlap=100)
texts = text_splitter.split_documents(documents)
vector_store = Chroma.from_documents(texts, embeddings, collection_metadata={
"hnsw:space": "cosine"}, persist_directory=os.path.join(output_folder, f"{pdf_file}_store"))
print(f"Vector Store created for {pdf_file}")
print("All Vector Stores Created.......")
|