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("本轮已达到模型请求上限")
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package round
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"example.com/ueino/pkg/model"
|
||||
"example.com/ueino/pkg/tool"
|
||||
)
|
||||
|
||||
type fakeChat struct {
|
||||
responses []model.ChatMessage
|
||||
err error
|
||||
requests []model.ChatCompletionRequest
|
||||
}
|
||||
|
||||
func (c *fakeChat) ChatCompletion(_ context.Context, messages []model.ChatMessage, tools []model.ToolDefinition, onText func(string)) (*model.ChatMessage, error) {
|
||||
c.requests = append(c.requests, model.ChatCompletionRequest{Messages: slices.Clone(messages), Tools: tools})
|
||||
if c.err != nil {
|
||||
return nil, c.err
|
||||
}
|
||||
message := c.responses[0]
|
||||
c.responses = c.responses[1:]
|
||||
if text, ok := message.Content.(string); ok && onText != nil {
|
||||
onText(text)
|
||||
}
|
||||
return &message, nil
|
||||
}
|
||||
|
||||
type fakeTool struct {
|
||||
arguments []string
|
||||
err error
|
||||
}
|
||||
|
||||
func (t *fakeTool) Definition() model.ToolDefinition {
|
||||
return model.ToolDefinition{Type: "function", Function: model.ToolFunctionDefinition{Name: "get_weather"}}
|
||||
}
|
||||
func (t *fakeTool) Execute(_ context.Context, arguments string) (string, error) {
|
||||
t.arguments = append(t.arguments, arguments)
|
||||
return "天气结果", t.err
|
||||
}
|
||||
|
||||
func toolMessage(id, arguments string) model.ChatMessage {
|
||||
return model.ChatMessage{Role: "assistant", Content: "", ToolCalls: []model.ToolCall{{ID: id, Type: "function", Function: model.ToolFunction{Name: "get_weather", Arguments: arguments}}}}
|
||||
}
|
||||
func finalMessage(text string) model.ChatMessage {
|
||||
return model.ChatMessage{Role: "assistant", Content: text}
|
||||
}
|
||||
|
||||
func TestCompleteRoundTools(t *testing.T) {
|
||||
weather := &fakeTool{}
|
||||
message := toolMessage("first", `{"location":"北京"}`)
|
||||
message.ToolCalls = append(message.ToolCalls, toolMessage("second", `{"location":"上海"}`).ToolCalls...)
|
||||
chat := &fakeChat{responses: []model.ChatMessage{message, finalMessage("最终回答")}}
|
||||
|
||||
var output strings.Builder
|
||||
reply, err := Complete(context.Background(), chat, []model.ChatMessage{{Role: "user", Content: "两地天气"}}, []tool.Tool{weather}, func(text string) { output.WriteString(text) })
|
||||
if err != nil || reply != "最终回答" || output.String() != reply {
|
||||
t.Fatalf("回答 = %q,错误 = %v", reply, err)
|
||||
}
|
||||
if len(weather.arguments) != 2 || weather.arguments[0] != `{"location":"北京"}` || weather.arguments[1] != `{"location":"上海"}` {
|
||||
t.Fatalf("工具参数错误: %v", weather.arguments)
|
||||
}
|
||||
request := chat.requests[1]
|
||||
if len(request.Tools) != 1 || len(request.Messages) != 4 {
|
||||
t.Fatalf("续接请求不正确: %+v", request)
|
||||
}
|
||||
assistant := request.Messages[1]
|
||||
if assistant.Role != "assistant" || len(assistant.ToolCalls) != 2 || assistant.ToolCalls[0].ID != "first" || assistant.ToolCalls[0].Index != nil {
|
||||
t.Fatalf("模型工具调用消息不正确: %+v", assistant)
|
||||
}
|
||||
if request.Messages[2].Role != "tool" || request.Messages[2].ToolCallID != "first" || request.Messages[3].ToolCallID != "second" {
|
||||
t.Fatalf("工具结果对应关系不正确: %+v", request.Messages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompleteRoundToolErrors(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
tools []tool.Tool
|
||||
arguments string
|
||||
}{
|
||||
{name: "未知工具", arguments: `{}`},
|
||||
{name: "执行失败", tools: []tool.Tool{&fakeTool{err: errors.New("天气服务不可用")}}, arguments: `{}`},
|
||||
{name: "无效参数", tools: []tool.Tool{tool.NewWeather()}, arguments: `{`},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
chat := &fakeChat{responses: []model.ChatMessage{toolMessage("call-1", tt.arguments), finalMessage("无法查询")}}
|
||||
reply, err := Complete(context.Background(), chat, []model.ChatMessage{{Role: "user", Content: "天气"}}, tt.tools, func(string) {})
|
||||
if err != nil || reply != "无法查询" {
|
||||
t.Fatalf("回答 = %q,错误 = %v", reply, err)
|
||||
}
|
||||
result := chat.requests[1].Messages[2]
|
||||
if result.ToolCallID != "call-1" || !strings.Contains(result.Content.(string), "工具执行失败") {
|
||||
t.Fatalf("未回传工具错误: %+v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompleteRoundInterrupted(t *testing.T) {
|
||||
weather := &fakeTool{}
|
||||
chat := &fakeChat{err: io.ErrUnexpectedEOF}
|
||||
if _, err := Complete(context.Background(), chat, []model.ChatMessage{{Role: "user", Content: "天气"}}, []tool.Tool{weather}, func(string) {}); !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
t.Fatalf("流中断错误 = %v", err)
|
||||
}
|
||||
if len(weather.arguments) != 0 {
|
||||
t.Fatal("流未完成不能执行工具")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompleteRoundLimit(t *testing.T) {
|
||||
weather := &fakeTool{}
|
||||
chat := &fakeChat{}
|
||||
for i := 0; i < 5; i++ {
|
||||
chat.responses = append(chat.responses, toolMessage("call-1", `{}`))
|
||||
}
|
||||
if _, err := Complete(context.Background(), chat, []model.ChatMessage{{Role: "user", Content: "天气"}}, []tool.Tool{weather}, func(string) {}); err == nil {
|
||||
t.Fatal("应在五次请求后报错")
|
||||
}
|
||||
if len(chat.requests) != 5 || len(weather.arguments) != 4 {
|
||||
t.Fatalf("请求或执行次数不正确: %d / %d", len(chat.requests), len(weather.arguments))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user