更新工具链
This commit is contained in:
+45
-2
@@ -1,12 +1,22 @@
|
||||
package timeagent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"
|
||||
)
|
||||
|
||||
const ActivationPrompt = "提供当前日期、时间和常用时间范围。当用户问题包含今天、今日、明天、昨天、本周、本月、本年、最近、历史上的今天、日程安排等相对时间表达时,应先调用此工具;如果后续还需要联网搜索或查数据库,可继续调用 search 或 sql。"
|
||||
const (
|
||||
ToolName = "time"
|
||||
ActivationPrompt = "提供当前日期、时间和常用时间范围。当用户问题包含今天、今日、明天、昨天、本周、本月、本年、最近、历史上的今天、日程安排等相对时间表达时,应先调用此工具;如果后续还需要联网搜索或查数据库,可继续调用 search 或 sql。"
|
||||
)
|
||||
|
||||
type ToolArgs struct {
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
type Range struct {
|
||||
Start time.Time
|
||||
@@ -23,6 +33,39 @@ type Context struct {
|
||||
ThisYear Range
|
||||
}
|
||||
|
||||
func ToolDefinition(description string) *model.Tool {
|
||||
description = strings.TrimSpace(description)
|
||||
if description == "" {
|
||||
description = ActivationPrompt
|
||||
}
|
||||
return &model.Tool{
|
||||
Type: model.ToolTypeFunction,
|
||||
Function: &model.FunctionDefinition{
|
||||
Name: ToolName,
|
||||
Description: description,
|
||||
Parameters: map[string]any{
|
||||
"type": "object",
|
||||
"properties": map[string]any{
|
||||
"reason": map[string]any{
|
||||
"type": "string",
|
||||
"description": "调用时间工具的原因。",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func ExecuteTool(args string, now time.Time) (string, error) {
|
||||
var parsed ToolArgs
|
||||
if strings.TrimSpace(args) != "" {
|
||||
if err := json.Unmarshal([]byte(args), &parsed); err != nil {
|
||||
return "", fmt.Errorf("解析时间工具参数失败: %w", err)
|
||||
}
|
||||
}
|
||||
return BuildContext(Resolve(now), parsed.Reason), nil
|
||||
}
|
||||
|
||||
func Resolve(now time.Time) Context {
|
||||
loc := now.Location()
|
||||
today := Range{Start: startOfDay(now), End: startOfDay(now).AddDate(0, 0, 1)}
|
||||
@@ -47,7 +90,7 @@ func Resolve(now time.Time) Context {
|
||||
|
||||
func BuildContext(ctx Context, routeReason string) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("时间工具结果。后续工具必须优先使用这里的绝对日期解释用户问题中的相对时间,不要自行猜测当前日期。\n")
|
||||
b.WriteString("时间工具结果。请优先使用这里的绝对日期解释用户问题中的相对时间,不要自行猜测当前日期。\n")
|
||||
fmt.Fprintf(&b, "当前本地日期时间:%s\n", ctx.Now.Format("2006-01-02 15:04:05 MST"))
|
||||
fmt.Fprintf(&b, "今天:%s\n", FormatSQLRange(ctx.Today))
|
||||
fmt.Fprintf(&b, "明天:%s\n", FormatSQLRange(ctx.Tomorrow))
|
||||
|
||||
@@ -29,3 +29,19 @@ func TestBuildContextIncludesSQLHints(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolDefinitionAndExecuteTool(t *testing.T) {
|
||||
definition := ToolDefinition("custom description")
|
||||
if definition.Function == nil || definition.Function.Name != ToolName || definition.Function.Description != "custom description" {
|
||||
t.Fatalf("unexpected definition: %#v", definition)
|
||||
}
|
||||
text, err := ExecuteTool(`{"reason":"测试原因"}`, time.Date(2026, 6, 10, 13, 14, 15, 0, time.UTC))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"时间工具结果", "2026-06-10", "本月", "start=", "end_exclusive=", "测试原因"} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Fatalf("tool result missing %q:\n%s", want, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user