This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.go
112 lines (92 loc) · 2.35 KB
/
client.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
package gpt35
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
const ModelGpt35Turbo = "gpt-3.5-turbo"
const MaxTokensGpt35Turbo = 4096
const (
RoleUser RoleType = "user"
RoleAssistant RoleType = "assistant"
RoleSystem RoleType = "system"
)
type Client struct {
client *http.Client
apiKey string
url string
}
// ClientOptions contains parameters for Client initialization
type ClientOptions struct {
// HTTP transport
Transport http.RoundTripper
// Base url of OpenAI API
URL string
}
type OptionFunc func(opts *ClientOptions) error
// WithTransport allows to override default client HTTP transport
func WithTransport(transport http.RoundTripper) OptionFunc {
return func(opts *ClientOptions) error {
if transport == nil {
return errors.New("cannot set nil as HTTP transport")
}
opts.Transport = transport
return nil
}
}
// WithURL allows to override base url of OpenAPI
func WithURL(baseURL string) OptionFunc {
return func(opts *ClientOptions) error {
opts.URL = baseURL
return nil
}
}
func defaultOptions() *ClientOptions {
return &ClientOptions{
Transport: http.DefaultTransport,
URL: "https://api.openai.com/v1/chat/completions",
}
}
// NewClient creates new OpenAI client
func NewClient(apiKey string, opts ...OptionFunc) (*Client, error) {
params := defaultOptions()
for _, opt := range opts {
if err := opt(params); err != nil {
return nil, fmt.Errorf("apply option: %w", err)
}
}
return &Client{
client: &http.Client{Transport: params.Transport},
apiKey: apiKey,
url: params.URL,
}, nil
}
// GetChat returns chat.
func (c *Client) GetChat(r *Request) (*Response, error) {
buf := bytes.NewBuffer(nil)
if err := json.NewEncoder(buf).Encode(r); err != nil {
return nil, fmt.Errorf("encode json: %w", err)
}
req, err := http.NewRequest(http.MethodPost, c.url, buf)
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.apiKey)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("http do: %w", err)
}
defer func() {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}()
var result Response
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("json decode: %w", err)
}
return &result, nil
}