forked from 0x9ef/openai-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenai.go
116 lines (104 loc) · 2.8 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
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
// Copyright (c) 2022 0x9ef. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package openai
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/go-playground/validator/v10"
)
type Engine struct {
apiKey string
apiBaseURL string
organizationId string
client *http.Client
validate *validator.Validate
n int
}
const (
defaultMaxTokens = 1024
)
// New is used to initialize engine.
func New(apiKey string) *Engine {
e := &Engine{
apiKey: apiKey,
apiBaseURL: "https://api.openai.com/v1",
client: &http.Client{},
validate: validator.New(),
}
v := validator.New()
v.SetTagName("binding")
e.validate = v
return e
}
// SetApiKey is used to set API key to access OpenAI API.
func (e *Engine) SetApiKey(apiKey string) {
e.apiKey = apiKey
}
// SetOrganizationId is used to set organization ID if user belongs to multiple organizations.
func (e *Engine) SetOrganizationId(organizationId string) {
e.organizationId = organizationId
}
func (e *Engine) newReq(ctx context.Context, method string, uri string, postType string, body io.Reader) (*http.Request, error) {
if ctx == nil {
ctx = context.Background() // prevent nil context error
}
if body == nil {
body = new(bytes.Reader) // prevent nil body error
}
req, err := http.NewRequestWithContext(ctx, method, uri, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", e.apiKey))
if len(e.organizationId) != 0 {
req.Header.Set("OpenAI-Organization", e.organizationId)
}
// Setup Content-Type depends on postType
switch {
case body != nil && postType == "json":
req.Header.Set("Content-type", "application/json")
case body != nil && postType == "formData":
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
}
return req, err
}
func (e *Engine) doReq(req *http.Request) (*http.Response, error) {
e.n++ // increment number of requests
resp, err := e.client.Do(req)
if err != nil {
return nil, err
}
// Check for valid status code
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return resp, nil
}
// If we have not-success HTTP status code, unmarshal to APIError
var apiErr APIError
if err := unmarshal(resp, &apiErr); err != nil {
return nil, err
}
if apiErr.Err.StatusCode == 0 {
// Overwrite apiErr status code if it's zero
apiErr.Err.StatusCode = resp.StatusCode
}
return resp, apiErr
}
func unmarshal(resp *http.Response, v interface{}) error {
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return err
}
return nil
}
func marshalJson(body interface{}) (io.Reader, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}