File size: 4,938 Bytes
54656fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Build or refresh a Pinecone namespace for retrieval evaluation.

Default behavior:
  - index qa_dataset_generation/data/test_notices_2025.json

For full 2025/2026 evaluation, pass --full. Use --force to re-embed notices even
when their manifest entries look unchanged.
"""

from __future__ import annotations

import argparse
import json
import logging
import os
import sys
from pathlib import Path


ROOT = Path(__file__).parent.parent
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

DEFAULT_CORPORA = [ROOT / "qa_dataset_generation" / "data" / "test_notices_2025.json"]
FULL_EVAL_CORPORA = [
    ROOT / "qa_dataset_generation" / "data" / "test_notices_2025.json",
    ROOT / "data" / "data_2026.json",
]
DEFAULT_EVAL_NAMESPACE = "eval"
DEFAULT_EVAL_STATE = ROOT / "pinecone_index" / "eval"


def load_notices(paths: list[Path]) -> list[dict]:
    seen_urls = set()
    notices = []
    for path in paths:
        with path.open(encoding="utf-8") as f:
            data = json.load(f)
        for item in data:
            url = item.get("url")
            if not url or url in seen_urls:
                continue
            seen_urls.add(url)
            notices.append(
                {
                    "title": item.get("title", ""),
                    "url": url,
                    "date": item.get("date", ""),
                    "body": item.get("body", ""),
                    "category": item.get("category", ""),
                }
            )
    return notices


def main() -> None:
    parser = argparse.ArgumentParser(description="Build a Pinecone evaluation namespace.")
    parser.add_argument(
        "--corpus",
        type=Path,
        action="append",
        default=None,
        help="Corpus JSON file. Can be passed multiple times.",
    )
    parser.add_argument(
        "--full",
        action="store_true",
        help="Index both the 2025 QA corpus and current 2026 notices.",
    )
    parser.add_argument(
        "--namespace",
        default=DEFAULT_EVAL_NAMESPACE,
        help="Pinecone namespace to index.",
    )
    parser.add_argument(
        "--state-dir",
        type=Path,
        default=DEFAULT_EVAL_STATE,
        help="Local manifest/cache directory for the evaluation namespace.",
    )
    parser.add_argument(
        "--force",
        action="store_true",
        help="Re-embed notices even when manifest entries look unchanged.",
    )
    parser.add_argument(
        "--sync-deletions",
        action="store_true",
        help="Delete vectors for notices no longer present in the selected corpora.",
    )
    parser.add_argument(
        "--pooling",
        choices=["cls", "mean"],
        default="cls",
        help="Pooling mode used by SimCSEEmbedder.",
    )
    parser.add_argument(
        "--model",
        default=None,
        help="Embedding model name/path. Defaults to api.core.config.BASE_MODEL_EMBED.",
    )
    parser.add_argument(
        "--backend",
        choices=["simcse", "sentence-transformers"],
        default="simcse",
        help="Embedding backend.",
    )
    parser.add_argument("--notice-batch-size", type=int, default=20)
    parser.add_argument("--embed-batch-size", type=int, default=16)
    args = parser.parse_args()

    default_corpora = FULL_EVAL_CORPORA if args.full else DEFAULT_CORPORA
    corpora = [path.resolve() for path in (args.corpus or default_corpora)]
    state_dir = args.state_dir.resolve()

    os.environ["VECTOR_DB"] = "pinecone"
    os.environ["PINECONE_NAMESPACE"] = args.namespace
    os.environ["VECTOR_STATE_PATH"] = str(state_dir)
    os.environ["PINECONE_CACHE_PATH"] = str(state_dir / "pinecone_chunks.json")
    os.environ["SIMCSE_POOLING"] = args.pooling
    os.environ["EMBEDDER_BACKEND"] = args.backend
    if args.model:
        os.environ["BASE_MODEL_EMBED"] = args.model

    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")

    from api.core.models import get_vector_collection, index_notices

    notices = load_notices(corpora)
    if not notices:
        raise SystemExit("No notices loaded.")

    print("=" * 72)
    print("Building Pinecone evaluation namespace")
    print("=" * 72)
    print(f"Namespace : {args.namespace}")
    print(f"State dir : {state_dir}")
    print(f"Backend   : {args.backend}")
    print(f"Model     : {args.model or '(config default)'}")
    print(f"Pooling   : {args.pooling}")
    print("Corpora:")
    for corpus in corpora:
        print(f"  - {corpus}")
    print(f"Notices   : {len(notices)} unique URLs")

    indexed = index_notices(
        notices,
        force=args.force,
        sync_deletions=args.sync_deletions,
        notice_batch_size=args.notice_batch_size,
        embed_batch_size=args.embed_batch_size,
    )
    print(f"Indexed notices : {indexed}")
    print(f"Pinecone vectors: {get_vector_collection().count():,}")


if __name__ == "__main__":
    main()