Spaces:
Running
Running
File size: 14,208 Bytes
9433533 |
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 |
from typing import Literal, Any
from collections.abc import Iterator, Iterable
from itertools import groupby
import logging
from langchain_core.documents import Document
from ask_candid.base.retrieval.elastic import (
build_sparse_vector_query,
build_sparse_vector_and_text_query,
news_query_builder,
multi_search_base
)
from ask_candid.base.retrieval.sparse_lexical import SpladeEncoder
from ask_candid.base.retrieval.schemas import ElasticHitsResult
import ask_candid.base.retrieval.sources as S
from ask_candid.services.small_lm import CandidSLM
from ask_candid.base.config.connections import SEMANTIC_ELASTIC_QA, NEWS_ELASTIC
SourceNames = Literal[
"Candid Blog",
"Candid Help",
"Candid Learning",
"Candid News",
"IssueLab Research Reports",
"YouTube Training"
]
sparse_encoder = SpladeEncoder()
logging.basicConfig(format="[%(levelname)s] (%(asctime)s) :: %(message)s")
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# TODO remove
def get_context(field_name: str, hit: ElasticHitsResult, context_length: int = 1024, add_context: bool = True) -> str:
"""Pads the relevant chunk of text with context before and after
Parameters
----------
field_name : str
a field with the long text that was chunked into pieces
hit : ElasticHitsResult
context_length : int, optional
length of text to add before and after the chunk, by default 1024
add_context : bool, optional
Set to `False` to expand the text context by searching for the Elastic inner hit inside the larger document
, by default True
Returns
-------
str
longer chunks stuffed together
"""
chunks = []
# NOTE chunks have tokens, long text is a string, but may contain html which affects tokenization
long_text = hit.source.get(field_name) or ""
long_text = long_text.lower()
inner_hits_field = f"embeddings.{field_name}.chunks"
found_chunks = hit.inner_hits.get(inner_hits_field, {}) if hit.inner_hits else None
if found_chunks:
for h in found_chunks.get("hits", {}).get("hits") or []:
chunk = h.get("fields", {})[inner_hits_field][0]["chunk"][0]
# cutting the middle because we may have tokenizing artifacts there
chunk = chunk[3: -3]
if add_context:
# Find the start and end indices of the chunk in the large text
start_index = long_text.find(chunk[:20])
# Chunk is found
if start_index != -1:
end_index = start_index + len(chunk)
pre_start_index = max(0, start_index - context_length)
post_end_index = min(len(long_text), end_index + context_length)
chunks.append(long_text[pre_start_index:post_end_index])
else:
chunks.append(chunk)
return '\n\n'.join(chunks)
def generate_queries(
query: str,
sources: list[SourceNames],
news_days_ago: int = 60
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
"""Builds Elastic queries against indices which do or do not support sparse vector queries.
Parameters
----------
query : str
Text describing a user's question or a description of investigative work which requires support from Candid's
knowledge base
sources : list[SourceNames]
One or more sources of knowledge from different areas at Candid.
* Candid Blog: Blog posts from Candid staff and trusted partners intended to help those in the sector or
illuminate ongoing work
* Candid Help: Candid FAQs to help user's get started with Candid's product platform and learning resources
* Candid Learning: Training documents from Candid's subject matter experts
* Candid News: News articles and press releases about real-time activity in the philanthropic sector
* IssueLab Research Reports: Academic research reports about the social/philanthropic sector
* YouTube Training: Transcripts from video-based training seminars from Candid's subject matter experts
news_days_ago : int, optional
How many days in the past to search for news articles, if a user is asking for recent trends then this value
should be set lower >~ 10, by default 60
Returns
-------
tuple[list[dict[str, Any]], list[dict[str, Any]]]
(sparse vector queries, queries for indices which do not support sparse vectors)
"""
vector_queries = []
quasi_vector_queries = []
for source_name in sources:
if source_name == "Candid Blog":
q = build_sparse_vector_query(query=query, fields=S.CandidBlogConfig.semantic_fields)
q["_source"] = {"excludes": ["embeddings"]}
q["size"] = 5
vector_queries.extend([{"index": S.CandidBlogConfig.index_name}, q])
elif source_name == "Candid Help":
q = build_sparse_vector_query(query=query, fields=S.CandidHelpConfig.semantic_fields)
q["_source"] = {"excludes": ["embeddings"]}
q["size"] = 5
vector_queries.extend([{"index": S.CandidHelpConfig.index_name}, q])
elif source_name == "Candid Learning":
q = build_sparse_vector_query(query=query, fields=S.CandidLearningConfig.semantic_fields)
q["_source"] = {"excludes": ["embeddings"]}
q["size"] = 5
vector_queries.extend([{"index": S.CandidLearningConfig.index_name}, q])
elif source_name == "Candid News":
q = news_query_builder(
query=query,
fields=S.CandidNewsConfig.semantic_fields,
encoder=sparse_encoder,
days_ago=news_days_ago
)
q["size"] = 5
quasi_vector_queries.extend([{"index": S.CandidNewsConfig.index_name}, q])
elif source_name == "IssueLab Research Reports":
q = build_sparse_vector_query(query=query, fields=S.IssueLabConfig.semantic_fields)
q["_source"] = {"excludes": ["embeddings"]}
q["size"] = 1
vector_queries.extend([{"index": S.IssueLabConfig.index_name}, q])
elif source_name == "YouTube Training":
q = build_sparse_vector_and_text_query(
query=query,
semantic_fields=S.YoutubeConfig.semantic_fields,
text_fields=S.YoutubeConfig.text_fields,
highlight_fields=S.YoutubeConfig.highlight_fields,
excluded_fields=S.YoutubeConfig.excluded_fields
)
q["size"] = 5
vector_queries.extend([{"index": S.YoutubeConfig.index_name}, q])
return vector_queries, quasi_vector_queries
def run_search(
vector_searches: list[dict[str, Any]] | None = None,
non_vector_searches: list[dict[str, Any]] | None = None,
) -> list[ElasticHitsResult]:
def _msearch_response_generator(responses: Iterable[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 and "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,
highlight=h.get("highlight", {})
)
results = []
if vector_searches is not None and len(vector_searches) > 0:
hits = multi_search_base(queries=vector_searches, credentials=SEMANTIC_ELASTIC_QA)
for hit in _msearch_response_generator(responses=hits):
results.append(hit)
if non_vector_searches is not None and len(non_vector_searches) > 0:
hits = multi_search_base(queries=non_vector_searches, credentials=NEWS_ELASTIC)
for hit in _msearch_response_generator(responses=hits):
results.append(hit)
return results
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
"""
nlp = CandidSLM()
text = []
for _, v in hits.items():
if _ == "text":
s = nlp.summarize(v, top_k=3)
text.append(s.summary)
# 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 reranker(
query_results: Iterable[ElasticHitsResult],
search_text: str | None = 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) # noqa: PLW2901
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:
if d.inner_hits:
text = retrieved_text(d.inner_hits)
if d.highlight:
highlight_texts = []
for k,v in d.highlight.items():
v_text = '\n'.join(v)
highlight_texts.append(v_text)
text = '\n'.join(highlight_texts)
texts.append(text)
if search_text and len(texts) == len(results) and len(texts) > 1:
logger.info("Re-ranking %d retrieval results", len(results))
scores = sparse_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) -> Document:
if "issuelab-elser" in hit.index:
doc = Document(
page_content='\n\n'.join([
hit.source.get("combined_item_description", ""),
hit.source.get("description", ""),
hit.source.get("combined_issuelab_findings", ""),
get_context("content", hit, context_length=12)
]),
metadata={
"title": hit.source["title"],
"source": "IssueLab",
"source_id": hit.source["resource_id"],
"url": hit.source.get("permalink", "")
}
)
elif "youtube" in hit.index:
highlight = hit.highlight or {}
doc = Document(
page_content='\n\n'.join([
hit.source.get("title", ""),
hit.source.get("semantic_description", ""),
' '.join(highlight.get("semantic_cc_text", []))
]),
metadata={
"title": hit.source.get("title", ""),
"source": "Candid YouTube",
"source_id": hit.source['video_id'],
"url": f"https://www.youtube.com/watch?v={hit.source['video_id']}"
}
)
elif "candid-blog" in hit.index:
doc = Document(
page_content='\n\n'.join([
hit.source.get("title", ""),
hit.source.get("excerpt", ""),
get_context("content", hit, context_length=12, add_context=False),
get_context("authors_text", hit, context_length=12, add_context=False),
hit.source.get("title_summary_tags", "")
]),
metadata={
"title": hit.source.get("title", ""),
"source": "Candid Blog",
"source_id": hit.source["id"],
"url": hit.source["link"]
}
)
elif "candid-learning" in hit.index:
doc = Document(
page_content='\n\n'.join([
hit.source.get("title", ""),
hit.source.get("staff_recommendations", ""),
hit.source.get("training_topics", ""),
get_context("content", hit, context_length=12)
]),
metadata={
"title": hit.source["title"],
"source": "Candid Learning",
"source_id": hit.source["post_id"],
"url": hit.source.get("url", "")
}
)
elif "candid-help" in hit.index:
doc = Document(
page_content='\n\n'.join([
hit.source.get("combined_article_description", ""),
get_context("content", hit, context_length=12)
]),
metadata={
"title": hit.source.get("title", ""),
"source": "Candid Help",
"source_id": hit.source["id"],
"url": hit.source.get("link", "")
}
)
elif "news" in hit.index:
doc = Document(
page_content='\n\n'.join([hit.source.get("title", ""), hit.source.get("content", "")]),
metadata={
"title": hit.source.get("title", ""),
"source": hit.source.get("site_name") or "Candid News",
"source_id": hit.source["id"],
"url": hit.source.get("link", "")
}
)
else:
raise ValueError(f"Unknown source result from index {hit.index}")
return doc
|