Compare commits
No commits in common. "519b4f40bbe33327ddf1c4114fbaeb43e1be0301" and "5d616dd31ee4886c7486559c4eacf6b143953131" have entirely different histories.
519b4f40bb
...
5d616dd31e
|
@ -99,11 +99,11 @@ func (b *Bot) Run() error {
|
||||||
bh.Use(b.chatTypeStatsCounter)
|
bh.Use(b.chatTypeStatsCounter)
|
||||||
|
|
||||||
// Command handlers
|
// Command handlers
|
||||||
|
bh.Handle(b.textMessageHandler, th.AnyMessageWithText())
|
||||||
bh.Handle(b.startHandler, th.CommandEqual("start"))
|
bh.Handle(b.startHandler, th.CommandEqual("start"))
|
||||||
bh.Handle(b.summarizeHandler, th.Or(th.CommandEqual("summarize"), th.CommandEqual("s")))
|
bh.Handle(b.summarizeHandler, th.Or(th.CommandEqual("summarize"), th.CommandEqual("s")))
|
||||||
bh.Handle(b.statsHandler, th.CommandEqual("stats"))
|
bh.Handle(b.statsHandler, th.CommandEqual("stats"))
|
||||||
bh.Handle(b.helpHandler, th.CommandEqual("help"))
|
bh.Handle(b.helpHandler, th.CommandEqual("help"))
|
||||||
bh.Handle(b.textMessageHandler, th.AnyMessageWithText())
|
|
||||||
|
|
||||||
bh.Start()
|
bh.Start()
|
||||||
|
|
||||||
|
|
|
@ -7,12 +7,6 @@ import (
|
||||||
|
|
||||||
const HistoryLength = 50
|
const HistoryLength = 50
|
||||||
|
|
||||||
type Message struct {
|
|
||||||
Name string
|
|
||||||
Text string
|
|
||||||
IsMe bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type MessageRingBuffer struct {
|
type MessageRingBuffer struct {
|
||||||
messages []Message
|
messages []Message
|
||||||
capacity int
|
capacity int
|
||||||
|
@ -37,6 +31,11 @@ func (b *MessageRingBuffer) GetAll() []Message {
|
||||||
return b.messages
|
return b.messages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
Name string
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) saveChatMessageToHistory(message *telego.Message) {
|
func (b *Bot) saveChatMessageToHistory(message *telego.Message) {
|
||||||
chatId := message.Chat.ID
|
chatId := message.Chat.ID
|
||||||
|
|
||||||
|
@ -56,7 +55,6 @@ func (b *Bot) saveChatMessageToHistory(message *telego.Message) {
|
||||||
b.history[chatId].Push(Message{
|
b.history[chatId].Push(Message{
|
||||||
Name: message.From.FirstName,
|
Name: message.From.FirstName,
|
||||||
Text: message.Text,
|
Text: message.Text,
|
||||||
IsMe: false,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +77,6 @@ func (b *Bot) saveBotReplyToHistory(message *telego.Message, reply string) {
|
||||||
b.history[chatId].Push(Message{
|
b.history[chatId].Push(Message{
|
||||||
Name: b.profile.Username,
|
Name: b.profile.Username,
|
||||||
Text: reply,
|
Text: reply,
|
||||||
IsMe: true,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
17
llm/llm.go
17
llm/llm.go
|
@ -37,7 +37,7 @@ func (l *LlmConnector) HandleChatMessage(text string, model string, requestConte
|
||||||
historyLength := len(requestContext.Chat.History)
|
historyLength := len(requestContext.Chat.History)
|
||||||
|
|
||||||
if historyLength > 0 {
|
if historyLength > 0 {
|
||||||
systemPrompt += "\nYou have access to last " + strconv.Itoa(historyLength) + "messages in this chat."
|
systemPrompt += "\nYou have an access to last " + strconv.Itoa(historyLength) + "messages in this chat."
|
||||||
}
|
}
|
||||||
|
|
||||||
req := openai.ChatCompletionRequest{
|
req := openai.ChatCompletionRequest{
|
||||||
|
@ -52,20 +52,9 @@ func (l *LlmConnector) HandleChatMessage(text string, model string, requestConte
|
||||||
|
|
||||||
if historyLength > 0 {
|
if historyLength > 0 {
|
||||||
for _, msg := range requestContext.Chat.History {
|
for _, msg := range requestContext.Chat.History {
|
||||||
var msgRole string
|
|
||||||
var msgText string
|
|
||||||
|
|
||||||
if msg.IsMe {
|
|
||||||
msgRole = openai.ChatMessageRoleAssistant
|
|
||||||
msgText = msg.Text
|
|
||||||
} else {
|
|
||||||
msgRole = openai.ChatMessageRoleSystem
|
|
||||||
msgText = "User " + msg.Name + " said:\n" + msg.Text
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
|
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
|
||||||
Role: msgRole,
|
Role: openai.ChatMessageRoleUser,
|
||||||
Content: msgText,
|
Content: msg.Name + ":\n\n" + quoteMessage(msg.Text),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,6 @@ type ChatContext struct {
|
||||||
type ChatMessage struct {
|
type ChatMessage struct {
|
||||||
Name string
|
Name string
|
||||||
Text string
|
Text string
|
||||||
IsMe bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c RequestContext) Prompt() string {
|
func (c RequestContext) Prompt() string {
|
||||||
|
@ -35,20 +34,14 @@ func (c RequestContext) Prompt() string {
|
||||||
|
|
||||||
prompt += "The type of chat you're in is \"" + c.Chat.Type + "\". "
|
prompt += "The type of chat you're in is \"" + c.Chat.Type + "\". "
|
||||||
|
|
||||||
if c.Chat.Type == "group" || c.Chat.Type == "supergroup" {
|
|
||||||
prompt += "Please consider that there are several users in this chat type who may discuss several unrelated " +
|
|
||||||
"topics. Try to respond only about the topic you were asked about and only to the user who asked you, " +
|
|
||||||
"but keep in mind another chat history. "
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.Chat.Title != "" {
|
if c.Chat.Title != "" {
|
||||||
prompt += "\nChat is called \"" + c.Chat.Title + "\". "
|
prompt += "Chat is called \"" + c.Chat.Title + "\". "
|
||||||
}
|
}
|
||||||
if c.Chat.Description != "" {
|
if c.Chat.Description != "" {
|
||||||
prompt += "Chat description is \"" + c.Chat.Description + "\". "
|
prompt += "Chat description is \"" + c.Chat.Description + "\". "
|
||||||
}
|
}
|
||||||
|
|
||||||
prompt += "\nProfile of the user who mentioned you in the chat:" +
|
prompt += "Profile of the user who mentioned you in the chat:" +
|
||||||
"First name: \"" + c.User.FirstName + "\"\n"
|
"First name: \"" + c.User.FirstName + "\"\n"
|
||||||
if c.User.Username != "" {
|
if c.User.Username != "" {
|
||||||
prompt += "Username: @" + c.User.Username + ".\n"
|
prompt += "Username: @" + c.User.Username + ".\n"
|
||||||
|
@ -56,9 +49,9 @@ func (c RequestContext) Prompt() string {
|
||||||
if c.User.LastName != "" {
|
if c.User.LastName != "" {
|
||||||
prompt += "Last name: \"" + c.User.LastName + "\"\n"
|
prompt += "Last name: \"" + c.User.LastName + "\"\n"
|
||||||
}
|
}
|
||||||
//if c.User.IsPremium {
|
if c.User.IsPremium {
|
||||||
// prompt += "Telegram Premium subscription: active."
|
prompt += "Telegram Premium subscription: active."
|
||||||
//}
|
}
|
||||||
|
|
||||||
return prompt
|
return prompt
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue