copernicus-rag-core / scripts /marine_rag /verify_copernicus_docs.py
dmpantiu's picture
Upload folder using huggingface_hub
0ec8fd6 verified
Raw
History Blame Contribute Delete
3.22 kB
#!/usr/bin/env python3
"""
verify_copernicus_docs.py — sanity-check the unified copernicus_docs index
WITHOUT hitting the Gemini quota:
1. point counts per store
2. BM25 (sparse) keyword queries per domain — local FastEmbed, no API
3. dense neighbour check — reuse a stored card vector as the query vector
Prints top hits so we can eyeball that the right datasets surface per store.
"""
import json
from pathlib import Path
from qdrant_client import QdrantClient, models
from fastembed import SparseTextEmbedding
OUT = Path(__file__).resolve().parent / "out"
COLLECTION = "copernicus_docs"
client = QdrantClient(path=str(OUT / "qdrant_db"))
_bm25 = SparseTextEmbedding(model_name="Qdrant/bm25")
def sparse(text):
r = list(_bm25.embed([text]))[0]
return models.SparseVector(indices=r.indices.tolist(), values=r.values.tolist())
def kw_search(q, k=5, store=None):
flt = models.Filter(must=[models.FieldCondition(key="store", match=models.MatchValue(value=store))]) if store else None
res = client.query_points(collection_name=COLLECTION, query=sparse(q),
using="sparse", limit=k, with_payload=True, query_filter=flt).points
return res
def main():
info = client.get_collection(COLLECTION)
print(f"=== {COLLECTION}: {info.points_count} points ===\n")
# per-store counts
print("per-store counts:")
for s in ("CMEMS", "CDS", "ADS", "EWDS"):
cnt = client.count(collection_name=COLLECTION,
count_filter=models.Filter(must=[models.FieldCondition(
key="store", match=models.MatchValue(value=s))])).count
print(f" {s:6s} {cnt}")
# BM25 keyword probes across domains
probes = [
("sea surface temperature satellite", None),
("greenhouse gas carbon dioxide forecast", "ADS"),
("river discharge flood forecast europe", "EWDS"),
("ERA5 reanalysis climate", "CDS"),
("ocean salinity mediterranean", "CMEMS"),
("wildfire fire danger", None),
]
print("\n=== BM25 keyword probes ===")
for q, store in probes:
print(f"\nQ: {q!r}" + (f" [store={store}]" if store else ""))
for p in kw_search(q, k=4, store=store):
pl = p.payload
print(f" {p.score:5.2f} [{pl.get('store'):5s}] {pl.get('dataset_id','')[:45]:45s} {pl.get('product_title','')[:40]}")
# dense neighbour check — take one CDS card's stored vector, find nearest
print("\n=== dense neighbour check (stored vector as query) ===")
sample = client.scroll(collection_name=COLLECTION, limit=1, with_vectors=True,
scroll_filter=models.Filter(must=[models.FieldCondition(
key="store", match=models.MatchValue(value="CDS"))]))[0][0]
print(f"seed: [{sample.payload['store']}] {sample.payload.get('dataset_id')}")
nn = client.query_points(collection_name=COLLECTION, query=sample.vector["dense"],
using="dense", limit=5, with_payload=True).points
for p in nn:
print(f" {p.score:5.3f} [{p.payload.get('store'):5s}] {p.payload.get('dataset_id','')[:45]}")
if __name__ == "__main__":
main()