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,124 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"example.com/ueino/pkg/model"
|
||||
)
|
||||
|
||||
func TestChatCompletionTools(t *testing.T) {
|
||||
t.Setenv("CHAT_TEST_KEY", "local-key")
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/chat/completions" || r.Header.Get("Authorization") != "Bearer local-key" {
|
||||
t.Errorf("请求不正确: %s", r.URL)
|
||||
}
|
||||
var request model.ChatCompletionRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !request.Stream || request.Model != "test-model" || len(request.Tools) != 1 || request.Tools[0].Function.Name != "get_weather" {
|
||||
t.Errorf("请求参数不正确: %+v", request)
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fmt.Fprint(w, `: heartbeat
|
||||
|
||||
data: {"choices":[{"delta":{"content":"查询中","tool_calls":[{"index":0,"id":"call-1","type":"function","function":{"name":"get_weather","arguments":"{\"location\":"}}]}}]}
|
||||
|
||||
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"北京\"}"}}]},"finish_reason":"tool_calls"}]}
|
||||
|
||||
data: {"choices":[]}
|
||||
|
||||
data: [DONE]
|
||||
|
||||
`)
|
||||
}))
|
||||
defer server.Close()
|
||||
chat := NewChat(ChatBaseURL(server.URL+"/v1/"), ChatModel("test-model"), ChatApiKeyByEnv("CHAT_TEST_KEY"))
|
||||
var output strings.Builder
|
||||
message, err := chat.ChatCompletion(context.Background(), []model.ChatMessage{{Role: "user", Content: "北京天气"}}, []model.ToolDefinition{{Type: "function", Function: model.ToolFunctionDefinition{Name: "get_weather"}}}, func(text string) { output.WriteString(text) })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if message.Role != "assistant" || message.Content != "查询中" || output.String() != "查询中" || len(message.ToolCalls) != 1 {
|
||||
t.Fatalf("助手消息不正确: %+v", message)
|
||||
}
|
||||
call := message.ToolCalls[0]
|
||||
if call.ID != "call-1" || call.Index != nil || call.Function.Name != "get_weather" || call.Function.Arguments != `{"location":"北京"}` {
|
||||
t.Fatalf("工具调用不正确: %+v", call)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestChatCompletionStreamErrors(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, body string
|
||||
wantUnexpected bool
|
||||
}{
|
||||
{name: "无效JSON", body: "data: invalid-json\n\n"},
|
||||
{name: "缺少完成标记", body: "data: {\"choices\":[{\"delta\":{\"content\":\"部分回答\"}}]}\n\n", wantUnexpected: true},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fmt.Fprint(w, tt.body)
|
||||
}))
|
||||
defer server.Close()
|
||||
chat := NewChat(ChatBaseURL(server.URL))
|
||||
message, err := chat.ChatCompletion(context.Background(), []model.ChatMessage{{Role: "user", Content: "你好"}}, nil, nil)
|
||||
if message != nil || err == nil || (tt.wantUnexpected && !errors.Is(err, io.ErrUnexpectedEOF)) {
|
||||
t.Fatalf("消息 = %+v,流错误 = %v", message, err)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCompletionHTTPError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(400); fmt.Fprint(w, "参数错误") }))
|
||||
defer server.Close()
|
||||
chat := NewChat(ChatBaseURL(server.URL))
|
||||
if _, err := chat.ChatCompletion(context.Background(), nil, nil, nil); err == nil {
|
||||
t.Fatal("空消息应报错")
|
||||
}
|
||||
if _, err := chat.ChatCompletion(context.Background(), []model.ChatMessage{{Role: "user", Content: "你好"}}, nil, nil); err == nil || !strings.Contains(err.Error(), "400") {
|
||||
t.Fatalf("请求错误 = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCompletionCancel(t *testing.T) {
|
||||
started := make(chan struct{})
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.WriteHeader(200)
|
||||
w.(http.Flusher).Flush()
|
||||
close(started)
|
||||
<-r.Context().Done()
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
result := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := NewChat(ChatBaseURL(server.URL)).ChatCompletion(ctx, []model.ChatMessage{{Role: "user", Content: "你好"}}, nil, nil)
|
||||
result <- err
|
||||
}()
|
||||
<-started
|
||||
cancel()
|
||||
select {
|
||||
case err := <-result:
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("取消错误 = %v", err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("取消后请求未结束")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user