-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathagent.py
More file actions
166 lines (143 loc) · 6.03 KB
/
Copy pathagent.py
File metadata and controls
166 lines (143 loc) · 6.03 KB
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
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
""" Agent module for WebFetchTool"""
from trpc_agent_sdk.agents import LlmAgent
from trpc_agent_sdk.models import LLMModel
from trpc_agent_sdk.models import OpenAIModel
from trpc_agent_sdk.tools import WebFetchTool
from .config import get_model_config
from .prompts import INSTRUCTION
def _create_model() -> LLMModel:
"""Create the LLM model used by every demo agent."""
api_key, url, model_name = get_model_config()
return OpenAIModel(model_name=model_name, api_key=api_key, base_url=url)
def create_default_fetch_agent() -> LlmAgent:
"""Build the baseline webfetch agent.
The construction below pins every HTTP-shape knob explicitly so the
example doubles as inline documentation:
- ``timeout`` — lower than the 30s default so transient network
hiccups surface quickly in the demo.
- ``user_agent`` — identifies the demo on the wire so downstream
logs can tell example traffic apart from real deployments.
- ``max_content_length`` — cap returned ``content`` text so the
demo output stays readable even for long pages.
- ``max_response_bytes`` — hard byte budget on the raw wire read
(~1 MiB here) that protects decode / memory budgets before the
char cap kicks in.
- ``follow_redirects`` / ``max_redirects`` — keep the redirect
loop bounded while still handling ``http`` → ``https`` hops.
- ``block_private_network`` — default-on SSRF boundary; pinned
here to make the intent explicit.
"""
web_fetch = WebFetchTool(
timeout=10.0,
user_agent="trpc-agent-python-webfetch-example/1.0",
max_content_length=4000,
max_response_bytes=1 * 1024 * 1024,
follow_redirects=True,
max_redirects=3,
block_private_network=True,
)
return LlmAgent(
name="default_webfetch_assistant",
description="Web-reading assistant that fetches a single URL and summarises its textual content.",
model=_create_model(),
instruction=INSTRUCTION,
tools=[web_fetch],
)
def create_cached_fetch_agent() -> LlmAgent:
"""Build a webfetch agent that enables the in-process LRU cache.
Cache knobs covered:
- ``enable_cache=True`` — opt in to the URL → ``FetchResult`` LRU.
- ``cache_ttl_seconds`` — how long an entry is considered fresh.
- ``cache_max_bytes`` — total byte budget for the cache; entries
larger than this limit are silently skipped.
The demo fetches the same URL twice in a row so the second call
comes back with ``cached=true`` on the tool response.
"""
web_fetch = WebFetchTool(
timeout=10.0,
user_agent="trpc-agent-python-webfetch-example/1.0",
max_content_length=4000,
max_response_bytes=1 * 1024 * 1024,
enable_cache=True,
cache_ttl_seconds=120.0,
cache_max_bytes=2 * 1024 * 1024,
)
return LlmAgent(
name="cached_webfetch_assistant",
description=("Web-reading assistant with an in-process LRU cache so repeated "
"fetches of the same URL skip the network."),
model=_create_model(),
instruction=INSTRUCTION,
tools=[web_fetch],
)
def create_whitelist_fetch_agent() -> LlmAgent:
"""Build a webfetch agent that only permits a closed set of hosts.
Configures ``allowed_domains`` so every non-whitelisted URL is
rejected with ``BLOCKED_URL`` before the HTTP GET is issued. The
matching is subdomain-aware (``www.`` stripped), so ``python.org``
also lets ``docs.python.org`` through.
"""
web_fetch = WebFetchTool(
timeout=10.0,
user_agent="trpc-agent-python-webfetch-example/1.0",
max_content_length=4000,
allowed_domains=["python.org"],
)
return LlmAgent(
name="whitelist_webfetch_assistant",
description=("Web-reading assistant restricted to a domain whitelist; "
"off-list URLs are rejected with BLOCKED_URL."),
model=_create_model(),
instruction=INSTRUCTION,
tools=[web_fetch],
)
def create_ssrf_fetch_agent() -> LlmAgent:
"""Build a webfetch agent that focuses on the SSRF guard."""
web_fetch = WebFetchTool(
timeout=10.0,
user_agent="trpc-agent-python-webfetch-example/1.0",
max_content_length=4000,
follow_redirects=True,
max_redirects=3,
block_private_network=True,
)
return LlmAgent(
name="ssrf_webfetch_assistant",
description=("Web-reading assistant with an SSRF guard that rejects loopback / private / "
"link-local / reserved / multicast / unspecified targets on every hop."),
model=_create_model(),
instruction=INSTRUCTION,
tools=[web_fetch],
)
def create_blocklist_fetch_agent() -> LlmAgent:
"""Build a webfetch agent that rejects a named set of hosts.
Configures ``blocked_domains`` so any URL whose host matches
(subdomain-aware, ``www.`` stripped) is rejected with
``BLOCKED_URL``. Blocks are evaluated *before* the allow list, so a
host present in both lists is still rejected.
"""
web_fetch = WebFetchTool(
timeout=10.0,
user_agent="trpc-agent-python-webfetch-example/1.0",
max_content_length=4000,
blocked_domains=["example.com"],
)
return LlmAgent(
name="blocklist_webfetch_assistant",
description=("Web-reading assistant with a domain blacklist; "
"blacklisted URLs are rejected with BLOCKED_URL."),
model=_create_model(),
instruction=INSTRUCTION,
tools=[web_fetch],
)
default_fetch_agent = create_default_fetch_agent()
cached_fetch_agent = create_cached_fetch_agent()
whitelist_fetch_agent = create_whitelist_fetch_agent()
blocklist_fetch_agent = create_blocklist_fetch_agent()
ssrf_fetch_agent = create_ssrf_fetch_agent()
root_agent = default_fetch_agent