feat: add progressive Eino examples and unify weather provider
This commit is contained in:
1 parent
73f6f0901e
commit
1373f0903f
18 files changed
+893
-380
No files matched your search
+22
-7
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"example.com/ueino/pkg/model"
|
||||
)
|
||||
@@ -22,15 +23,25 @@ func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { r
|
||||
func TestTerminalWeatherAndMemory(t *testing.T) {
|
||||
previousTransport := http.DefaultTransport
|
||||
weatherCalls := 0
|
||||
today := time.Now().In(time.FixedZone("Asia/Shanghai", 8*60*60)).Format(time.DateOnly)
|
||||
http.DefaultTransport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||
if r.URL.Host != "wttr.in" {
|
||||
var body string
|
||||
switch r.URL.Host {
|
||||
case "geocoding-api.open-meteo.com":
|
||||
if r.URL.Query().Get("name") != "北京" {
|
||||
t.Errorf("地点查询错误: %s", r.URL)
|
||||
}
|
||||
body = `{"results":[{"name":"北京","country":"中国","latitude":39.9,"longitude":116.4}]}`
|
||||
case "api.open-meteo.com":
|
||||
if r.URL.Query().Get("start_date") != today || r.URL.Query().Get("end_date") != today {
|
||||
t.Errorf("默认日期错误: %s", r.URL)
|
||||
}
|
||||
body = fmt.Sprintf(`{"daily":{"time":[%q],"weather_code":[0],"temperature_2m_min":[10],"temperature_2m_max":[20],"precipitation_probability_max":[0]}}`, today)
|
||||
default:
|
||||
return previousTransport.RoundTrip(r)
|
||||
}
|
||||
weatherCalls++
|
||||
if r.URL.Path != "/北京" || r.URL.Query().Get("format") != "3" {
|
||||
t.Errorf("天气请求不正确: %s", r.URL)
|
||||
}
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader("北京: 晴 +20°C")), Header: make(http.Header), Request: r}, nil
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: r}, nil
|
||||
})
|
||||
t.Cleanup(func() { http.DefaultTransport = previousTransport })
|
||||
var step atomic.Int32
|
||||
@@ -72,13 +83,17 @@ func TestTerminalWeatherAndMemory(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
run(ctx)
|
||||
if weatherCalls != 1 || step.Load() != 4 {
|
||||
if weatherCalls != 2 || step.Load() != 4 {
|
||||
t.Fatalf("调用次数不正确: 天气 %d,模型 %d", weatherCalls, step.Load())
|
||||
}
|
||||
first, second, third, fourth := <-requests, <-requests, <-requests, <-requests
|
||||
if len(first.Tools) != 1 || len(second.Messages) != 4 || second.Messages[2].ToolCalls[0].ID != "weather-1" || second.Messages[3].ToolCallID != "weather-1" || second.Messages[3].Content != "北京: 晴 +20°C" {
|
||||
if len(first.Tools) != 1 || len(second.Messages) != 4 || second.Messages[2].ToolCalls[0].ID != "weather-1" || second.Messages[3].ToolCallID != "weather-1" {
|
||||
t.Fatalf("工具调用续接不正确: %+v", second)
|
||||
}
|
||||
var forecast model.WeatherForecast
|
||||
if err := json.Unmarshal([]byte(second.Messages[3].Content.(string)), &forecast); err != nil || len(forecast.Days) != 1 || forecast.Days[0].Date != today || forecast.Days[0].Condition != "晴" {
|
||||
t.Fatalf("天气结果不正确: %+v,%v", forecast, err)
|
||||
}
|
||||
for _, request := range []model.ChatCompletionRequest{third, fourth} {
|
||||
if len(request.Messages) != 4 || request.Messages[1].Content != "北京天气" || request.Messages[2].Content != "北京晴天" {
|
||||
t.Fatalf("只应保存用户问题和最终回答: %+v", request.Messages)
|
||||
|
||||
Reference in new issue
Block a user