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,96 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"example.com/ueino/adk/internal/config"
|
||||
"example.com/ueino/pkg/model"
|
||||
)
|
||||
|
||||
func TestChatForecast(t *testing.T) {
|
||||
for _, days := range []int{1, 2} {
|
||||
t.Run(fmt.Sprintf("预报%d天", days), func(t *testing.T) {
|
||||
today := time.Now().In(time.FixedZone("Asia/Shanghai", 8*60*60))
|
||||
start, end := today.AddDate(0, 0, 1).Format(time.DateOnly), today.AddDate(0, 0, days).Format(time.DateOnly)
|
||||
previous := http.DefaultTransport
|
||||
var weatherCalls atomic.Int32
|
||||
http.DefaultTransport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||
var response any
|
||||
switch r.URL.Host {
|
||||
case "geocoding-api.open-meteo.com":
|
||||
if r.URL.Query().Get("name") != "北京" {
|
||||
t.Errorf("城市提取错误: %s", r.URL)
|
||||
}
|
||||
response = map[string]any{"results": []map[string]any{{"name": "北京", "country": "中国", "latitude": 39.9, "longitude": 116.4}}}
|
||||
case "api.open-meteo.com":
|
||||
if r.URL.Query().Get("start_date") != start || r.URL.Query().Get("end_date") != end {
|
||||
t.Errorf("日期传递错误: %s", r.URL)
|
||||
}
|
||||
dates, codes := []string{}, []int{}
|
||||
maxTemps, minTemps, rain := []int{}, []int{}, []int{}
|
||||
for i := 1; i <= days; i++ {
|
||||
dates = append(dates, today.AddDate(0, 0, i).Format(time.DateOnly))
|
||||
codes, maxTemps, minTemps, rain = append(codes, 61), append(maxTemps, 18), append(minTemps, 8), append(rain, 70)
|
||||
}
|
||||
response = map[string]any{"daily": map[string]any{"time": dates, "weather_code": codes, "temperature_2m_max": maxTemps, "temperature_2m_min": minTemps, "precipitation_probability_max": rain}}
|
||||
default:
|
||||
return previous.RoundTrip(r)
|
||||
}
|
||||
weatherCalls.Add(1)
|
||||
data, _ := json.Marshal(response)
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(string(data))), Header: make(http.Header), Request: r}, nil
|
||||
})
|
||||
t.Cleanup(func() { http.DefaultTransport = previous })
|
||||
var modelCalls atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var request model.ChatCompletionRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !strings.Contains(request.Messages[0].Content.(string), today.Format(time.DateOnly)) || !strings.Contains(request.Messages[0].Content.(string), "Asia/Shanghai") {
|
||||
t.Error("系统消息缺少当前日期或时区")
|
||||
}
|
||||
properties := request.Tools[0].Function.Parameters["properties"].(map[string]any)
|
||||
if properties["start_date"] == nil || properties["end_date"] == nil {
|
||||
t.Error("Eino 工具定义缺少日期参数")
|
||||
}
|
||||
if modelCalls.Add(1) == 1 {
|
||||
arguments, _ := json.Marshal(model.WeatherArguments{Location: "北京", StartDate: start, EndDate: end})
|
||||
writeCompletion(w, request.Stream, "", []model.ToolCall{{ID: "forecast-1", Type: "function", Function: model.ToolFunction{Name: "get_weather", Arguments: string(arguments)}}})
|
||||
return
|
||||
}
|
||||
last := request.Messages[len(request.Messages)-1]
|
||||
var forecast model.WeatherForecast
|
||||
if last.Role != "tool" || last.ToolCallID != "forecast-1" {
|
||||
t.Errorf("工具消息不正确: %+v", last)
|
||||
} else if err := json.Unmarshal([]byte(last.Content.(string)), &forecast); err != nil || len(forecast.Days) != days || forecast.Days[0].Date != start || forecast.Days[0].Condition != "小雨" {
|
||||
t.Errorf("预报没有正确回传: %+v,%v", forecast, err)
|
||||
}
|
||||
writeCompletion(w, request.Stream, start+" 北京小雨,记得带伞。", nil)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
chat, err := New(ctx, config.Config{Chat: config.ModelConfig{BaseURL: server.URL, Model: "test-model"}, MaxToken: 4096})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output strings.Builder
|
||||
if err := chat.Chat(ctx, "查询北京的预报", func(s string) { output.WriteString(s) }, nil, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if modelCalls.Load() != 2 || weatherCalls.Load() != 2 || !strings.Contains(output.String(), start) || !strings.Contains(output.String(), "小雨") {
|
||||
t.Fatalf("模型调用=%d,天气请求=%d,输出=%q", modelCalls.Load(), weatherCalls.Load(), output.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user