| """ |
| 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()) |
|
|