-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathcollect_pyvespa.py
More file actions
245 lines (226 loc) · 7.86 KB
/
collect_pyvespa.py
File metadata and controls
245 lines (226 loc) · 7.86 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
from vespa.application import Vespa
from vespa.evaluation import VespaFeatureCollector
import vespa.querybuilder as qb
from typing import Dict, Any
import json
import logging
from pathlib import Path
import argparse
def feature_collection_second_phase_query_fn(
query_text: str, top_k: int = 10, query_id: str = None
) -> Dict[str, Any]:
"""
Convert plain text into a JSON body for Vespa query with 'feature-collection' rank profile.
Includes both semantic similarity and BM25 matching with match features.
"""
return {
"yql": str(
qb.select("*")
.from_("doc")
.where(
(
qb.nearestNeighbor(
field="title_embedding",
query_vector="embedding",
annotations={
"targetHits": 100,
"label": "title_label",
},
)
| qb.nearestNeighbor(
field="chunk_embeddings",
query_vector="embedding",
annotations={
"targetHits": 100,
"label": "chunk_label",
},
)
| qb.userQuery(
query_text,
)
)
)
),
"query": query_text,
"ranking": "collect-second-phase",
"input.query(embedding)": f"embed({query_text})",
"input.query(float_embedding)": f"embed({query_text})",
"hits": top_k,
"timeout": "5s",
"presentation.summary": "no-chunks",
"presentation.timing": True,
}
def feature_collection_first_phase_query_fn(
query_text: str, top_k: int = 10, query_id: str = None
) -> Dict[str, Any]:
"""
Convert plain text into a JSON body for Vespa query with 'feature-collection' rank profile.
Includes both semantic similarity and BM25 matching with match features.
"""
return {
"yql": str(
qb.select("*")
.from_("doc")
.where(
(
qb.nearestNeighbor(
field="title_embedding",
query_vector="embedding",
annotations={
"targetHits": 100,
"label": "title_label",
},
)
| qb.nearestNeighbor(
field="chunk_embeddings",
query_vector="embedding",
annotations={
"targetHits": 100,
"label": "chunk_label",
},
)
| qb.userQuery(
query_text,
)
)
)
),
"query": query_text,
"ranking": "collect-training-data",
"input.query(embedding)": f"embed(@query)",
"input.query(float_embedding)": f"embed(@query)",
"hits": top_k,
"timeout": "5s",
"presentation.summary": "no-chunks",
"presentation.timing": True,
}
def generate_collector_name(
collect_matchfeatures: bool,
collect_rankfeatures: bool,
collect_summaryfeatures: bool,
second_phase: bool,
) -> str:
"""
Generate a collector name based on feature collection settings and phase.
Args:
collect_matchfeatures: Whether match features are being collected
collect_rankfeatures: Whether rank features are being collected
collect_summaryfeatures: Whether summary features are being collected
second_phase: Whether using second phase (True) or first phase (False)
Returns:
Generated collector name string
"""
features = []
if collect_matchfeatures:
features.append("match")
if collect_rankfeatures:
features.append("rank")
if collect_summaryfeatures:
features.append("summary")
features_str = "_".join(features) if features else "nofeatures"
phase_str = "second_phase" if second_phase else "first_phase"
return f"{features_str}_{phase_str}"
def main(args):
dataset_path = Path(args.dataset_dir)
queries_path = dataset_path / args.queries_filename
# Validate file exists
if not queries_path.exists():
raise FileNotFoundError(f"Queries file not found: {queries_path}")
logging.info(f"Loading queries from: {queries_path}")
with open(queries_path, "r") as file:
queries_data = json.load(file)
ids_to_text = {q["query_id"]: q["query_text"] for q in queries_data}
relevant_docs = {
q["query_id"]: set(map(str, q["relevant_document_ids"])) for q in queries_data
}
# Generate collector_name if not provided
if not args.collector_name:
args.collector_name = generate_collector_name(
args.collect_matchfeatures,
args.collect_rankfeatures,
args.collect_summaryfeatures,
args.second_phase,
)
logging.info(f"Generated collector name: {args.collector_name}")
logging.info(f"Connecting to Vespa at {args.vespa_url}:{args.vespa_port}")
app = Vespa(url=args.vespa_url, port=args.vespa_port)
function_to_use = (
feature_collection_second_phase_query_fn
if args.second_phase
else feature_collection_first_phase_query_fn
)
feature_collector = VespaFeatureCollector(
queries=ids_to_text,
relevant_docs=relevant_docs,
vespa_query_fn=function_to_use,
app=app,
name=args.collector_name,
id_field="id",
collect_matchfeatures=args.collect_matchfeatures,
collect_summaryfeatures=args.collect_summaryfeatures,
collect_rankfeatures=args.collect_rankfeatures,
write_csv=True,
random_hits_strategy="ratio",
random_hits_value=1,
)
results = feature_collector.collect()
return results
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Collect training data from Vespa using pyvespa VespaFeatureCollector."
)
parser.add_argument(
"--dataset_dir",
type=str,
default="../queries",
help="Directory containing the queries JSON file. Assumed relative to script location if not absolute.",
)
parser.add_argument(
"--second_phase",
action="store_true",
default=False,
help="Use second phase of feature collection. Else uses first phase.",
)
parser.add_argument(
"--queries_filename",
type=str,
default="queries.json",
help="Filename of the JSON file containing queries.",
)
parser.add_argument(
"--vespa_url",
type=str,
default="http://localhost",
help="Vespa application URL.",
)
parser.add_argument(
"--vespa_port", type=int, default=8080, help="Vespa application port."
)
parser.add_argument(
"--collector_name",
type=str,
required=False,
help="Name for the VespaFeatureCollector instance.",
)
parser.add_argument(
"--collect_matchfeatures",
action="store_true",
default=False,
help="Collect match features from Vespa responses.",
)
parser.add_argument(
"--collect_summaryfeatures",
action="store_true",
default=False,
help="Collect summary features from Vespa responses.",
)
parser.add_argument(
"--collect_rankfeatures",
action="store_true",
default=False,
help="Collect rank features from Vespa responses.",
)
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
main(args)