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,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