Spaces:
Runtime error
Runtime error
File size: 17,486 Bytes
7c88df9 |
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
import os
import time
from typing import Any, Dict, Tuple
import asyncio
import numpy as np
import torch
from dotenv import load_dotenv
from vespa.application import Vespa
from vespa.io import VespaQueryResponse
from .colpali import SimMapGenerator
import backend.stopwords
import logging
class VespaQueryClient:
MAX_QUERY_TERMS = 64
VESPA_SCHEMA_NAME = "pdf_page"
SELECT_FIELDS = "id,title,url,blur_image,page_number,snippet,text"
def __init__(self, logger: logging.Logger):
"""
Initialize the VespaQueryClient by loading environment variables and establishing a connection to the Vespa application.
"""
load_dotenv()
self.logger = logger
if os.environ.get("USE_MTLS") == "true":
self.logger.info("Connected using mTLS")
mtls_key = os.environ.get("VESPA_CLOUD_MTLS_KEY")
mtls_cert = os.environ.get("VESPA_CLOUD_MTLS_CERT")
self.vespa_app_url = os.environ.get("VESPA_APP_MTLS_URL")
if not self.vespa_app_url:
raise ValueError(
"Please set the VESPA_APP_MTLS_URL environment variable"
)
if not mtls_cert or not mtls_key:
raise ValueError(
"USE_MTLS was true, but VESPA_CLOUD_MTLS_KEY and VESPA_CLOUD_MTLS_CERT were not set"
)
# write the key and cert to a file
mtls_key_path = "/tmp/vespa-data-plane-private-key.pem"
with open(mtls_key_path, "w") as f:
f.write(mtls_key)
mtls_cert_path = "/tmp/vespa-data-plane-public-cert.pem"
with open(mtls_cert_path, "w") as f:
f.write(mtls_cert)
# Instantiate Vespa connection
self.app = Vespa(
url=self.vespa_app_url, key=mtls_key_path, cert=mtls_cert_path
)
else:
self.logger.info("Connected using token")
self.vespa_app_url = os.environ.get("VESPA_APP_TOKEN_URL")
if not self.vespa_app_url:
raise ValueError(
"Please set the VESPA_APP_TOKEN_URL environment variable"
)
self.vespa_cloud_secret_token = os.environ.get("VESPA_CLOUD_SECRET_TOKEN")
if not self.vespa_cloud_secret_token:
raise ValueError(
"Please set the VESPA_CLOUD_SECRET_TOKEN environment variable"
)
# Instantiate Vespa connection
self.app = Vespa(
url=self.vespa_app_url,
vespa_cloud_secret_token=self.vespa_cloud_secret_token,
)
self.app.wait_for_application_up()
self.logger.info(f"Connected to Vespa at {self.vespa_app_url}")
def get_fields(self, sim_map: bool = False):
if not sim_map:
return self.SELECT_FIELDS
else:
return "summaryfeatures"
def format_query_results(
self, query: str, response: VespaQueryResponse, hits: int = 5
) -> dict:
"""
Format the Vespa query results.
Args:
query (str): The query text.
response (VespaQueryResponse): The response from Vespa.
hits (int, optional): Number of hits to display. Defaults to 5.
Returns:
dict: The JSON content of the response.
"""
query_time = response.json.get("timing", {}).get("searchtime", -1)
query_time = round(query_time, 2)
count = response.json.get("root", {}).get("fields", {}).get("totalCount", 0)
result_text = f"Query text: '{query}', query time {query_time}s, count={count}, top results:\n"
self.logger.debug(result_text)
return response.json
async def query_vespa_bm25(
self,
query: str,
q_emb: torch.Tensor,
hits: int = 3,
timeout: str = "10s",
sim_map: bool = False,
**kwargs,
) -> dict:
"""
Query Vespa using the BM25 ranking profile.
This corresponds to the "BM25" radio button in the UI.
Args:
query (str): The query text.
q_emb (torch.Tensor): Query embeddings.
hits (int, optional): Number of hits to retrieve. Defaults to 3.
timeout (str, optional): Query timeout. Defaults to "10s".
Returns:
dict: The formatted query results.
"""
async with self.app.asyncio(connections=1) as session:
query_embedding = self.format_q_embs(q_emb)
start = time.perf_counter()
response: VespaQueryResponse = await session.query(
body={
"yql": (
f"select {self.get_fields(sim_map=sim_map)} from {self.VESPA_SCHEMA_NAME} where userQuery();"
),
"ranking": self.get_rank_profile("bm25", sim_map),
"query": query,
"timeout": timeout,
"hits": hits,
"input.query(qt)": query_embedding,
"presentation.timing": True,
**kwargs,
},
)
assert response.is_successful(), response.json
stop = time.perf_counter()
self.logger.debug(
f"Query time + data transfer took: {stop - start} s, Vespa reported searchtime was "
f"{response.json.get('timing', {}).get('searchtime', -1)} s"
)
return self.format_query_results(query, response)
def float_to_binary_embedding(self, float_query_embedding: dict) -> dict:
"""
Convert float query embeddings to binary embeddings.
Args:
float_query_embedding (dict): Dictionary of float embeddings.
Returns:
dict: Dictionary of binary embeddings.
"""
binary_query_embeddings = {}
for key, vector in float_query_embedding.items():
binary_vector = (
np.packbits(np.where(np.array(vector) > 0, 1, 0))
.astype(np.int8)
.tolist()
)
binary_query_embeddings[key] = binary_vector
if len(binary_query_embeddings) >= self.MAX_QUERY_TERMS:
self.logger.warning(
f"Warning: Query has more than {self.MAX_QUERY_TERMS} terms. Truncating."
)
break
return binary_query_embeddings
def create_nn_query_strings(
self, binary_query_embeddings: dict, target_hits_per_query_tensor: int = 20
) -> Tuple[str, dict]:
"""
Create nearest neighbor query strings for Vespa.
Args:
binary_query_embeddings (dict): Binary query embeddings.
target_hits_per_query_tensor (int, optional): Target hits per query tensor. Defaults to 20.
Returns:
Tuple[str, dict]: Nearest neighbor query string and query tensor dictionary.
"""
nn_query_dict = {}
for i in range(len(binary_query_embeddings)):
nn_query_dict[f"input.query(rq{i})"] = binary_query_embeddings[i]
nn = " OR ".join(
[
f"({{targetHits:{target_hits_per_query_tensor}}}nearestNeighbor(embedding,rq{i}))"
for i in range(len(binary_query_embeddings))
]
)
return nn, nn_query_dict
def format_q_embs(self, q_embs: torch.Tensor) -> dict:
"""
Convert query embeddings to a dictionary of lists.
Args:
q_embs (torch.Tensor): Query embeddings tensor.
Returns:
dict: Dictionary where each key is an index and value is the embedding list.
"""
return {idx: emb.tolist() for idx, emb in enumerate(q_embs)}
async def get_result_from_query(
self,
query: str,
q_embs: torch.Tensor,
ranking: str,
idx_to_token: dict,
) -> Dict[str, Any]:
"""
Get query results from Vespa based on the ranking method.
Args:
query (str): The query text.
q_embs (torch.Tensor): Query embeddings.
ranking (str): The ranking method to use.
idx_to_token (dict): Index to token mapping.
Returns:
Dict[str, Any]: The query results.
"""
# Remove stopwords from the query to avoid visual emphasis on irrelevant words (e.g., "the", "and", "of")
query = backend.stopwords.filter(query)
rank_method = ranking.split("_")[0]
sim_map: bool = len(ranking.split("_")) > 1 and ranking.split("_")[1] == "sim"
if rank_method == "colpali": # ColPali
result = await self.query_vespa_colpali(
query=query, ranking=rank_method, q_emb=q_embs, sim_map=sim_map
)
elif rank_method == "hybrid": # Hybrid ColPali+BM25
result = await self.query_vespa_colpali(
query=query, ranking=rank_method, q_emb=q_embs, sim_map=sim_map
)
elif rank_method == "bm25":
result = await self.query_vespa_bm25(query, q_embs, sim_map=sim_map)
else:
raise ValueError(f"Unsupported ranking: {rank_method}")
if "root" not in result or "children" not in result["root"]:
result["root"] = {"children": []}
return result
for single_result in result["root"]["children"]:
self.logger.debug(single_result["fields"].keys())
return result
def get_sim_maps_from_query(
self, query: str, q_embs: torch.Tensor, ranking: str, idx_to_token: dict
):
"""
Get similarity maps from Vespa based on the ranking method.
Args:
query (str): The query text.
q_embs (torch.Tensor): Query embeddings.
ranking (str): The ranking method to use.
idx_to_token (dict): Index to token mapping.
Returns:
Dict[str, Any]: The query results.
"""
# Get the result by calling asyncio.run
result = asyncio.run(
self.get_result_from_query(query, q_embs, ranking, idx_to_token)
)
vespa_sim_maps = []
for single_result in result["root"]["children"]:
vespa_sim_map = single_result["fields"].get("summaryfeatures", None)
if vespa_sim_map is not None:
vespa_sim_maps.append(vespa_sim_map)
else:
raise ValueError("No sim_map found in Vespa response")
return vespa_sim_maps
async def get_full_image_from_vespa(self, doc_id: str) -> str:
"""
Retrieve the full image from Vespa for a given document ID.
Args:
doc_id (str): The document ID.
Returns:
str: The full image data.
"""
async with self.app.asyncio(connections=1) as session:
start = time.perf_counter()
response: VespaQueryResponse = await session.query(
body={
"yql": f'select full_image from {self.VESPA_SCHEMA_NAME} where id contains "{doc_id}"',
"ranking": "unranked",
"presentation.timing": True,
"ranking.matching.numThreadsPerSearch": 1,
},
)
assert response.is_successful(), response.json
stop = time.perf_counter()
self.logger.debug(
f"Getting image from Vespa took: {stop - start} s, Vespa reported searchtime was "
f"{response.json.get('timing', {}).get('searchtime', -1)} s"
)
return response.json["root"]["children"][0]["fields"]["full_image"]
def get_results_children(self, result: VespaQueryResponse) -> list:
return result["root"]["children"]
def results_to_search_results(
self, result: VespaQueryResponse, idx_to_token: dict
) -> list:
# Initialize sim_map_ fields in the result
fields_to_add = [
f"sim_map_{token}_{idx}"
for idx, token in idx_to_token.items()
if not SimMapGenerator.should_filter_token(token)
]
for child in result["root"]["children"]:
for sim_map_key in fields_to_add:
child["fields"][sim_map_key] = None
return self.get_results_children(result)
async def get_suggestions(self, query: str) -> list:
async with self.app.asyncio(connections=1) as session:
start = time.perf_counter()
yql = f'select questions from {self.VESPA_SCHEMA_NAME} where questions matches (".*{query}.*")'
response: VespaQueryResponse = await session.query(
body={
"yql": yql,
"query": query,
"ranking": "unranked",
"presentation.timing": True,
"presentation.summary": "suggestions",
"ranking.matching.numThreadsPerSearch": 1,
},
)
assert response.is_successful(), response.json
stop = time.perf_counter()
self.logger.debug(
f"Getting suggestions from Vespa took: {stop - start} s, Vespa reported searchtime was "
f"{response.json.get('timing', {}).get('searchtime', -1)} s"
)
search_results = (
response.json["root"]["children"]
if "root" in response.json and "children" in response.json["root"]
else []
)
questions = [
result["fields"]["questions"]
for result in search_results
if "questions" in result["fields"]
]
unique_questions = set([item for sublist in questions for item in sublist])
# remove an artifact from our data generation
if "string" in unique_questions:
unique_questions.remove("string")
return list(unique_questions)
def get_rank_profile(self, ranking: str, sim_map: bool) -> str:
if sim_map:
return f"{ranking}_sim"
else:
return ranking
async def query_vespa_colpali(
self,
query: str,
ranking: str,
q_emb: torch.Tensor,
target_hits_per_query_tensor: int = 100,
hnsw_explore_additional_hits: int = 300,
hits: int = 3,
timeout: str = "10s",
sim_map: bool = False,
**kwargs,
) -> dict:
"""
Query Vespa using nearest neighbor search with mixed tensors for MaxSim calculations.
This corresponds to the "ColPali" radio button in the UI.
Args:
query (str): The query text.
q_emb (torch.Tensor): Query embeddings.
target_hits_per_query_tensor (int, optional): Target hits per query tensor. Defaults to 20.
hits (int, optional): Number of hits to retrieve. Defaults to 3.
timeout (str, optional): Query timeout. Defaults to "10s".
Returns:
dict: The formatted query results.
"""
async with self.app.asyncio(connections=1) as session:
float_query_embedding = self.format_q_embs(q_emb)
binary_query_embeddings = self.float_to_binary_embedding(
float_query_embedding
)
# Mixed tensors for MaxSim calculations
query_tensors = {
"input.query(qtb)": binary_query_embeddings,
"input.query(qt)": float_query_embedding,
}
nn_string, nn_query_dict = self.create_nn_query_strings(
binary_query_embeddings, target_hits_per_query_tensor
)
query_tensors.update(nn_query_dict)
response: VespaQueryResponse = await session.query(
body={
**query_tensors,
"presentation.timing": True,
"yql": (
f"select {self.get_fields(sim_map=sim_map)} from {self.VESPA_SCHEMA_NAME} where {nn_string} or userQuery()"
),
"ranking.profile": self.get_rank_profile(
ranking=ranking, sim_map=sim_map
),
"timeout": timeout,
"hits": hits,
"query": query,
"hnsw.exploreAdditionalHits": hnsw_explore_additional_hits,
"ranking.rerankCount": 100,
**kwargs,
},
)
assert response.is_successful(), response.json
return self.format_query_results(query, response)
async def keepalive(self) -> bool:
"""
Query Vespa to keep the connection alive.
Returns:
bool: True if the connection is alive.
"""
async with self.app.asyncio(connections=1) as session:
response: VespaQueryResponse = await session.query(
body={
"yql": f"select title from {self.VESPA_SCHEMA_NAME} where true limit 1;",
"ranking": "unranked",
"query": "keepalive",
"timeout": "3s",
"hits": 1,
},
)
assert response.is_successful(), response.json
return True
|