Upload README.md
Browse files
README.md
CHANGED
|
@@ -290,6 +290,11 @@ def build_chains(llms: dict, debug_context: str) -> dict:
|
|
| 290 |
return rag_chains
|
| 291 |
```
|
| 292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
[⬆ Back to Top](#top)
|
| 294 |
|
| 295 |
---
|
|
@@ -338,14 +343,14 @@ For statistical and qualitative validation under the **RAGAS** metrics (*Faithfu
|
|
| 338 |
|
| 339 |
The two-stage pipeline was executed and evaluated across the following models:
|
| 340 |
|
| 341 |
-
* `Llama
|
| 342 |
-
* `Llama
|
| 343 |
-
* `
|
| 344 |
-
* `
|
| 345 |
-
* `
|
| 346 |
-
* `
|
| 347 |
-
* `
|
| 348 |
-
* `
|
| 349 |
|
| 350 |
📄 **Complete Log Records:**
|
| 351 |
|
|
@@ -355,4 +360,84 @@ The raw responses generated by each model for all 5 questions (containing the Le
|
|
| 355 |
|
| 356 |
---
|
| 357 |
|
| 358 |
-
[⬆ Back to Top](#top)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
return rag_chains
|
| 291 |
```
|
| 292 |
|
| 293 |
+
| Library | Version |
|
| 294 |
+
| :--- | :--- |
|
| 295 |
+
| `langchain-core` | 0.3.86 |
|
| 296 |
+
| `json_repair` | 0.44.1 |
|
| 297 |
+
|
| 298 |
[⬆ Back to Top](#top)
|
| 299 |
|
| 300 |
---
|
|
|
|
| 343 |
|
| 344 |
The two-stage pipeline was executed and evaluated across the following models:
|
| 345 |
|
| 346 |
+
* `Llama-3.3-70B-Instruct (GGUF Q8_0)`
|
| 347 |
+
* `Llama-3.1-8B-Instruct`
|
| 348 |
+
* `gemini-3.1-flash-lite`
|
| 349 |
+
* `gemini-3.1-pro-preview`
|
| 350 |
+
* `gpt-4o-mini-2024-07-18`
|
| 351 |
+
* `gpt-4o-2024-08-06`
|
| 352 |
+
* `sabia-4`
|
| 353 |
+
* `sabiazinho-4`
|
| 354 |
|
| 355 |
📄 **Complete Log Records:**
|
| 356 |
|
|
|
|
| 360 |
|
| 361 |
---
|
| 362 |
|
| 363 |
+
[⬆ Back to Top](#top)
|
| 364 |
+
|
| 365 |
+
---
|
| 366 |
+
|
| 367 |
+
<a id="7-document-vectorization-and-indexing"></a>
|
| 368 |
+
## 🔎 7. Document Vectorization and Indexing
|
| 369 |
+
|
| 370 |
+
```python
|
| 371 |
+
import pickle
|
| 372 |
+
import faiss
|
| 373 |
+
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
|
| 374 |
+
from langchain_community.vectorstores import FAISS
|
| 375 |
+
from langchain_community.docstore.in_memory import InMemoryDocstore
|
| 376 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 377 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 378 |
+
from langchain.retrievers import ParentDocumentRetriever
|
| 379 |
+
from langchain.storage import InMemoryStore
|
| 380 |
+
|
| 381 |
+
PDF_DIRECTORY = "documentos_pdf"
|
| 382 |
+
VECTOR_DB_PATH = "faiss_index_lgpd"
|
| 383 |
+
DOCSTORE_PATH = "docstore_lgpd.pkl"
|
| 384 |
+
EMBEDDING_MODEL = "intfloat/multilingual-e5-small"
|
| 385 |
+
|
| 386 |
+
def create_retriever():
|
| 387 |
+
# LGPD and ANPD guidance documents (PDF)
|
| 388 |
+
docs = DirectoryLoader(PDF_DIRECTORY, glob="*.pdf", loader_cls=PyPDFLoader).load()
|
| 389 |
+
|
| 390 |
+
# Embedding model: converts each text chunk into a 384-dimensional dense vector
|
| 391 |
+
embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
|
| 392 |
+
|
| 393 |
+
# FAISS index: exact search by L2 (Euclidean) distance.
|
| 394 |
+
# The vector dimension is read from the embedding model itself (384).
|
| 395 |
+
embedding_size = len(embeddings.embed_query("teste"))
|
| 396 |
+
index = faiss.IndexFlatL2(embedding_size)
|
| 397 |
+
vectorstore = FAISS(
|
| 398 |
+
embedding_function=embeddings,
|
| 399 |
+
index=index,
|
| 400 |
+
docstore=InMemoryDocstore(),
|
| 401 |
+
index_to_docstore_id={}
|
| 402 |
+
)
|
| 403 |
+
|
| 404 |
+
# Store for the parent chunks (full text and metadata)
|
| 405 |
+
store = InMemoryStore()
|
| 406 |
+
|
| 407 |
+
# Parent chunks: up to 1,500 characters (200 overlap), split preferably
|
| 408 |
+
# at the legal structure (chapters, sections, articles, paragraphs)
|
| 409 |
+
parent_splitter = RecursiveCharacterTextSplitter(
|
| 410 |
+
chunk_size=1500, chunk_overlap=200,
|
| 411 |
+
separators=["\nCAPÍTULO", "\nSEÇÃO", "\nArt. ", "\n§ ", "\n\n", "\n"]
|
| 412 |
+
)
|
| 413 |
+
# Child chunks: up to 400 characters (50 overlap). Only these are vectorized and indexed.
|
| 414 |
+
child_splitter = RecursiveCharacterTextSplitter(chunk_size=400, chunk_overlap=50)
|
| 415 |
+
|
| 416 |
+
# Parent Document Retriever: each child vector keeps a document ID pointing to its
|
| 417 |
+
# parent chunk in the store, so a match on a child chunk returns the broader parent text
|
| 418 |
+
retriever = ParentDocumentRetriever(
|
| 419 |
+
vectorstore=vectorstore, docstore=store, child_splitter=child_splitter,
|
| 420 |
+
parent_splitter=parent_splitter, search_type="mmr", search_kwargs={"k": 15, "fetch_k": 30}
|
| 421 |
+
)
|
| 422 |
+
|
| 423 |
+
# Splits the documents into parent/child chunks, vectorizes the child chunks
|
| 424 |
+
# and adds them to the FAISS index
|
| 425 |
+
retriever.add_documents(docs)
|
| 426 |
+
|
| 427 |
+
# Persists the FAISS index and the parent-chunk store to disk
|
| 428 |
+
vectorstore.save_local(VECTOR_DB_PATH)
|
| 429 |
+
with open(DOCSTORE_PATH, "wb") as f: pickle.dump(store, f)
|
| 430 |
+
return retriever
|
| 431 |
+
```
|
| 432 |
+
|
| 433 |
+
| Library | Version |
|
| 434 |
+
| :--- | :--- |
|
| 435 |
+
| `langchain` | 0.3.30 |
|
| 436 |
+
| `langchain-community` | 0.3.31 |
|
| 437 |
+
| `langchain-text-splitters` | 0.3.11 |
|
| 438 |
+
| `langchain-huggingface` | 0.3.1 |
|
| 439 |
+
| `sentence-transformers` | 5.1.2 |
|
| 440 |
+
| `faiss-cpu` | 1.13.0 |
|
| 441 |
+
| `pypdf` | 6.20.1 |
|
| 442 |
+
|
| 443 |
+
[⬆ Back to Top](#top)
|