File size: 1,611 Bytes
8a2dcce | 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 | """
Comparison agent.
Handles queries like "difference between BFS and DFS" — retrieves chunks
per-topic (rather than pooled) so the prompt builder can present each
topic's relevant sections side by side.
"""
from logs.logger import get_logger
logger = get_logger(__name__)
def gather_comparison_context(topics: list, retriever, top_k_per_topic: int = None) -> dict:
"""
Args:
topics: canonical topic names, e.g. ["Breadth-First Search", "Depth-First Search"].
retriever: a rag.retriever.Retriever instance.
top_k_per_topic: chunks to retrieve per topic (defaults to retriever/config default).
Returns:
{
"Breadth-First Search": [chunk, chunk, ...],
"Depth-First Search": [chunk, chunk, ...],
}
A topic with no chunks clearing the similarity threshold maps to [].
"""
context = retriever.retrieve_topics(topics, top_k_per_topic=top_k_per_topic)
for topic, chunks in context.items():
if not chunks:
logger.warning(
"Comparison agent: no chunks passed the similarity threshold for topic %r",
topic,
)
return context
def has_sufficient_context(comparison_context: dict) -> bool:
"""
True only if every topic in the comparison has at least one retrieved
chunk. If any topic comes back empty, the caller should fall back to
the "insufficient information" response rather than comparing a topic
it knows nothing about against one it does.
"""
return all(len(chunks) > 0 for chunks in comparison_context.values())
|