forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread_test.go
95 lines (83 loc) · 2.26 KB
/
thread_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
package openai_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
openai "github.com/odannyc/go-openai"
"github.com/odannyc/go-openai/internal/test/checks"
)
// TestThread Tests the thread endpoint of the API using the mocked server.
func TestThread(t *testing.T) {
threadID := "thread_abc123"
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler(
"/v1/threads/"+threadID,
func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
resBytes, _ := json.Marshal(openai.Thread{
ID: threadID,
Object: "thread",
CreatedAt: 1234567890,
})
fmt.Fprintln(w, string(resBytes))
case http.MethodPost:
var request openai.ThreadRequest
err := json.NewDecoder(r.Body).Decode(&request)
checks.NoError(t, err, "Decode error")
resBytes, _ := json.Marshal(openai.Thread{
ID: threadID,
Object: "thread",
CreatedAt: 1234567890,
})
fmt.Fprintln(w, string(resBytes))
case http.MethodDelete:
fmt.Fprintln(w, `{
"id": "thread_abc123",
"object": "thread.deleted",
"deleted": true
}`)
}
},
)
server.RegisterHandler(
"/v1/threads",
func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
var request openai.ModifyThreadRequest
err := json.NewDecoder(r.Body).Decode(&request)
checks.NoError(t, err, "Decode error")
resBytes, _ := json.Marshal(openai.Thread{
ID: threadID,
Object: "thread",
CreatedAt: 1234567890,
Metadata: request.Metadata,
})
fmt.Fprintln(w, string(resBytes))
}
},
)
ctx := context.Background()
_, err := client.CreateThread(ctx, openai.ThreadRequest{
Messages: []openai.ThreadMessage{
{
Role: openai.ThreadMessageRoleUser,
Content: "Hello, World!",
},
},
})
checks.NoError(t, err, "CreateThread error")
_, err = client.RetrieveThread(ctx, threadID)
checks.NoError(t, err, "RetrieveThread error")
_, err = client.ModifyThread(ctx, threadID, openai.ModifyThreadRequest{
Metadata: map[string]interface{}{
"key": "value",
},
})
checks.NoError(t, err, "ModifyThread error")
_, err = client.DeleteThread(ctx, threadID)
checks.NoError(t, err, "DeleteThread error")
}