-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
75 lines (60 loc) · 1.52 KB
/
app.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
68
69
70
71
72
73
74
75
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) ChatGPT(querry string) (string, error) {
// Load .env
if err := godotenv.Load(); err != nil {
log.Println("Ошибка загрузки .env")
}
OpenAIKey := os.Getenv("OpenAIKey")
client := openai.NewClient(OpenAIKey)
req := openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo16K0613,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a bot who loves to help people!",
},
},
}
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: querry,
})
defer func() {
if r := recover(); r != nil {
log.Println("Ошибка, сейчас восстановлю работу бота:", r)
}
}()
resp, err := client.CreateChatCompletion(context.Background(), req)
if err != nil {
return "", fmt.Errorf("Ошибка завершения чата: %v", err)
}
answer := ""
if len(resp.Choices) > 0 {
answer = resp.Choices[0].Message.Content
}
if answer == "" {
log.Println("Введен пустой запрос...")
}
return answer, nil
}