-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathopenai.go
87 lines (72 loc) · 2 KB
/
openai.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
package openai
import (
"net/http"
"git.stuart.fun/andrew/rester/v2"
)
const defaultBase = "https://api.openai.com/v1"
// Client holds the base rester.Client and has methods for communicating with
// OpenAI.
type Client struct {
c *rester.Client
}
type optStruct struct {
Org string
BaseURL string
APIVersion string
}
type Opt func(*optStruct)
// Withorg sets the identifier for a specific org, for the case where a user may be part of more than one organization.
func WithOrg(o string) Opt {
return func(opt *optStruct) {
opt.Org = o
}
}
// WithBaseURL sets the base URL of the API service to use. This can be used
// for alternative hosted versions of the OpenAI API, like MS Azure.
func WithBaseURL(base string) Opt {
return func(opt *optStruct) {
opt.BaseURL = base
}
}
// WithAPIVersion sets the API Version header of the API service to use. This
// can be used for alternative hosted versions of the OpenAI API, like MS
// Azure.
func WithAPIVersion(base string) Opt {
return func(opt *optStruct) {
opt.APIVersion = base
}
}
// NewClient returns an OpenAI base client with the token.
func NewClient(tok string, opts ...Opt) (*Client, error) {
var os optStruct
for _, o := range opts {
o(&os)
}
dh := rester.DefaultHeaders{
"Authorization": {"Bearer " + tok},
"Content-Type": {"application/json"},
}
if os.Org != "" {
dh["OpenAI-Organization"] = []string{os.Org}
}
if os.APIVersion != "" {
dh["OpenAI-Version"] = []string{os.APIVersion}
}
base := os.BaseURL
if base == "" {
base = defaultBase
}
c := rester.Must(rester.New(base))
c.Transport = rester.All{
dh,
rester.ResponseFunc(parseOpenAIError),
}.Wrap(http.DefaultTransport)
return &Client{c: c}, nil
}
// Usage is a record type returned from many different openai endpoints letting
// the user know how many tokens were used processing their request.
type Usage struct {
CompletionTokens int `json:"completion_tokens"`
PromptTokens int `json:"prompt_tokens"`
TotalTokens int `json:"total_tokens"`
}