gusdelact commited on
Commit
6596b89
1 Parent(s): 2bcd23c

prgorama python para vectorizar un documento con ChromaDB

Browse files
Files changed (1) hide show
  1. vectorizar.py +39 -0
vectorizar.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.document_loaders import PyPDFLoader
2
+ from langchain.text_splitter import CharacterTextSplitter
3
+ from langchain_community.embeddings import BedrockEmbeddings
4
+ from langchain_aws import ChatBedrock
5
+ from langchain_community.vectorstores import Chroma
6
+
7
+ #Las variables de ambiente AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION
8
+ #se deben configurar en la línea de comando del sistema operativo
9
+
10
+ def initLLM():
11
+ return ChatBedrock(model_id="anthropic.claude-3-sonnet-20240229-v1:0")
12
+ def initEmbedder():
13
+ return BedrockEmbeddings(model_id='amazon.titan-embed-text-v1')
14
+
15
+ def initChromaDB(document_chunks,embbeder):
16
+ return Chroma.from_documents(document_chunks,embedding=embbeder, persist_directory='./data')
17
+
18
+ def embedding(thePathFile,embedder):
19
+ #cargar el archivo PDF
20
+ loader = PyPDFLoader(thePathFile)
21
+ pages = loader.load()
22
+ print(len(pages))
23
+ #hacer chunk de 500 caracteres
24
+ document_splitter=CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=100)
25
+ document_chunks=document_splitter.split_documents(pages)
26
+ print(len(document_chunks))
27
+ print(embedder)
28
+ if embedder is not None:
29
+ print("Cargando a la base vectorial...")
30
+ vectorDB=initChromaDB(document_chunks, embedder)
31
+ print("Fin carga")
32
+ return vectorDB
33
+
34
+ # Ejecutar la aplicación
35
+ if __name__ == "__main__":
36
+ bedrock_llm=initLLM()
37
+ bedrock_embedder=initEmbedder()
38
+ chromaDB=embedding("el principito.pdf",bedrock_embedder)
39
+ print(chromaDB)