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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| package main
import ( "context" "fmt" "github.com/cloudwego/eino-examples/internal/logs" clc "github.com/cloudwego/eino-ext/callbacks/cozeloop" "github.com/cloudwego/eino-ext/components/model/ollama" "github.com/cloudwego/eino-ext/components/tool/duckduckgo/v2" "github.com/cloudwego/eino/callbacks" "github.com/cloudwego/eino/components/tool" "github.com/cloudwego/eino/components/tool/utils" "github.com/cloudwego/eino/compose" "github.com/cloudwego/eino/schema" "github.com/coze-dev/cozeloop-go" "os" )
func main() { cozeloopApiToken := os.Getenv("COZELOOP_API_TOKEN") cozeloopWorkspaceID := os.Getenv("COZELOOP_WORKSPACE_ID") ctx := context.Background() var handlers []callbacks.Handler if cozeloopApiToken != "" && cozeloopWorkspaceID != "" { client, err := cozeloop.NewClient( cozeloop.WithAPIBaseURL("http://localhost:8888/"), cozeloop.WithAPIToken(cozeloopApiToken), cozeloop.WithWorkspaceID(cozeloopWorkspaceID), ) if err != nil { panic(err) } defer client.Close(ctx) handlers = append(handlers, clc.NewLoopHandler(client)) } callbacks.AppendGlobalHandlers(handlers...)
updateTool, err := utils.InferTool("update_todo", "Update a todo item, eg: content,deadline...", UpdateTodoFunc) if err != nil { logs.Errorf("InferTool failed, err=%v", err) return }
searchTool, err := duckduckgo.NewTextSearchTool(ctx, &duckduckgo.Config{}) if err != nil { logs.Errorf("NewTextSearchTool failed, err=%v", err) return }
todoTools := []tool.BaseTool{ getAddTodoTool(), updateTool, &ListTodoTool{}, searchTool, }
chatModel, err := ollama.NewChatModel(ctx, &ollama.ChatModelConfig{ BaseURL: "http://localhost:11434", Model: "qwen2.5:7b", })
if err != nil { logs.Errorf("NewChatModel failed, err=%v", err) return }
toolInfos := make([]*schema.ToolInfo, 0, len(todoTools)) var info *schema.ToolInfo for _, todoTool := range todoTools { info, err = todoTool.Info(ctx) if err != nil { logs.Infof("get ToolInfo failed, err=%v", err) return } toolInfos = append(toolInfos, info) }
err = chatModel.BindTools(toolInfos) if err != nil { logs.Errorf("BindTools failed, err=%v", err) return }
todoToolsNode, err := compose.NewToolNode(ctx, &compose.ToolsNodeConfig{ Tools: todoTools, }) if err != nil { logs.Errorf("NewToolNode failed, err=%v", err) return }
chain := compose.NewChain[[]*schema.Message, []*schema.Message]() chain. AppendChatModel(chatModel, compose.WithNodeName("chat_model")). AppendToolsNode(todoToolsNode, compose.WithNodeName("tools"))
agent, err := chain.Compile(ctx) if err != nil { logs.Errorf("chain.Compile failed, err=%v", err) return }
resp, err := agent.Invoke(ctx, []*schema.Message{ { Role: schema.User, Content: "增加一个 做晚餐 的任务,要在今天下午4点开始,今天晚上20点完成,是最后时间", }, }) if err != nil { logs.Errorf("agent.Invoke failed, err=%v", err) return }
for idx, msg := range resp { logs.Infof("\n") logs.Infof("message %d: %s: %s", idx, msg.Role, msg.Content) }
resp2, err := agent.Invoke(ctx, []*schema.Message{ { Role: schema.User, Content: "我已经完成做晚餐的任务,请帮我更新。给我搜索一下武安君是谁?限制50个字", }, }) if err != nil { logs.Errorf("agent.Invoke failed, err=%v", err) return }
for idx, msg := range resp2 { logs.Infof("\n") logs.Infof("message %d: %s: %s", idx, msg.Role, msg.Content) } }
func getAddTodoTool() tool.InvokableTool { info := &schema.ToolInfo{ Name: "add_todo", Desc: "Add a todo item", ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{ "content": { Desc: "The content of the todo item", Type: schema.String, Required: true, }, "started_at": { Desc: "The started time of the todo item, in unix timestamp", Type: schema.Integer, }, "deadline": { Desc: "The deadline of the todo item, in unix timestamp", Type: schema.Integer, }, }), }
return utils.NewTool(info, AddTodoFunc) }
type ListTodoTool struct{}
func (lt *ListTodoTool) Info(_ context.Context) (*schema.ToolInfo, error) { return &schema.ToolInfo{ Name: "list_todo", Desc: "List all todo items", ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{ "finished": { Desc: "filter todo items if finished", Type: schema.Boolean, Required: false, }, }), }, nil }
type TodoUpdateParams struct { ID string `json:"id" jsonschema_description:"id of the todo"` Content *string `json:"content,omitempty" jsonschema_description:"content of the todo"` StartedAt *int64 `json:"started_at,omitempty" jsonschema_description:"start time in unix timestamp"` Deadline *int64 `json:"deadline,omitempty" jsonschema_description:"deadline of the todo in unix timestamp"` Done *bool `json:"done,omitempty" jsonschema_description:"done status"` }
type TodoAddParams struct { Content string `json:"content"` StartAt *int64 `json:"started_at,omitempty"` Deadline *int64 `json:"deadline,omitempty"` }
func (lt *ListTodoTool) InvokableRun(_ context.Context, argumentsInJSON string, _ ...tool.Option) (string, error) { logs.Infof("invoke tool list_todo: %s", argumentsInJSON) return `{"todos": [{"id": "1", "content": "在2024年12月10日之前完成Eino项目演示文稿的准备工作", "started_at": 1717401600, "deadline": 1717488000, "done": false}]}`, nil }
func AddTodoFunc(_ context.Context, params *TodoAddParams) (string, error) { logs.Infof("invoke tool add_todo: %+v", params) job := fmt.Sprintf("任务: %s 添加成功,开始时间为:%v,截止时间为:%v \n", params.Content, *params.StartAt, *params.Deadline) fmt.Println(job) return `{"msg": "add todo success"}`, nil }
func UpdateTodoFunc(_ context.Context, params *TodoUpdateParams) (string, error) { logs.Infof("invoke tool update_todo: %+v", params) fmt.Printf("任务: %s ✅", params.ID) return `{"msg": "update todo success"}`, nil }
|