Spaces:
Running
Running
File size: 12,341 Bytes
bea5044 f86d7f2 bea5044 cc80c3d bea5044 cc80c3d bea5044 f86d7f2 bea5044 cc80c3d bea5044 cc80c3d bea5044 cc80c3d bea5044 cc80c3d bea5044 cc80c3d bea5044 cc80c3d bea5044 f86d7f2 cc80c3d bea5044 cc80c3d f86d7f2 cc80c3d bea5044 cc80c3d bea5044 f86d7f2 cc80c3d bea5044 cc80c3d bea5044 f86d7f2 cc80c3d bea5044 cc80c3d bea5044 f86d7f2 cc80c3d bea5044 cc80c3d bea5044 cc80c3d bea5044 cc80c3d f86d7f2 cc80c3d f86d7f2 cc80c3d bea5044 cc80c3d bea5044 cc80c3d bea5044 cc80c3d bea5044 f86d7f2 bea5044 f86d7f2 2744d22 bea5044 f86d7f2 bea5044 f86d7f2 bea5044 f86d7f2 bea5044 f86d7f2 bea5044 f86d7f2 bea5044 f86d7f2 bea5044 f86d7f2 bea5044 f86d7f2 bea5044 |
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 |
from typing import List, Tuple, Dict, Iterable, Iterator, Optional, Union, Any
from itertools import groupby
from torch.nn import functional as F
from pydantic import BaseModel, Field
from langchain_core.documents import Document
from elasticsearch import Elasticsearch
from ask_candid.retrieval.sparse_lexical import SpladeEncoder
from ask_candid.retrieval.sources.schema import ElasticHitsResult
from ask_candid.retrieval.sources.issuelab import IssueLabConfig, process_issuelab_hit
from ask_candid.retrieval.sources.youtube import YoutubeConfig, process_youtube_hit
from ask_candid.retrieval.sources.candid_blog import CandidBlogConfig, process_blog_hit
from ask_candid.retrieval.sources.candid_learning import CandidLearningConfig, process_learning_hit
from ask_candid.retrieval.sources.candid_help import CandidHelpConfig, process_help_hit
from ask_candid.retrieval.sources.candid_news import CandidNewsConfig, process_news_hit
from ask_candid.services.small_lm import CandidSLM
from ask_candid.base.config.connections import SEMANTIC_ELASTIC_QA, NEWS_ELASTIC
from ask_candid.base.config.data import DataIndices, ALL_INDICES
encoder = SpladeEncoder()
class RetrieverInput(BaseModel):
"""Input to the Elasticsearch retriever."""
user_input: str = Field(description="query to look up in retriever")
def build_sparse_vector_query(
query: str,
fields: Tuple[str],
inference_id: str = ".elser-2-elasticsearch"
) -> Dict[str, Any]:
"""Builds a valid Elasticsearch text expansion query payload
Parameters
----------
query : str
Search context string
fields : Tuple[str]
Semantic text field names
inference_id : str, optional
ID of model deployed in Elasticsearch, by default ".elser-2-elasticsearch"
Returns
-------
Dict[str, Any]
"""
output = []
for f in fields:
output.append({
"nested": {
"path": f"embeddings.{f}.chunks",
"query": {
"sparse_vector": {
"field": f"embeddings.{f}.chunks.vector",
"inference_id": inference_id,
"prune": True,
"query": query,
"boost": 1 / len(fields)
}
},
"inner_hits": {
"_source": False,
"size": 2,
"fields": [f"embeddings.{f}.chunks.chunk"]
}
}
})
return {"query": {"bool": {"should": output}}}
def news_query_builder(query: str) -> Dict[str, Any]:
"""Builds a valid Elasticsearch query against Candid news, simulating a token expansion.
Parameters
----------
query : str
Search context string
Returns
-------
Dict[str, Any]
"""
tokens = encoder.token_expand(query)
query = {
"_source": ["id", "link", "title", "content", "site_name"],
"query": {
"bool": {
"filter": [
{"range": {"event_date": {"gte": "now-60d/d"}}},
{"range": {"insert_date": {"gte": "now-60d/d"}}},
{"range": {"article_trust_worthiness": {"gt": 0.8}}}
],
"should": []
}
}
}
for token, score in tokens.items():
if score > 0.4:
query["query"]["bool"]["should"].append({
"multi_match": {
"query": token,
"fields": CandidNewsConfig.text_fields,
"boost": score
}
})
return query
def query_builder(query: str, indices: List[DataIndices]) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:
"""Builds Elasticsearch multi-search query payload
Parameters
----------
query : str
Search context string
indices : List[DataIndices]
Semantic index names to search over
Returns
-------
Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]
(semantic index queries, news queries)
"""
queries, news_queries = [], []
if indices is None:
indices = list(ALL_INDICES)
for index in indices:
if index == "issuelab":
q = build_sparse_vector_query(query=query, fields=IssueLabConfig.text_fields)
q["_source"] = {"excludes": ["embeddings"]}
q["size"] = 2
queries.extend([{"index": IssueLabConfig.index_name}, q])
elif index == "youtube":
q = build_sparse_vector_query(query=query, fields=YoutubeConfig.text_fields)
q["_source"] = {"excludes": ["embeddings", *YoutubeConfig.excluded_fields]}
q["size"] = 5
queries.extend([{"index": YoutubeConfig.index_name}, q])
elif index == "candid_blog":
q = build_sparse_vector_query(query=query, fields=CandidBlogConfig.text_fields)
q["_source"] = {"excludes": ["embeddings"]}
q["size"] = 5
queries.extend([{"index": CandidBlogConfig.index_name}, q])
elif index == "candid_learning":
q = build_sparse_vector_query(query=query, fields=CandidLearningConfig.text_fields)
q["_source"] = {"excludes": ["embeddings"]}
q["size"] = 5
queries.extend([{"index": CandidLearningConfig.index_name}, q])
elif index == "candid_help":
q = build_sparse_vector_query(query=query, fields=CandidHelpConfig.text_fields)
q["_source"] = {"excludes": ["embeddings"]}
q["size"] = 5
queries.extend([{"index": CandidHelpConfig.index_name}, q])
elif index == "news":
q = news_query_builder(query=query)
q["size"] = 5
news_queries.extend([{"index": CandidNewsConfig.index_name}, q])
return queries, news_queries
def multi_search(
queries: List[Dict[str, Any]],
news_queries: Optional[List[Dict[str, Any]]] = None
) -> List[ElasticHitsResult]:
"""Runs multi-search query
Parameters
----------
queries : List[Dict[str, Any]]
Pre-built multi-search query payload
Returns
-------
List[ElasticHitsResult]
"""
def _msearch_response_generator(responses: List[Dict[str, Any]]) -> Iterator[ElasticHitsResult]:
for query_group in responses:
for h in query_group.get("hits", {}).get("hits", []):
inner_hits = h.get("inner_hits", {})
if not inner_hits:
if "news" in h.get("_index"):
inner_hits = {"text": h.get("_source", {}).get("content")}
yield ElasticHitsResult(
index=h["_index"],
id=h["_id"],
score=h["_score"],
source=h["_source"],
inner_hits=inner_hits
)
results = []
if len(queries) > 0:
with Elasticsearch(
cloud_id=SEMANTIC_ELASTIC_QA.cloud_id,
api_key=SEMANTIC_ELASTIC_QA.api_key,
verify_certs=False,
request_timeout=60 * 3
) as es:
for hit in _msearch_response_generator(es.msearch(body=queries).get("responses", [])):
results.append(hit)
if news_queries is not None and len(news_queries):
with Elasticsearch(
NEWS_ELASTIC.url,
http_auth=(NEWS_ELASTIC.username, NEWS_ELASTIC.password),
timeout=60
) as es:
for hit in _msearch_response_generator(es.msearch(body=news_queries).get("responses", [])):
results.append(hit)
return results
def get_query_results(search_text: str, indices: Optional[List[str]] = None) -> List[ElasticHitsResult]:
"""Builds and executes Elasticsearch data queries from a search string.
Parameters
----------
search_text : str
Search context string
indices : Optional[List[str]], optional
Semantic index names to search over, by default None
Returns
-------
List[ElasticHitsResult]
"""
queries, news_q = query_builder(query=search_text, indices=indices)
return multi_search(queries, news_queries=news_q)
def retrieved_text(hits: Dict[str, Any]) -> str:
"""Extracts retrieved sub-texts from documents which are strong hits from semantic queries for the purpose of
re-scoring by a secondary language model.
Parameters
----------
hits : Dict[str, Any]
Returns
-------
str
"""
text = []
for _, v in hits.items():
if _ == "text":
text.append(v)
continue
for h in (v.get("hits", {}).get("hits") or []):
for _, field in h.get("fields", {}).items():
for chunk in field:
if chunk.get("chunk"):
text.extend(chunk["chunk"])
return '\n'.join(text)
def cosine_rescore(query: str, contexts: List[str]) -> List[float]:
"""Computes cosine scores between retrieved contexts and the original query to re-score results based on overall
relevance to the original query.
Parameters
----------
query : str
Search context string
contexts : List[str]
Semantic field sub-texts, order is by document retrieved from the original multi-search query.
Returns
-------
List[float]
Scores in the same order as the input document contexts
"""
nlp = CandidSLM()
X = nlp.encode([query, *contexts]).vectors
X = F.normalize(X, dim=-1, p=2.)
cosine = X[1:] @ X[:1].T
return cosine.flatten().cpu().numpy().tolist()
def reranker(
query_results: Iterable[ElasticHitsResult],
search_text: Optional[str] = None,
max_num_results: int = 5
) -> Iterator[ElasticHitsResult]:
"""Reranks Elasticsearch hits coming from multiple indices/queries which may have scores on different scales.
This will shuffle results
Parameters
----------
query_results : Iterable[ElasticHitsResult]
Yields
------
Iterator[ElasticHitsResult]
"""
results: List[ElasticHitsResult] = []
texts: List[str] = []
for _, data in groupby(query_results, key=lambda x: x.index):
data = list(data)
max_score = max(data, key=lambda x: x.score).score
min_score = min(data, key=lambda x: x.score).score
for d in data:
d.score = (d.score - min_score) / (max_score - min_score + 1e-9)
results.append(d)
if search_text:
text = retrieved_text(d.inner_hits)
texts.append(text)
if search_text and len(texts) == len(results):
# scores = cosine_rescore(search_text, texts)
scores = encoder.query_reranking(query=search_text, documents=texts)
for r, s in zip(results, scores):
r.score = s
yield from sorted(results, key=lambda x: x.score, reverse=True)[:max_num_results]
def process_hit(hit: ElasticHitsResult) -> Union[Document, None]:
"""Parse Elasticsearch hit results into data structures handled by the RAG pipeline.
Parameters
----------
hit : ElasticHitsResult
Returns
-------
Union[Document, None]
"""
if "issuelab-elser" in hit.index:
doc = process_issuelab_hit(hit)
elif "youtube" in hit.index:
doc = process_youtube_hit(hit)
elif "candid-blog" in hit.index:
doc = process_blog_hit(hit)
elif "candid-learning" in hit.index:
doc = process_learning_hit(hit)
elif "candid-help" in hit.index:
doc = process_help_hit(hit)
elif "news" in hit.index:
doc = process_news_hit(hit)
else:
doc = None
return doc
def get_reranked_results(results: List[ElasticHitsResult], search_text: Optional[str] = None) -> List[Document]:
"""Run data re-ranking and document building for tool usage.
Parameters
----------
results : List[ElasticHitsResult]
search_text : Optional[str], optional
Search context string, by default None
Returns
-------
List[Document]
"""
output = []
for r in reranker(results, search_text=search_text):
hit = process_hit(r)
if hit is not None:
output.append(hit)
return output
|