-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer_chatgpt.go
67 lines (54 loc) · 1.36 KB
/
consumer_chatgpt.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
package lowbot
import (
"context"
"github.com/google/uuid"
"github.com/sashabaranov/go-openai"
)
type ChatGPTConsumer struct {
*Consumer
model string
conn *openai.Client
}
func NewChatGPTConsumer(token string, model string) (IConsumer, error) {
if token == "" {
return nil, ERR_UNKNOWN_CHATGPT_TOKEN
}
conn := openai.NewClient(token)
if conn == nil {
return nil, ERR_CONNECT_CHATGPT
}
return &ChatGPTConsumer{
Consumer: &Consumer{
ConsumerID: uuid.New(),
Name: CONSUMER_CHATGPT_NAME,
},
conn: conn,
model: model,
}, nil
}
func (consumer *ChatGPTConsumer) GetConsumer() *Consumer {
return consumer.Consumer
}
func (consumer *ChatGPTConsumer) Run(interaction *Interaction) ([]*Interaction, error) {
resp, err := consumer.conn.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: consumer.model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: interaction.Parameters.Text,
},
},
},
)
if err != nil {
return nil, err
}
answerInteraction := NewInteractionMessageText(resp.Choices[0].Message.Content)
replier := NewWho(consumer.ConsumerID.String(), consumer.Name)
answerInteraction.SetReplier(replier)
answerInteraction.SetTo(interaction.To)
answerInteraction.SetFrom(interaction.From)
return []*Interaction{answerInteraction}, nil
}