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>
48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package tool
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"example.com/ueino/pkg/model"
|
|
"example.com/ueino/pkg/provider"
|
|
|
|
einotool "github.com/cloudwego/eino/components/tool"
|
|
"github.com/cloudwego/eino/schema"
|
|
)
|
|
|
|
// Image 根据完整提示词调用图片接口并保存结果。
|
|
type Image struct {
|
|
client *provider.Image
|
|
}
|
|
|
|
func NewImage(client *provider.Image) *Image { return &Image{client: client} }
|
|
|
|
func (i *Image) Info(context.Context) (*schema.ToolInfo, error) {
|
|
return &schema.ToolInfo{
|
|
Name: "generate_image",
|
|
Desc: "根据完整的创作提示词生成一张图片,保存到本地并返回实际路径。",
|
|
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
|
|
"prompt": {Type: schema.String, Required: true, Desc: "包含主体、构图、风格及用户约束的完整创作提示词"},
|
|
}),
|
|
}, nil
|
|
}
|
|
|
|
func (i *Image) InvokableRun(ctx context.Context, arguments string, _ ...einotool.Option) (string, error) {
|
|
var args model.ImageArguments
|
|
if err := json.Unmarshal([]byte(arguments), &args); err != nil {
|
|
return "", fmt.Errorf("生图参数无效: %w", err)
|
|
}
|
|
if strings.TrimSpace(args.Prompt) == "" {
|
|
return "", fmt.Errorf("生图提示词不能为空")
|
|
}
|
|
result, err := i.client.Generate(ctx, args.Prompt)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
data, err := json.Marshal(result)
|
|
return string(data), err
|
|
}
|