forked from BasedDevelopment/aiChan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.go
121 lines (109 loc) · 4.12 KB
/
chat.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright (C) 2022 Tianyu Zhu [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog/log"
)
func chat(s *discordgo.Session, m *discordgo.MessageCreate, msg string) {
// Token
token := k.String("ai.chat.token")
var bearer = "Bearer " + token
// send API req
type req struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
Temperature float64 `json:"temperature"`
Max_tokens int `json:"max_tokens"`
Top_p float64 `json:"top_p"`
Frequency_penalty float64 `json:"frequency_penalty"`
Presence_penalty float64 `json:"presence_penalty"`
user string `json:"user"`
}
request := req{
Model: "text-davinci-003",
Prompt: msg,
Temperature: 0.9,
Max_tokens: 350,
Top_p: 1,
Frequency_penalty: 0,
Presence_penalty: 0.6,
user: m.Author.ID,
}
reqBody, err := json.Marshal(request)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal request")
if _, err := s.ChannelMessageSendReply(m.ChannelID, "Chat: http err", m.MessageReference); err != nil {
log.Error().Err(err).Msg("Chat: Error sending discord message")
}
return
}
httpReq, err := http.NewRequest("POST", "https://api.openai.com/v1/completions", bytes.NewBuffer(reqBody))
if err != nil {
log.Error().Err(err).Msg("Failed to create request")
if _, err := s.ChannelMessageSendReply(m.ChannelID, "Error", m.MessageReference); err != nil {
log.Error().Err(err).Msg("Chat: Error sending discord message")
}
return
}
httpReq.Header.Set("Authorization", bearer)
httpReq.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{}
resp, err := httpClient.Do(httpReq)
defer resp.Body.Close()
if err != nil {
log.Error().Err(err).Msg("Error sending request")
if _, err := s.ChannelMessageSendReply(m.ChannelID, "Error http", m.Reference()); err != nil {
log.Error().Err(err).Msg("Chat: Error sending discord message")
}
return
}
// read response
var result map[string]any
json.NewDecoder(resp.Body).Decode(&result)
user := m.Author.Username + "#" + m.Author.Discriminator
// Handle response
if result == nil {
respRead, _ := io.ReadAll(resp.Body)
respStr := string(respRead)
log.Warn().Str("user", user).Str("resp", respStr).Str("prompt", msg).Msg("Chat: result is nil")
if _, err := s.ChannelMessageSendReply(m.ChannelID, "Chat: results is nil", m.Reference()); err != nil {
log.Error().Err(err).Msg("Chat: Error sending discord message")
}
return
}
if result["choices"] == nil {
resultStr := fmt.Sprintf("%#v", result)
log.Warn().Str("user", user).Str("prompt", msg).Str("resp", resultStr).Msg("Chat: choices is nil")
if _, err := s.ChannelMessageSendReply(m.ChannelID, "Chat: choices is nil", m.Reference()); err != nil {
log.Error().Err(err).Msg("Chat: Error sending discord message")
}
return
}
// Send response
aiRespStr := result["choices"].([]interface{})[0].(map[string]interface{})["text"].(string)
if proceed := mod(s, m, aiRespStr); proceed == true {
log.Info().Str("user", user).Str("prompt", msg).Str("resp", aiRespStr).Msg("Chat: Success")
if _, err := s.ChannelMessageSendReply(m.ChannelID, aiRespStr, m.Reference()); err != nil {
log.Error().Err(err).Msg("Chat: Error sending discord message")
}
} else {
log.Warn().Str("user", user).Str("prompt", msg).Str("resp", aiRespStr).Msg("Chat: Flagged by mod endpoint")
}
}