Mueble commited on
Commit
1c44e5c
·
verified ·
1 Parent(s): 08ca130

Create build_index.py

Browse files
Files changed (1) hide show
  1. build_index.py +31 -0
build_index.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # build_index.py (ejecutar solo una vez localmente)
2
+ import requests, re
3
+ from bs4 import BeautifulSoup
4
+ from sentence_transformers import SentenceTransformer
5
+ import chromadb
6
+ from chromadb.config import Settings
7
+ import os
8
+
9
+ # Funciones
10
+ def extraer_texto(url): ...
11
+
12
+ def dividir_en_chunks(texto, max_len=400): ...
13
+
14
+ # URLs
15
+ urls = [ ... ]
16
+ documentos = [extraer_texto(url) for url in urls if url]
17
+ chunks = [chunk for doc in documentos if doc for chunk in dividir_en_chunks(doc)]
18
+
19
+ # Embeddings
20
+ embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
21
+ vectores = embedding_model.encode(chunks)
22
+
23
+ # Guardar en persistencia
24
+ chroma_client = chromadb.Client(Settings(anonymized_telemetry=False, persist_directory="chroma_db"))
25
+ collection = chroma_client.get_or_create_collection("batalla_ayacucho_rag")
26
+
27
+ for i, chunk in enumerate(chunks):
28
+ collection.add(documents=[chunk], ids=[f"chunk_{i}"], embeddings=[vectores[i].tolist()])
29
+
30
+ chroma_client.persist()
31
+ print(f"✅ {len(chunks)} chunks embebidos y guardados.")