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
@@ -23,15 +23,25 @@ func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { r
|
||||
func TestChatWeatherAndHistory(t *testing.T) {
|
||||
previous := http.DefaultTransport
|
||||
var weatherCalls atomic.Int32
|
||||
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 previous.RoundTrip(r)
|
||||
}
|
||||
weatherCalls.Add(1)
|
||||
if r.URL.Path != "/北京" {
|
||||
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 = previous })
|
||||
var step atomic.Int32
|
||||
@@ -65,13 +75,13 @@ data: [DONE]
|
||||
if err := chat.Chat(ctx, "北京天气", func(text string) { output.WriteString(text) }, nil, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "北京晴天") || strings.Contains(output.String(), "+20°C") {
|
||||
if !strings.Contains(output.String(), "北京晴天") || strings.Contains(output.String(), "temperature_2m_max") {
|
||||
t.Fatalf("终端输出 = %q", output.String())
|
||||
}
|
||||
if err := chat.Chat(ctx, "继续", func(string) {}, nil, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if step.Load() != 3 || weatherCalls.Load() != 1 {
|
||||
if step.Load() != 3 || weatherCalls.Load() != 2 {
|
||||
t.Fatalf("请求次数:模型 %d,天气 %d", step.Load(), weatherCalls.Load())
|
||||
}
|
||||
first, second, third := <-requests, <-requests, <-requests
|
||||
@@ -79,9 +89,13 @@ data: [DONE]
|
||||
t.Fatalf("工具定义 = %+v", first.Tools)
|
||||
}
|
||||
last := second.Messages[len(second.Messages)-1]
|
||||
if last.Role != "tool" || last.ToolCallID != "weather-1" || !strings.Contains(last.Content.(string), "北京: 晴 +20°C") {
|
||||
if last.Role != "tool" || last.ToolCallID != "weather-1" {
|
||||
t.Fatalf("工具结果 = %+v", last)
|
||||
}
|
||||
var forecast model.WeatherForecast
|
||||
if err := json.Unmarshal([]byte(last.Content.(string)), &forecast); err != nil || len(forecast.Days) != 1 || forecast.Days[0].Date != today || forecast.Days[0].Condition != "晴" {
|
||||
t.Fatalf("工具预报结果不正确: %+v,%v", forecast, err)
|
||||
}
|
||||
if len(third.Messages) != 4 || third.Messages[1].Content != "北京天气" || third.Messages[2].Content != "北京晴天" || third.Messages[3].Content != "继续" {
|
||||
t.Fatalf("对话历史 = %+v", third.Messages)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func timedInstruction(instruction string, now time.Time) string {
|
||||
明天:%s;本周六:%s;本周日:%s。
|
||||
查询天气时将相对时间换算为 YYYY-MM-DD,日期参数只能是一个完整日期,不要把多个日期拼进同一个字段。
|
||||
查询本周末两天时,日期参数示例:{"start_date":"%s","end_date":"%s"},location 使用用户指定的地点。日期存在歧义时先询问。
|
||||
未提供查询地点且历史中也没有时,先询问,不要猜测。收到预报后直接根据结果回答,不重复查询相同日期;无法获取时如实说明,不用当前天气代替目标日期预报。`,
|
||||
未提供查询地点且历史中也没有时,先询问,不要猜测。收到预报后直接根据结果回答,不重复查询相同日期;无法获取时如实说明,不用今天的预报代替其他目标日期的预报。`,
|
||||
now.Format(time.DateOnly), weekdays[now.Weekday()], now.AddDate(0, 0, 1).Format(time.DateOnly),
|
||||
saturday.Format(time.DateOnly), saturday.AddDate(0, 0, 1).Format(time.DateOnly),
|
||||
saturday.Format(time.DateOnly), saturday.AddDate(0, 0, 1).Format(time.DateOnly))
|
||||
|
||||
Reference in new issue
Block a user