-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai.go
47 lines (39 loc) · 803 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
46
47
package main
import (
"context"
openai "github.com/sashabaranov/go-openai"
)
var _ MessageClient = (*OpenAI)(nil)
type OpenAI struct {
apiKey string
model string
}
func NewOpenAI(apiKey, model string) *OpenAI {
return &OpenAI{
apiKey,
model,
}
}
func (o *OpenAI) CreateMessage(ctx context.Context, system string, prompt string) (string, error) {
client := openai.NewClient(o.apiKey)
resp, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: o.model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: system,
},
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
},
)
if err != nil {
return "", err
}
return resp.Choices[0].Message.Content, nil
}