File size: 1,234 Bytes
ba65d38
 
 
 
9c9cf42
 
ba65d38
 
 
 
 
 
 
 
 
 
 
 
 
447f0e2
ba65d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
from __future__ import annotations
import hashlib
from typing import Any

from pdf_utils import extract_pdf_text_by_page
from chunking import chunk_text

def _stable_id(*parts: str) -> str:
    raw = "||".join(parts).encode("utf-8", errors="ignore")
    return hashlib.sha1(raw).hexdigest()

def build_vectors_for_pdf(
    pdf_bytes: bytes,
    filename: str,
    embed_fn,
    chunk_size: int,
    overlap: int,
) -> list[dict[str, Any]]:
    pages = extract_pdf_text_by_page(pdf_bytes)
    all_vectors: list[dict[str, Any]] = []

    for p in pages:
        page_num = p["page"]
        text = p["text"]
        if not text:
            continue

        chunks = chunk_text(text, chunk_size=chunk_size, overlap=overlap)
        if not chunks:
            continue

        embeddings = embed_fn(chunks)

        for idx, (chunk, vec) in enumerate(zip(chunks, embeddings)):
            vid = _stable_id(filename, str(page_num), str(idx), chunk[:80])
            metadata = {
                "filename": filename,
                "page": page_num,
                "chunk_index": idx,
                "text": chunk,
            }
            all_vectors.append({"id": vid, "values": vec, "metadata": metadata})

    return all_vectors