-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfunction_tool.py
More file actions
140 lines (112 loc) · 4.18 KB
/
function_tool.py
File metadata and controls
140 lines (112 loc) · 4.18 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
#!/usr/bin/env python3
# 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.
from pydantic import BaseModel
from pydantic import Field
from trpc_agent_sdk.context import InvocationContext
from trpc_agent_sdk.tools import register_tool
# =============================================================================
# 1. Directly package functions to create tools
# =============================================================================
async def get_weather(city: str) -> dict:
"""Get weather information for the given city
Args:
city: City name, like "Beijing", "Shanghai" etc.
Returns:
A dictionary containing weather information, including temperature, weather condition and humidity
"""
# Simulate weather API call
weather_data = {
"Beijing": {
"temperature": "15°C",
"condition": "Sunny",
"humidity": "45%"
},
"Shanghai": {
"temperature": "18°C",
"condition": "Cloudy",
"humidity": "65%"
},
"Shenzhen": {
"temperature": "25°C",
"condition": "Light rain",
"humidity": "80%"
},
}
if city in weather_data:
return {"status": "success", "city": city, **weather_data[city], "last_updated": "2024-01-01T12:00:00Z"}
else:
return {
"status": "error",
"error_message": f"Not supported to query weather information for {city}",
"supported_cities": list(weather_data.keys()),
}
async def calculate(operation: str, a: float, b: float) -> float:
"""Perform basic mathematical calculations.
Args:
operation: The operation to perform (add, subtract, multiply, divide)
a: First number
b: Second number
Returns:
The result of the calculation
"""
operations = {
"add": lambda x, y: x + y,
"subtract": lambda x, y: x - y,
"multiply": lambda x, y: x * y,
"divide": lambda x, y: x / y if y != 0 else float("inf"),
}
if operation not in operations:
raise ValueError(f"Unsupported operation: {operation}")
result = operations[operation](a, b)
return result
class City(BaseModel):
"""Address information for weather query."""
city: str = Field(..., description="City name")
class Address(BaseModel):
"""Address information for weather query."""
city: City = Field(..., description="City name")
province: str = Field(..., description="Province name")
class PostalCodeInfo(BaseModel):
"""Address information for weather query."""
city: str = Field(..., description="City name")
postal_code: str = Field(..., description="Postal code")
def get_postal_code(addr: Address) -> PostalCodeInfo:
"""Get postal code for the given address."""
cities = {
"Guangdong": {
"Shenzhen": "518000",
"Guangzhou": "518001",
"Zhuhai": "518002",
},
"Jiangsu": {
"Nanjing": "320000",
"Suzhou": "320001",
}
}
return PostalCodeInfo(city=addr.city.city, postal_code=cities.get(addr.province, {}).get(addr.city.city, "Unknown"))
# =============================================================================
# 2. Use decorator to register tools
# =============================================================================
@register_tool("get_session_info")
async def get_session_info(tool_context: InvocationContext) -> dict:
"""Get current session information
Args:
tool_context: Execution context (auto-injected)
Returns:
Basic information about the current session
"""
# This should be the business logic, like:
# - Async database query user information
# - Call external API to get user status etc.
# Here we directly get the session information through tool_context.session
session = tool_context.session
return {
"status": "success",
"session_id": session.id,
"user_id": session.user_id,
"app_name": session.app_name,
}