| |
| """ |
| load_eqc_qa.py — load embedded EQC QA chunks into a SEPARATE embedded Qdrant. |
| |
| Mirrors marine_rag/load_qdrant.py. Collection `eqc_qa`: |
| - dense (768-dim, Cosine) gemini-embedding-2-preview |
| - sparse (BM25 via FastEmbed, IDF modifier) |
| - payload indexes: dataset_id, store, doc_type, aspect |
| |
| Storage: eqc_qa/qdrant_db (its OWN db — does NOT touch marine_rag/out/qdrant_db |
| or pubs_rag/qdrant_db, to avoid single-process lock contention). |
| |
| Usage: python load_eqc_qa.py --recreate |
| """ |
| import argparse |
| import json |
| import sys |
| import time |
| import uuid |
| from pathlib import Path |
|
|
| from qdrant_client import QdrantClient, models |
| from fastembed import SparseTextEmbedding |
|
|
| ROOT = Path(__file__).resolve().parent |
| COLLECTION = "eqc_qa" |
| DENSE_DIM = 768 |
| INPUT = ROOT / "chunks_embedded.jsonl" |
| LOCAL_DB = ROOT / "qdrant_db" |
| BATCH = 256 |
|
|
| _bm25 = SparseTextEmbedding(model_name="Qdrant/bm25") |
|
|
|
|
| def log(*a): |
| print(*a, file=sys.stderr, flush=True) |
|
|
|
|
| def to_sparse(text: str) -> models.SparseVector: |
| r = list(_bm25.embed([text]))[0] |
| return models.SparseVector(indices=r.indices.tolist(), values=r.values.tolist()) |
|
|
|
|
| def create_collection(client: QdrantClient, recreate: bool) -> None: |
| names = [c.name for c in client.get_collections().collections] |
| if COLLECTION in names: |
| if recreate: |
| client.delete_collection(COLLECTION) |
| else: |
| log(f"'{COLLECTION}' exists: {client.get_collection(COLLECTION).points_count} pts") |
| return |
| client.create_collection( |
| collection_name=COLLECTION, |
| vectors_config={"dense": models.VectorParams(size=DENSE_DIM, distance=models.Distance.COSINE)}, |
| sparse_vectors_config={"sparse": models.SparseVectorParams(modifier=models.Modifier.IDF)}, |
| ) |
| for field in ("dataset_id", "store", "doc_type", "aspect"): |
| client.create_payload_index(collection_name=COLLECTION, field_name=field, |
| field_schema=models.PayloadSchemaType.KEYWORD) |
| log(f"created '{COLLECTION}' (dense+sparse, 4 payload indexes)") |
|
|
|
|
| def load(client: QdrantClient) -> None: |
| buf, total, skipped, t0 = [], 0, 0, time.time() |
| with open(INPUT, encoding="utf-8") as f: |
| for line in f: |
| c = json.loads(line) |
| emb = c.get("embedding") |
| if not emb: |
| skipped += 1 |
| continue |
| raw = c.get("text_raw", "") |
| buf.append(models.PointStruct( |
| id=str(uuid.uuid5(uuid.NAMESPACE_DNS, c["chunk_id"])), |
| vector={"dense": emb, "sparse": to_sparse(raw)}, |
| payload={ |
| "chunk_id": c["chunk_id"], |
| "report_id": c["report_id"], |
| "dataset_id": c["dataset_id"], |
| "store": c["store"], |
| "doc_type": c["doc_type"], |
| "aspect": c["aspect"], |
| "aspect_base": c.get("aspect_base", ""), |
| "category": c.get("category", ""), |
| "match_confidence": c.get("match_confidence", ""), |
| "section": c.get("section", ""), |
| "title": c.get("title", ""), |
| "text_raw": raw[:2500], |
| }, |
| )) |
| if len(buf) >= BATCH: |
| client.upsert(collection_name=COLLECTION, points=buf) |
| total += len(buf) |
| log(f" [{total}] {total/(time.time()-t0):.0f} pts/s") |
| buf = [] |
| if buf: |
| client.upsert(collection_name=COLLECTION, points=buf) |
| total += len(buf) |
| log(f"DONE: {total} points, skipped {skipped}, total now " |
| f"{client.get_collection(COLLECTION).points_count}") |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--recreate", action="store_true") |
| a = ap.parse_args() |
| client = QdrantClient(path=str(LOCAL_DB)) |
| log(f"Qdrant local: {LOCAL_DB}") |
| create_collection(client, a.recreate) |
| load(client) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|