-
Notifications
You must be signed in to change notification settings - Fork 14
/
complete_stream_test.go
152 lines (132 loc) · 4.35 KB
/
complete_stream_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package anthropic_test
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"testing"
"github.com/liushuangls/go-anthropic/v2"
"github.com/liushuangls/go-anthropic/v2/internal/test"
"github.com/liushuangls/go-anthropic/v2/internal/test/checks"
)
var (
testCompletionStreamContent = []string{"My", " name", " is", " Claude", "."}
)
func TestCompleteStream(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler("/v1/complete", handlerCompleteStream)
ts := server.AnthropicTestServer()
ts.Start()
defer ts.Close()
baseUrl := ts.URL + "/v1"
client := anthropic.NewClient(
test.GetTestToken(),
anthropic.WithBaseURL(baseUrl),
)
var receivedContent string
resp, err := client.CreateCompleteStream(context.Background(), anthropic.CompleteStreamRequest{
CompleteRequest: anthropic.CompleteRequest{
Model: anthropic.ModelClaude3Haiku20240307,
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
MaxTokensToSample: 1000,
},
OnCompletion: func(data anthropic.CompleteResponse) {
receivedContent += data.Completion
//t.Logf("CreateCompleteStream OnCompletion data: %+v", data)
},
OnPing: func(data anthropic.CompleteStreamPingData) {},
OnError: func(response anthropic.ErrorResponse) {},
})
if err != nil {
t.Fatalf("CreateCompleteStream error: %s", err)
}
expected := strings.Join(testCompletionStreamContent, "")
if receivedContent != expected {
t.Fatalf(
"CreateCompleteStream content not match expected: %s, got: %s",
expected,
receivedContent,
)
}
if resp.Completion != expected {
t.Fatalf(
"CreateCompleteStream content not match expected: %s, got: %s",
expected,
resp.Completion,
)
}
t.Logf("CreateCompleteStream resp: %+v", resp)
}
func TestCompleteStreamError(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler("/v1/complete", handlerCompleteStream)
ts := server.AnthropicTestServer()
ts.Start()
defer ts.Close()
baseUrl := ts.URL + "/v1"
client := anthropic.NewClient(
test.GetTestToken(),
anthropic.WithBaseURL(baseUrl),
)
var receivedContent string
param := anthropic.CompleteStreamRequest{
CompleteRequest: anthropic.CompleteRequest{
Model: anthropic.ModelClaude3Haiku20240307,
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
MaxTokensToSample: 1000,
//Temperature: &temperature,
},
OnCompletion: func(data anthropic.CompleteResponse) {
receivedContent += data.Completion
//t.Logf("CreateCompleteStream OnCompletion data: %+v", data)
},
OnPing: func(data anthropic.CompleteStreamPingData) {},
OnError: func(response anthropic.ErrorResponse) {},
}
param.SetTemperature(2)
_, err := client.CreateCompleteStream(context.Background(), param)
checks.HasError(t, err, "should error")
var e *anthropic.APIError
if !errors.As(err, &e) {
t.Fatal("should api error")
}
t.Logf("CreateCompleteStream error: %+v", err)
}
func handlerCompleteStream(w http.ResponseWriter, r *http.Request) {
request, err := getRequest[anthropic.CompleteStreamRequest](r)
if err != nil {
http.Error(w, "request error", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/event-stream")
var dataBytes []byte
dataBytes = append(dataBytes, []byte("event: ping\n")...)
dataBytes = append(dataBytes, []byte(`data: {"type": "ping"}`+"\n\n")...)
if request.Temperature != nil && *request.Temperature > 1 {
dataBytes = append(dataBytes, []byte("event: error\n")...)
dataBytes = append(
dataBytes,
[]byte(
`data: {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}`+"\n\n",
)...)
}
for _, t := range testCompletionStreamContent {
dataBytes = append(dataBytes, []byte("event: completion\n")...)
dataBytes = append(
dataBytes,
[]byte(
fmt.Sprintf(
`data: {"type":"completion","id":"compl_01GatBXF5t5K51mYzbVgRJfZ","completion":"%s","stop_reason":null,"model":"claude-3-haiku-20240307","stop":null,"log_id":"compl_01GatBXF5t5K51mYzbVgRJfZ"}`,
t,
)+"\n\n",
)...)
}
dataBytes = append(dataBytes, []byte("event: completion\n")...)
dataBytes = append(
dataBytes,
[]byte(
`data: {"type":"completion","id":"compl_01GatBXF5t5K51mYzbVgRJfZ","completion":"","stop_reason":"stop_sequence","model":"claude-3-haiku-20240307","stop":null,"log_id":"compl_01GatBXF5t5K51mYzbVgRJfZ"}`+"\n\n",
)...)
_, _ = w.Write(dataBytes)
}