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>
This commit is contained in:
commit
81deca8c8e
75 files changed
+7048
No files matched your search
@@ -0,0 +1,62 @@
|
||||
package round
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"example.com/ueino/pkg/model"
|
||||
"example.com/ueino/pkg/opt"
|
||||
"example.com/ueino/pkg/tool"
|
||||
)
|
||||
|
||||
// Client 只负责模型请求,不执行工具或保存记忆。
|
||||
type Client interface {
|
||||
ChatCompletion(context.Context, []model.ChatMessage, []model.ToolDefinition, func(string)) (*model.ChatMessage, error)
|
||||
}
|
||||
|
||||
// Complete 完成当前一轮的模型和工具交互,仅返回最终回答。
|
||||
func Complete(ctx context.Context, chat Client, messages []model.ChatMessage, tools []tool.Tool, onChunk func(string)) (string, error) {
|
||||
var definitions []model.ToolDefinition
|
||||
available := make(map[string]tool.Tool)
|
||||
for _, t := range tools {
|
||||
definition := t.Definition()
|
||||
definitions = append(definitions, definition)
|
||||
available[definition.Function.Name] = t
|
||||
}
|
||||
for step := 0; step < opt.MAX_TOOL_ROUND; step++ {
|
||||
assistant, err := chat.ChatCompletion(ctx, messages, definitions, onChunk)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(assistant.ToolCalls) == 0 {
|
||||
fmt.Println()
|
||||
log.Printf("[DEBUG] 无需工具调用")
|
||||
return assistant.Content.(string), nil
|
||||
}
|
||||
|
||||
if step == opt.MAX_TOOL_ROUND-1 {
|
||||
return "", fmt.Errorf("本轮已达到 %d 次模型请求上限", opt.MAX_TOOL_ROUND)
|
||||
}
|
||||
|
||||
messages = append(messages, *assistant)
|
||||
for _, call := range assistant.ToolCalls {
|
||||
fmt.Println()
|
||||
log.Printf("[DEBUG] 调用工具 %s,ID=%s,参数=%s", call.Function.Name, call.ID, call.Function.Arguments)
|
||||
result := ""
|
||||
if t, ok := available[call.Function.Name]; ok {
|
||||
result, err = t.Execute(ctx, call.Function.Arguments)
|
||||
if err != nil {
|
||||
result = "工具执行失败: " + err.Error()
|
||||
}
|
||||
} else {
|
||||
result = "工具执行失败: 未知工具 " + call.Function.Name
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
messages = append(messages, model.ChatMessage{Role: "tool", Content: result, ToolCallID: call.ID})
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("本轮已达到模型请求上限")
|
||||
}
|
||||
Reference in new issue
Block a user