generated from LegalForceLawRAPC/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopenai.go
68 lines (61 loc) · 1.35 KB
/
gopenai.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
package gopenai
import (
"net/http"
"time"
"github.com/LegalForceLawRAPC/gopenai/constants"
"github.com/LegalForceLawRAPC/gopenai/dalle"
)
var c *http.Client
// NewClient creates a new openAI client
func NewClient() *Client {
c = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
},
}
return &Client{
basicAuth: basicAuth{
apiKey: "",
organisation: "",
},
client: c,
}
}
// Connect to the OpenAI API
func (c *Client) Connect(apiKey string, organisation string) error {
constants.SetApiKey(apiKey)
constants.SetOrgId(organisation)
c.basicAuth.apiKey = apiKey
c.basicAuth.organisation = organisation
l := &ListModels{}
err := c.Do(constants.GetOpenAIEndpoint("listModels"), &l)
if err != nil {
return err.Error
}
return nil
}
// ListModels returns a list of available models
func (c *Client) ListModels() *ListModels {
if availableModels != nil {
return availableModels
} else {
l := &ListModels{}
err := c.Do(constants.GetOpenAIEndpoint("listModels"), &l)
if err != nil {
return nil
}
availableModels = l
return l
}
}
// Dalle returns a new dalle client
func (c *Client) Dalle() *dalle.Dalle {
return &dalle.Dalle{
Client: c.client,
BasicAuth: constants.BasicAuth{
ApiKey: c.basicAuth.apiKey,
Organisation: c.basicAuth.organisation,
},
}
}