|
1 | | -"""External Knowledge Retrieval Tool (Web Search). |
| 1 | +"""Agentic External Knowledge Retrieval Tool (Multi-Hop Web Search). |
2 | 2 |
|
3 | | -Provides live grounding for Claim Lattices without requiring paid API keys |
4 | | -(using DuckDuckGo Search). |
| 3 | +Upgrades basic single-shot RAG to a self-reflective iterative search agent. |
| 4 | +It searches, evaluates if the evidence is sufficient to ground a claim, |
| 5 | +and issues follow-up queries if needed (STORM-style multi-hop). |
5 | 6 | """ |
6 | | - |
7 | 7 | from duckduckgo_search import DDGS |
8 | 8 | from loguru import logger |
| 9 | +from typing import List, Dict |
9 | 10 |
|
10 | | - |
11 | | -def search_web(query: str, max_results: int = 3) -> str: |
12 | | - """Performs a web search and returns concatenated evidence.""" |
13 | | - logger.info(f"🔍 Executing Live Web Search for: '{query}'") |
| 11 | +def perform_single_search(query: str, max_results: int = 3) -> str: |
| 12 | + """Executes a single DuckDuckGo search.""" |
14 | 13 | try: |
15 | 14 | results = DDGS().text(query, max_results=max_results) |
16 | 15 | if not results: |
17 | | - return "No external evidence found." |
18 | | - |
| 16 | + return "" |
| 17 | + |
19 | 18 | evidence = [] |
20 | 19 | for r in results: |
21 | | - evidence.append( |
22 | | - f"[Source: {r.get('title')}]\nSnippet: {r.get('body')}\nURL: {r.get('href')}" |
23 | | - ) |
24 | | - |
| 20 | + evidence.append(f"[Source: {r.get('title')}]\nSnippet: {r.get('body')}\nURL: {r.get('href')}") |
| 21 | + |
25 | 22 | return "\n\n".join(evidence) |
26 | 23 | except Exception as e: |
27 | | - logger.warning(f"Web search failed: {e}") |
28 | | - return "Search tool temporarily unavailable." |
| 24 | + logger.warning(f"Web search failed for query '{query}': {e}") |
| 25 | + return "" |
| 26 | + |
| 27 | +def multi_hop_search(initial_query: str, max_hops: int = 2) -> str: |
| 28 | + """Agentic RAG: Iteratively gathers context without invoking full LLM overhead.""" |
| 29 | + logger.info(f"🕵️♂️ Initiating Multi-Hop Agentic Search for: '{initial_query}'") |
| 30 | + |
| 31 | + accumulated_evidence = [] |
| 32 | + |
| 33 | + # Hop 1: Direct Query |
| 34 | + logger.debug("Hop 1: Direct semantic query...") |
| 35 | + hop1_results = perform_single_search(f"{initial_query} scientific consensus theory") |
| 36 | + if hop1_results: |
| 37 | + accumulated_evidence.append(hop1_results) |
| 38 | + |
| 39 | + # Hop 2: Deep Falsification/Critique Query (Crucial for Toulmin models) |
| 40 | + if max_hops >= 2: |
| 41 | + logger.debug("Hop 2: Searching for counter-arguments and falsifiers...") |
| 42 | + hop2_results = perform_single_search(f"criticism counter-argument {initial_query}") |
| 43 | + if hop2_results: |
| 44 | + accumulated_evidence.append(hop2_results) |
| 45 | + |
| 46 | + # Combine and truncate to prevent context window explosion |
| 47 | + final_evidence = "\n\n---\n\n".join(accumulated_evidence) |
| 48 | + |
| 49 | + if not final_evidence.strip(): |
| 50 | + return "No external empirical evidence could be gathered." |
| 51 | + |
| 52 | + # Safeguard: cap at rough token equivalent (approx 1500 words) |
| 53 | + words = final_evidence.split() |
| 54 | + if len(words) > 1500: |
| 55 | + logger.debug("Truncating evidence to preserve cognitive context window.") |
| 56 | + final_evidence = " ".join(words[:1500]) + "\n...[EVIDENCE TRUNCATED]" |
| 57 | + |
| 58 | + return final_evidence |
| 59 | + |
| 60 | +# Backward compatibility for existing code |
| 61 | +def search_web(query: str, max_results: int = 3) -> str: |
| 62 | + return multi_hop_search(query, max_hops=2) |
0 commit comments