-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchatGpt.go
41 lines (38 loc) · 962 Bytes
/
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
package main
import (
"strings"
gogpt "github.com/sashabaranov/go-gpt3"
)
func requetOpenAI(chatGpt *ChatGpt, curr Session, input string) {
chatGpt.Lock()
chatGpt.routine = true
inp := curr.Msg
chatGpt.Unlock()
req := gogpt.CompletionRequest{
Model: chatGpt.Model,
MaxTokens: int(chatGpt.MaxTokens),
Prompt: inp,
Temperature: float32(chatGpt.Temperature),
TopP: float32(chatGpt.TopP),
FrequencyPenalty: float32(chatGpt.FrequencyPenalty),
PresencePenalty: float32(chatGpt.PresencePenalty),
}
resp, err := chatGpt.c.CreateCompletion(chatGpt.ctx, req)
if err != nil {
chatGpt.Lock()
chatGpt.rep = err.Error()
chatGpt.Unlock()
return
}
if len(resp.Choices) > 0 {
chatGpt.Lock()
ret := strings.TrimPrefix(resp.Choices[0].Text, "\n")
chatGpt.history = append(chatGpt.history, ret)
if ret == "" {
chatGpt.rep = "?"
} else {
chatGpt.rep = ret
}
chatGpt.Unlock()
}
}