-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaiChatBotOpenAI.go
44 lines (38 loc) · 1013 Bytes
/
aiChatBotOpenAI.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
package main
import (
"context"
openai "github.com/sashabaranov/go-openai"
)
type OpenAIChatBot struct {
openAIKey string
modelName string
openAIClient *openai.Client
maxTokens int
temperature float32
}
func CreateNewOpenAIChatBot(openAIKey string, modelName string, maxTokens int, temperature float64) *OpenAIChatBot {
openAIClient := openai.NewClient(openAIKey)
return &OpenAIChatBot{
openAIClient: openAIClient,
openAIKey: openAIKey,
modelName: modelName,
maxTokens: maxTokens,
temperature: float32(temperature),
}
}
func (chatBot *OpenAIChatBot) Query(messages *[]openai.ChatCompletionMessage) (string, error) {
resp, err := chatBot.openAIClient.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: chatBot.modelName,
Messages: *messages,
MaxTokens: chatBot.maxTokens,
Temperature: chatBot.temperature,
},
)
if err != nil {
return "", err
}
res := resp.Choices[0].Message.Content
return res, nil
}