File size: 4,663 Bytes
3193174
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Agent with web_search tool.

Demonstrates the WebSearchTool for searching the internet and reading web pages:
  1. Direct tool usage (no LLM) β€” quick search, deep search, URL read
  2. Agent with web search
  3. Agent with deep search (fetch_content=True)

Configure your LLM via environment variables:
    LLM_API_KEY, LLM_BASE_URL, LLM_MODEL

Run:
    python -m examples.web_search_example
"""

import os

from builder import GraphBuilder
from execution import MACPRunner
from tools import WebSearchTool, create_openai_caller, get_registry

# ── Helpers ──────────────────────────────────────────────────────────────────


def _setup_tools() -> None:
    get_registry().register(WebSearchTool(max_results=3, max_content_length=2000, fetch_content=False, timeout=15))


def _create_llm():
    return create_openai_caller(
        base_url=os.getenv("LLM_BASE_URL", "http://localhost:8000/v1"),
        api_key=os.getenv("LLM_API_KEY", "your-api-key"),
        model=os.getenv("LLM_MODEL", "gpt-4o-mini"),
        temperature=0.1,
    )


def _header(title: str) -> None:
    print(f"\n{'─' * 60}\n  {title}\n{'─' * 60}")


# ── Example 1: Direct tool usage ────────────────────────────────────────────


def example_direct_usage():
    """Three WebSearchTool modes without any LLM."""
    _header("1 Β· Direct WebSearchTool Usage (no LLM)")

    # Quick search
    print("\n  a) Quick search (titles + snippets):")
    result = WebSearchTool(max_results=3, fetch_content=False).execute(query="Python programming")
    print(f"  {result.output[:400]}" if result.success else f"  Failed: {result.error}")

    # Deep search
    print("\n  b) Deep search (full page content):")
    result = WebSearchTool(max_results=2, fetch_content=True, max_content_length=1000).execute(query="Python asyncio")
    print(f"  {result.output[:400]}" if result.success else f"  Failed: {result.error}")

    # Direct URL
    print("\n  c) Read specific URL:")
    result = WebSearchTool().execute(url="https://httpbin.org/html")
    print(f"  {result.output[:300]}" if result.success else f"  Failed: {result.error}")


# ── Example 2: Agent with web search ────────────────────────────────────────


def example_agent_search():
    """Agent searches the web and summarises results."""
    _header("2 Β· Agent with Web Search")

    builder = GraphBuilder()
    builder.add_agent(
        agent_id="researcher",
        display_name="Web Researcher",
        persona="a research assistant",
        description="Searches the web for current information.",
        tools=["web_search"],
    )
    builder.add_task(query="Search for 'Python asyncio tutorial' and summarise the key concepts")
    builder.connect_task_to_agents(agent_ids=["researcher"])

    result = MACPRunner(llm_caller=_create_llm()).run_round(builder.build())
    print(f"  Result: {result.final_answer}")


# ── Example 3: Agent with deep search ───────────────────────────────────────


def example_agent_deep_search():
    """Agent searches with fetch_content=True to read full pages."""
    _header("3 Β· Agent with Deep Search")

    # Re-register with fetch_content enabled
    get_registry().register(WebSearchTool(max_results=2, fetch_content=True, max_content_length=2000, timeout=15))

    builder = GraphBuilder()
    builder.add_agent(
        agent_id="deep_researcher",
        display_name="Deep Researcher",
        persona="a thorough researcher",
        description="Searches the web and reads full page content.",
        tools=["web_search"],
    )
    builder.add_task(query="Search for 'FastAPI tutorial' and read the full content")
    builder.connect_task_to_agents(agent_ids=["deep_researcher"])

    result = MACPRunner(llm_caller=_create_llm()).run_round(builder.build())
    print(f"  Result: {result.final_answer}")


# ── Main ─────────────────────────────────────────────────────────────────────


def main():
    _setup_tools()

    example_direct_usage()
    example_agent_search()
    example_agent_deep_search()

    print(f"\n{'=' * 60}")
    print("All web search examples completed βœ…")


if __name__ == "__main__":
    main()