generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai.go
45 lines (36 loc) · 832 Bytes
/
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
package llm
import (
"io"
"log/slog"
"strings"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
type OpenAIClient struct {
Client *openai.Client
log *slog.Logger
}
type NewOpenAIClientOptions struct {
BaseURL string
Key string
Log *slog.Logger
}
func NewOpenAIClient(opts NewOpenAIClientOptions) *OpenAIClient {
if opts.Log == nil {
opts.Log = slog.New(slog.NewTextHandler(io.Discard, nil))
}
var clientOpts []option.RequestOption
if opts.BaseURL != "" {
if !strings.HasSuffix(opts.BaseURL, "/") {
opts.BaseURL += "/"
}
clientOpts = append(clientOpts, option.WithBaseURL(opts.BaseURL))
}
if opts.Key != "" {
clientOpts = append(clientOpts, option.WithAPIKey(opts.Key))
}
return &OpenAIClient{
Client: openai.NewClient(clientOpts...),
log: opts.Log,
}
}