-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchat.go
237 lines (210 loc) · 5.76 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"io"
"os"
"strconv"
"time"
"github.com/meinside/openai-go"
"github.com/tectiv3/awsnova-go"
"github.com/tectiv3/chatgpt-bot/i18n"
tele "gopkg.in/telebot.v3"
)
func (c *Chat) getSentMessage(context tele.Context) *tele.Message {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.MessageID != nil {
id, _ := strconv.Atoi(*c.MessageID)
return &tele.Message{ID: id, Chat: &tele.Chat{ID: c.ChatID}}
}
// if we already have a message ID, use it, otherwise create a new message
if context.Get("reply") != nil {
sentMessage := context.Get("reply").(tele.Message)
c.MessageID = &([]string{strconv.Itoa(sentMessage.ID)}[0])
return &sentMessage
}
msgPointer, _ := context.Bot().Send(context.Recipient(), "...", "text", &tele.SendOptions{ReplyTo: context.Message()})
c.MessageID = &([]string{strconv.Itoa(msgPointer.ID)}[0])
return msgPointer
}
func (c *Chat) addToolResultToDialog(id, content string) {
c.mutex.Lock()
defer c.mutex.Unlock()
msg := openai.NewChatToolMessage(id, content)
// log.Printf("Adding tool message to history: %v\n", msg)
c.History = append(c.History,
ChatMessage{
Role: msg.Role,
Content: &content,
ChatID: c.ChatID,
ToolCallID: &id,
CreatedAt: time.Now(),
})
}
func (c *Chat) addImageToDialog(text, path string) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.History = append(c.History,
ChatMessage{
Role: openai.ChatMessageRoleUser,
Content: &text,
ImagePath: &path,
ChatID: c.ChatID,
CreatedAt: time.Now(),
})
}
func (c *Chat) addMessageToDialog(msg openai.ChatMessage) {
c.mutex.Lock()
defer c.mutex.Unlock()
// log.Printf("Adding message to history: %v\n", msg)
toolCalls := make([]ToolCall, 0)
for _, tc := range msg.ToolCalls {
toolCalls = append(toolCalls, ToolCall{
ID: tc.ID,
Type: tc.Type,
Function: tc.Function,
})
}
content, err := msg.ContentString()
if err != nil {
if contentArr, err := msg.ContentArray(); err == nil {
for _, c := range contentArr {
if c.Type == "text" {
content = *c.Text
break
}
//if c.Type == "image_url" {
//
//}
}
}
}
c.History = append(c.History,
ChatMessage{
Role: msg.Role,
Content: &content,
ToolCalls: toolCalls,
ChatID: c.ChatID,
CreatedAt: time.Now(),
})
}
func (c *Chat) getDialog(request *string) []openai.ChatMessage {
prompt := c.MasterPrompt
if c.RoleID != nil {
prompt = c.Role.Prompt
}
system := openai.NewChatSystemMessage(prompt)
if request != nil {
c.addMessageToDialog(openai.NewChatUserMessage(*request))
}
history := []openai.ChatMessage{system}
for _, h := range c.History {
if h.CreatedAt.Before(
time.Now().AddDate(0, 0, -int(c.ConversationAge)),
) {
continue
}
var message openai.ChatMessage
if h.ImagePath != nil {
reader, err := os.Open(*h.ImagePath)
if err != nil {
Log.Warn("Error opening image file", "error=", err)
continue
}
defer reader.Close()
image, err := io.ReadAll(reader)
if err != nil {
Log.Warn("Error reading file content", "error=", err)
continue
}
content := []openai.ChatMessageContent{{Type: "text", Text: h.Content}}
content = append(content, openai.NewChatMessageContentWithBytes(image))
message = openai.ChatMessage{Role: h.Role, Content: content}
} else {
message = openai.ChatMessage{Role: h.Role, Content: h.Content}
}
if h.Role == openai.ChatMessageRoleAssistant && h.ToolCalls != nil {
message.ToolCalls = make([]openai.ToolCall, 0)
for _, tc := range h.ToolCalls {
message.ToolCalls = append(message.ToolCalls, openai.ToolCall{
ID: tc.ID,
Type: tc.Type,
Function: tc.Function,
})
}
}
if h.ToolCallID != nil {
message.ToolCallID = h.ToolCallID
}
history = append(history, message)
}
// Log.Infof("Dialog history: %v", history)
return history
}
func (c *Chat) getNovaDialog(request *string) []awsnova.Message {
c.History = append(c.History, ChatMessage{
Role: "user",
Content: request,
ChatID: c.ChatID,
CreatedAt: time.Now(),
})
history := []awsnova.Message{}
for _, h := range c.History {
if h.CreatedAt.Before(
time.Now().AddDate(0, 0, -int(c.ConversationAge)),
) {
continue
}
var message awsnova.Message
if h.ImagePath != nil {
reader, err := os.Open(*h.ImagePath)
if err != nil {
Log.Warn("Error opening image file", "error=", err)
continue
}
defer reader.Close()
image, err := io.ReadAll(reader)
if err != nil {
Log.Warn("Error reading file content", "error=", err)
continue
}
content := []awsnova.Content{{
Text: h.Content,
Image: &awsnova.Image{Format: "png", Source: struct {
Bytes string `json:"bytes"`
}{Bytes: toBase64(image)}},
}}
message = awsnova.Message{Role: string(h.Role), Content: content}
} else {
message = awsnova.Message{Role: string(h.Role), Content: []awsnova.Content{{
Text: h.Content,
}}}
}
// if h.Role == "assistant" && h.ToolCalls != nil {
// message.ToolCalls = make([]openai.ToolCall, 0)
// for _, tc := range h.ToolCalls {
// message.ToolCalls = append(message.ToolCalls, openai.ToolCall{
// ID: tc.ID,
// Type: tc.Type,
// Function: tc.Function,
// })
// }
// }
// if h.ToolCallID != nil {
// message.ToolCallID = h.ToolCallID
// }
history = append(history, message)
}
// Log.Infof("Dialog history: %v", history)
return history
}
func (c *Chat) t(key string, replacements ...*i18n.Replacements) string {
return l.GetWithLocale(c.Lang, key, replacements...)
}
func (c *Chat) removeMenu(context tele.Context) {
c.mutex.Lock()
if c.MessageID != nil {
_, _ = context.Bot().EditReplyMarkup(tele.StoredMessage{MessageID: *c.MessageID, ChatID: c.ChatID}, removeMenu)
c.MessageID = nil
}
c.mutex.Unlock()
}