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,70 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadStreamInterleavedTools(t *testing.T) {
|
||||
body := `data: {"choices":[{"delta":{"tool_calls":[{"index":1,"id":"second","type":"function","function":{"name":"get_","arguments":"{\"location\":\"上"}},{"index":0,"id":"first","type":"function","function":{"name":"get_weather","arguments":"{\"location\":\"北"}}]}}]}
|
||||
data: {"choices":[{"delta":{"tool_calls":[{"index":1,"function":{"name":"weather","arguments":"海\"}"}},{"index":0,"function":{"arguments":"京\"}"}}]},"finish_reason":"tool_calls"}]}
|
||||
data: [DONE]
|
||||
`
|
||||
message, err := readStream(context.Background(), strings.NewReader(body), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(message.ToolCalls) != 2 {
|
||||
t.Fatalf("工具调用 = %+v", message.ToolCalls)
|
||||
}
|
||||
for i, want := range []struct{ id, args string }{{"first", `{"location":"北京"}`}, {"second", `{"location":"上海"}`}} {
|
||||
call := message.ToolCalls[i]
|
||||
if call.ID != want.id || call.Index != nil || call.Function.Name != "get_weather" || call.Function.Arguments != want.args {
|
||||
t.Fatalf("工具调用 = %+v", call)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStreamInvalidCompletion(t *testing.T) {
|
||||
for _, choice := range []string{
|
||||
`{"delta":{"tool_calls":[{"id":"call-1"}]},"finish_reason":"tool_calls"}`,
|
||||
`{"delta":{"tool_calls":[{"index":-1}]},"finish_reason":"tool_calls"}`,
|
||||
`{"delta":{"tool_calls":[{"index":0,"type":"function","function":{"name":"get_weather"}}]},"finish_reason":"tool_calls"}`,
|
||||
`{"delta":{"tool_calls":[{"index":0,"id":"call-1","type":"function","function":{"name":"get_weather"}}]},"finish_reason":"stop"}`,
|
||||
`{"delta":{},"finish_reason":"tool_calls"}`,
|
||||
`{"delta":{"content":"部分回答"},"finish_reason":"length"}`,
|
||||
`{"delta":{"content":"部分回答"}}`,
|
||||
} {
|
||||
body := "data: {\"choices\":[" + choice + "]}\n\ndata: [DONE]\n"
|
||||
if message, err := readStream(context.Background(), strings.NewReader(body), nil); message != nil || err == nil {
|
||||
t.Fatalf("异常响应应报错: %s, %+v, %v", choice, message, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStreamLiveText(t *testing.T) {
|
||||
reader, writer := io.Pipe()
|
||||
defer reader.Close()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() {
|
||||
defer writer.Close()
|
||||
fmt.Fprint(writer, "data: {\"choices\":[{\"delta\":{\"content\":\"你好\"}}]}\n")
|
||||
// 等回调收到文字再发送结尾,证明回调不需要等待整个响应结束。
|
||||
<-ctx.Done()
|
||||
fmt.Fprint(writer, "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\ndata: [DONE]\n")
|
||||
}()
|
||||
// 使用独立上下文,cancel 只用于通知写入方。
|
||||
message, err := readStream(context.Background(), reader, func(text string) {
|
||||
if text != "你好" {
|
||||
t.Errorf("文字 = %q", text)
|
||||
}
|
||||
cancel()
|
||||
})
|
||||
if err != nil || message.Content != "你好" {
|
||||
t.Fatalf("消息 = %+v,错误 = %v", message, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user