-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathagent.py
More file actions
48 lines (36 loc) · 1.58 KB
/
Copy pathagent.py
File metadata and controls
48 lines (36 loc) · 1.58 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
# 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.
""" Weather agent with cancellation support using ClaudeAgent. """
from trpc_agent_sdk.models import OpenAIModel
from trpc_agent_sdk.server.agents.claude import ClaudeAgent
from trpc_agent_sdk.tools import FunctionTool
from .config import get_model_config
from .prompts import INSTRUCTION
from .tools import get_weather_report
def _create_model() -> OpenAIModel:
"""Create a model"""
api_key, url, model_name = get_model_config()
model = OpenAIModel(model_name=model_name, api_key=api_key, base_url=url)
return model
def create_agent() -> ClaudeAgent:
"""Create a weather query agent with cancellation support.
This agent demonstrates cooperative cancellation at various checkpoints:
- After model initialization
- During streaming response iteration
The tool has a 2-second delay to simulate a slow API call, which gives
us enough time to cancel during execution.
"""
# Create tool with simulated delay to demonstrate cancellation
weather_tool = FunctionTool(get_weather_report)
return ClaudeAgent(
name="claude_weather_agent_with_cancel",
description="A professional weather query assistant that supports cancellation at any time.",
model=_create_model(),
instruction=INSTRUCTION,
tools=[weather_tool],
enable_session=True, # Use TRPC session for history management
)
root_agent = create_agent()