-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcomplete_test.go
120 lines (99 loc) · 3.05 KB
/
complete_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
package anthropic_test
import (
"context"
"encoding/json"
"net/http"
"strconv"
"testing"
"time"
"github.com/liushuangls/go-anthropic/v2"
"github.com/liushuangls/go-anthropic/v2/internal/test"
)
func TestComplete(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler("/v1/complete", handleCompleteEndpoint)
ts := server.AnthropicTestServer()
ts.Start()
defer ts.Close()
baseUrl := ts.URL + "/v1"
t.Run("create complete success", func(t *testing.T) {
client := anthropic.NewClient(test.GetTestToken(), anthropic.WithBaseURL(baseUrl))
resp, err := client.CreateComplete(context.Background(), anthropic.CompleteRequest{
Model: anthropic.ModelClaude3Haiku20240307,
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
MaxTokensToSample: 1000,
})
if err != nil {
t.Fatalf("CreateComplete error: %v", err)
}
t.Logf("Create Complete resp: %+v", resp)
})
t.Run("create complete failure", func(t *testing.T) {
client := anthropic.NewClient("invalid token", anthropic.WithBaseURL(baseUrl))
_, err := client.CreateComplete(context.Background(), anthropic.CompleteRequest{
Model: anthropic.ModelClaude3Haiku20240307,
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
MaxTokensToSample: 1000,
})
if err == nil {
t.Fatalf("CreateComplete expected error, got nil")
}
})
}
func TestSetTemperature(t *testing.T) {
cr := anthropic.CompleteRequest{
Model: anthropic.ModelClaude3Haiku20240307,
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
MaxTokensToSample: 1000,
}
temp := float32(0.5)
cr.SetTemperature(temp)
if *cr.Temperature != temp {
t.Fatalf("SetTemperature failed: %v", cr.Temperature)
}
}
func TestSetTopP(t *testing.T) {
cr := anthropic.CompleteRequest{
Model: anthropic.ModelClaude3Haiku20240307,
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
MaxTokensToSample: 1000,
}
topP := float32(0.5)
cr.SetTopP(topP)
if *cr.TopP != topP {
t.Fatalf("SetTopP failed: %v", cr.TopP)
}
}
func TestSetTopK(t *testing.T) {
cr := anthropic.CompleteRequest{
Model: anthropic.ModelClaude3Haiku20240307,
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
MaxTokensToSample: 1000,
}
topK := 5
cr.SetTopK(topK)
if *cr.TopK != topK {
t.Fatalf("SetTopK failed: %v", cr.TopK)
}
}
func handleCompleteEndpoint(w http.ResponseWriter, r *http.Request) {
var err error
var resBytes []byte
// completions only accepts POST requests
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
var completeReq anthropic.CompleteRequest
if completeReq, err = getRequest[anthropic.CompleteRequest](r); err != nil {
http.Error(w, "could not read request", http.StatusInternalServerError)
return
}
res := anthropic.CompleteResponse{
Type: "completion",
ID: strconv.Itoa(int(time.Now().Unix())),
Completion: "hello",
Model: completeReq.Model,
}
resBytes, _ = json.Marshal(res)
_, _ = w.Write(resBytes)
}