feat: add weather tools to legacy and Eino chat
This commit is contained in:
1 parent
6e6171f481
commit
86e86bc9fa
18 files changed
+926
-77
No files matched your search
@@ -0,0 +1,87 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"example.com/ueino/pkg/model"
|
||||
)
|
||||
|
||||
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||
|
||||
func TestChatWeatherAndHistory(t *testing.T) {
|
||||
previous := http.DefaultTransport
|
||||
var weatherCalls atomic.Int32
|
||||
http.DefaultTransport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||
if r.URL.Host != "wttr.in" {
|
||||
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
|
||||
})
|
||||
t.Cleanup(func() { http.DefaultTransport = previous })
|
||||
var step atomic.Int32
|
||||
requests := make(chan model.ChatCompletionRequest, 3)
|
||||
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)
|
||||
}
|
||||
requests <- request
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
switch step.Add(1) {
|
||||
case 1:
|
||||
fmt.Fprint(w, `data: {"choices":[{"index":0,"delta":{"role":"assistant","content":"查询中","tool_calls":[{"index":0,"id":"weather-1","type":"function","function":{"name":"get_weather","arguments":"{\"location\":\"北京\"}"}}]},"finish_reason":"tool_calls"}]}
|
||||
|
||||
data: [DONE]
|
||||
|
||||
`)
|
||||
default:
|
||||
fmt.Fprint(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"北京晴天\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
chat, err := New(ctx, Config{BaseURL: server.URL, Model: "test-model", APIKey: "test-key", MaxToken: 4096})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output strings.Builder
|
||||
if err := chat.Chat(ctx, "北京天气", func(text string) { output.WriteString(text) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "北京晴天") || strings.Contains(output.String(), "+20°C") {
|
||||
t.Fatalf("终端输出 = %q", output.String())
|
||||
}
|
||||
if err := chat.Chat(ctx, "继续", func(string) {}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if step.Load() != 3 || weatherCalls.Load() != 1 {
|
||||
t.Fatalf("请求次数:模型 %d,天气 %d", step.Load(), weatherCalls.Load())
|
||||
}
|
||||
first, second, third := <-requests, <-requests, <-requests
|
||||
if len(first.Tools) != 1 || first.Tools[0].Function.Name != "get_weather" {
|
||||
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") {
|
||||
t.Fatalf("工具结果 = %+v", last)
|
||||
}
|
||||
if len(third.Messages) != 4 || third.Messages[1].Content != "北京天气" || third.Messages[2].Content != "北京晴天" || third.Messages[3].Content != "继续" {
|
||||
t.Fatalf("对话历史 = %+v", third.Messages)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user