Files
example-eino/pkg/tool/weather.go
T
zhaoyupeng.elqbandTRAE CLI 81deca8c8e feat: initialize Eino examples and ADK applications
Include chat, weather, image, travel, streaming ASR and text examples,
shared model initialization, documentation, and existing tests.

Co-authored-by: TRAE CLI <traecli@bytedance.com>
2026-11-19 11:08:03 +08:00

64 lines
2.3 KiB
Go

package tool
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
"example.com/ueino/pkg/model"
)
// Weather 通过 Open-Meteo 查询指定地点的逐日天气预报。
type Weather struct {
client *http.Client
geocodingURL string
forecastURL string
}
func NewWeather() *Weather {
return &Weather{
client: &http.Client{Timeout: 10 * time.Second},
geocodingURL: "https://geocoding-api.open-meteo.com/v1/search",
forecastURL: "https://api.open-meteo.com/v1/forecast",
}
}
func (w *Weather) Definition() model.ToolDefinition {
return model.ToolDefinition{
Type: "function",
Function: model.ToolFunctionDefinition{
Name: "get_weather",
Description: "查询指定地点天气。不传日期查询北京时间今天的预报;传 start_date 查询该日,end_date 可指定结束日期(含)。预报支持北京时间今天起共16天,不支持历史天气。日期必须是 YYYY-MM-DD,明天、本周末等需根据系统当前日期换算。地点可从对话获取,缺少地点时先询问,不要猜测。",
Parameters: map[string]any{
"type": "object",
"properties": map[string]any{
"location": map[string]string{"type": "string", "description": "需要查询的城市或地点,例如北京、上海"},
"start_date": map[string]string{"type": "string", "description": "预报开始日期,YYYY-MM-DD;不传则查北京时间今天的预报"},
"end_date": map[string]string{"type": "string", "description": "预报结束日期,YYYY-MM-DD,含当天;不传则只查开始日期,不能单独提供"},
},
"required": []string{"location"},
},
},
}
}
func (w *Weather) Execute(ctx context.Context, arguments string) (string, error) {
var args model.WeatherArguments
if err := json.Unmarshal([]byte(arguments), &args); err != nil {
log.Printf("[D] unmarshal weather arguments failed, err = %v, raw = %s", err, arguments)
return "", fmt.Errorf("天气参数无效: %w", err)
}
location := strings.TrimSpace(args.Location)
if location == "" {
return "", fmt.Errorf("请提供要查询的地点")
}
args.Location = location
args.StartDate = strings.TrimSpace(args.StartDate)
args.EndDate = strings.TrimSpace(args.EndDate)
return w.forecast(ctx, args)
}