schema.ToolInfo 是 Eino 框架中用于描述工具元信息的结构体,大模型本身并不知道系统内有哪些可用工具,ToolInfo 的作用就是向大模型提供工具的完整说明:包含工具名称、功能描述、入参结构(JSON Schema),让大模型理解该工具的用途与调用规范;同时 Eino 框架也会依靠这份信息,完成工具匹配、参数校验与路由分发。
// CalorieArgs结构体tag自动生成schema
params, err := utils.GoStruct2ParamsOneOf[CalorieArgs]()
toolInfo := schema.ToolInfo{
Name: "估算每日热量",
Desc: "根据用户年龄、身高、体重、性别、活动系数,计算用户每日所需热量",
ParamsOneOf: params, // 直接拿到ParamsOneOf,不用手动New
}
有以下两种方式:
方式一:手动构造
func (c *CalorieTool) Info(ctx context.Context) (*schema.ToolInfo, error) {
js := &jsonschema.Schema{
Type: "object",
Required: []string{"age", "height", "weight", "gender", "activity_rate"},
Properties: orderedmap.New[string, *jsonschema.Schema](
orderedmap.WithInitialData[string, *jsonschema.Schema](
orderedmap.Pair[string, *jsonschema.Schema]{
Key: "age",
Value: &jsonschema.Schema{
Type: "integer",
Description: "用户年龄",
},
},
orderedmap.Pair[string, *jsonschema.Schema]{
Key: "height",
Value: &jsonschema.Schema{
Type: "number",
Description: "身高,单位cm",
},
},
orderedmap.Pair[string, *jsonschema.Schema]{
Key: "weight",
Value: &jsonschema.Schema{
Type: "number",
Description: "体重,单位kg",
},
},
orderedmap.Pair[string, *jsonschema.Schema]{
Key: "gender",
Value: &jsonschema.Schema{
Type: "string",
Description: "性别:男 / 女",
},
},
orderedmap.Pair[string, *jsonschema.Schema]{
Key: "activity_rate",
Value: &jsonschema.Schema{
Type: "number",
Description: "活动系数",
},
},
),
),
}
toolInfo := schema.ToolInfo{
Name: "估算每日热量",
Desc: "根据用户年龄、身高、体重、性别、活动系数,计算用户每日所需热量",
ParamsOneOf: schema.NewParamsOneOfByJSONSchema(js),
}
return &toolInfo, nil
}
方式二:从 Go 结构体 tag 自动反射生成 schema
type CalorieArgs struct {
Age float64 `json:"age" jsonschema:"required" jsonschema_description:"用户年龄"`
Height float64 `json:"height" jsonschema_description:"身高,单位cm"`
Weight float64 `json:"weight" jsonschema_description:"体重,单位kg"`
Gender string `json:"gender" jsonschema:"enum=男,enum=女" jsonschema_description:"性别:男 / 女"`
ActivityRate float64 `json:"activity_rate" jsonschema_description:"活动系数"`
}
func (c *CalorieTool) Info(ctx context.Context) (*schema.ToolInfo, error) {
params, err := utils.GoStruct2ParamsOneOf[CalorieArgs]()
if err != nil {
return nil, err
}
toolInfo := schema.ToolInfo{
Name: "估算每日热量",
Desc: "根据用户年龄、身高、体重、性别、活动系数,计算用户每日所需热量",
//ParamsOneOf: schema.NewParamsOneOfByJSONSchema(js),
ParamsOneOf: params,
}
return &toolInfo, nil
}