Compare commits
No commits in common. "541c1d3bbf7d141386ba3fc2ac5630c8556a2b0a" and "61b3bd00c3582c00777dc0906ce2d284d4e2cb33" have entirely different histories.
541c1d3bbf
...
61b3bd00c3
20
bot/bot.go
20
bot/bot.go
|
@ -30,7 +30,7 @@ type Bot struct {
|
||||||
extractor *extractor.Extractor
|
extractor *extractor.Extractor
|
||||||
stats *stats.Stats
|
stats *stats.Stats
|
||||||
models ModelSelection
|
models ModelSelection
|
||||||
history map[int64]*MessageHistory
|
history map[int64]*MessageRingBuffer
|
||||||
profile BotInfo
|
profile BotInfo
|
||||||
|
|
||||||
markdownV1Replacer *strings.Replacer
|
markdownV1Replacer *strings.Replacer
|
||||||
|
@ -48,7 +48,7 @@ func NewBot(
|
||||||
extractor: extractor,
|
extractor: extractor,
|
||||||
stats: stats.NewStats(),
|
stats: stats.NewStats(),
|
||||||
models: models,
|
models: models,
|
||||||
history: make(map[int64]*MessageHistory),
|
history: make(map[int64]*MessageRingBuffer),
|
||||||
profile: BotInfo{0, "", ""},
|
profile: BotInfo{0, "", ""},
|
||||||
|
|
||||||
markdownV1Replacer: strings.NewReplacer(
|
markdownV1Replacer: strings.NewReplacer(
|
||||||
|
@ -129,7 +129,7 @@ func (b *Bot) textMessageHandler(bot *telego.Bot, update telego.Update) {
|
||||||
slog.Info("/any-message", "type", "private")
|
slog.Info("/any-message", "type", "private")
|
||||||
b.processMention(message)
|
b.processMention(message)
|
||||||
default:
|
default:
|
||||||
slog.Debug("/any-message", "info", "MessageData is not mention, reply or private chat. Skipping.")
|
slog.Debug("/any-message", "info", "Message is not mention, reply or private chat. Skipping.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,13 +144,7 @@ func (b *Bot) processMention(message *telego.Message) {
|
||||||
|
|
||||||
requestContext := b.createLlmRequestContextFromMessage(message)
|
requestContext := b.createLlmRequestContextFromMessage(message)
|
||||||
|
|
||||||
userMessageData := tgUserMessageToMessageData(message)
|
llmReply, err := b.llm.HandleChatMessage(message.Text, b.models.TextRequestModel, requestContext)
|
||||||
|
|
||||||
llmReply, err := b.llm.HandleChatMessage(
|
|
||||||
messageDataToLlmMessage(userMessageData),
|
|
||||||
b.models.TextRequestModel,
|
|
||||||
requestContext,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Cannot get reply from LLM connector")
|
slog.Error("Cannot get reply from LLM connector")
|
||||||
|
|
||||||
|
@ -233,11 +227,9 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
|
|
||||||
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
||||||
|
|
||||||
replyMarkdown := b.escapeMarkdownV1Symbols(llmReply)
|
|
||||||
|
|
||||||
message := tu.Message(
|
message := tu.Message(
|
||||||
chatID,
|
chatID,
|
||||||
replyMarkdown,
|
b.escapeMarkdownV1Symbols(llmReply),
|
||||||
).WithParseMode("Markdown")
|
).WithParseMode("Markdown")
|
||||||
|
|
||||||
_, err = bot.SendMessage(b.reply(update.Message, message))
|
_, err = bot.SendMessage(b.reply(update.Message, message))
|
||||||
|
@ -247,8 +239,6 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
b.trySendReplyError(update.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
b.saveBotReplyToHistory(update.Message, replyMarkdown)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) helpHandler(bot *telego.Bot, update telego.Update) {
|
func (b *Bot) helpHandler(bot *telego.Bot, update telego.Update) {
|
||||||
|
|
|
@ -7,28 +7,25 @@ import (
|
||||||
|
|
||||||
const HistoryLength = 150
|
const HistoryLength = 150
|
||||||
|
|
||||||
type MessageData struct {
|
type Message struct {
|
||||||
Name string
|
Name string
|
||||||
Username string
|
Text string
|
||||||
Text string
|
IsMe bool
|
||||||
IsMe bool
|
|
||||||
IsUserRequest bool
|
|
||||||
ReplyTo *MessageData
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageHistory struct {
|
type MessageRingBuffer struct {
|
||||||
messages []MessageData
|
messages []Message
|
||||||
capacity int
|
capacity int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMessageHistory(capacity int) *MessageHistory {
|
func NewMessageBuffer(capacity int) *MessageRingBuffer {
|
||||||
return &MessageHistory{
|
return &MessageRingBuffer{
|
||||||
messages: make([]MessageData, 0, capacity),
|
messages: make([]Message, 0, capacity),
|
||||||
capacity: capacity,
|
capacity: capacity,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *MessageHistory) Push(element MessageData) {
|
func (b *MessageRingBuffer) Push(element Message) {
|
||||||
if len(b.messages) >= b.capacity {
|
if len(b.messages) >= b.capacity {
|
||||||
b.messages = b.messages[1:]
|
b.messages = b.messages[1:]
|
||||||
}
|
}
|
||||||
|
@ -36,7 +33,7 @@ func (b *MessageHistory) Push(element MessageData) {
|
||||||
b.messages = append(b.messages, element)
|
b.messages = append(b.messages, element)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *MessageHistory) GetAll() []MessageData {
|
func (b *MessageRingBuffer) GetAll() []Message {
|
||||||
return b.messages
|
return b.messages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,72 +50,43 @@ func (b *Bot) saveChatMessageToHistory(message *telego.Message) {
|
||||||
|
|
||||||
_, ok := b.history[chatId]
|
_, ok := b.history[chatId]
|
||||||
if !ok {
|
if !ok {
|
||||||
b.history[chatId] = NewMessageHistory(HistoryLength)
|
b.history[chatId] = NewMessageBuffer(HistoryLength)
|
||||||
}
|
}
|
||||||
|
|
||||||
msgData := tgUserMessageToMessageData(message)
|
b.history[chatId].Push(Message{
|
||||||
|
Name: message.From.FirstName,
|
||||||
b.history[chatId].Push(msgData)
|
Text: message.Text,
|
||||||
|
IsMe: false,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) saveBotReplyToHistory(replyTo *telego.Message, text string) {
|
func (b *Bot) saveBotReplyToHistory(message *telego.Message, reply string) {
|
||||||
chatId := replyTo.Chat.ID
|
chatId := message.Chat.ID
|
||||||
|
|
||||||
slog.Info(
|
slog.Info(
|
||||||
"history-reply-save",
|
"history-reply-save",
|
||||||
"chat", chatId,
|
"chat", chatId,
|
||||||
"to_id", replyTo.From.ID,
|
"to_id", message.From.ID,
|
||||||
"to_name", replyTo.From.FirstName,
|
"to_name", message.From.FirstName,
|
||||||
"text", text,
|
"text", reply,
|
||||||
)
|
)
|
||||||
|
|
||||||
_, ok := b.history[chatId]
|
_, ok := b.history[chatId]
|
||||||
if !ok {
|
if !ok {
|
||||||
b.history[chatId] = NewMessageHistory(HistoryLength)
|
b.history[chatId] = NewMessageBuffer(HistoryLength)
|
||||||
}
|
}
|
||||||
|
|
||||||
msgData := MessageData{
|
b.history[chatId].Push(Message{
|
||||||
Name: b.profile.Name,
|
Name: b.profile.Username,
|
||||||
Username: b.profile.Username,
|
Text: reply,
|
||||||
Text: text,
|
IsMe: true,
|
||||||
IsMe: true,
|
})
|
||||||
}
|
|
||||||
|
|
||||||
if replyTo.ReplyToMessage != nil {
|
|
||||||
replyMessage := replyTo.ReplyToMessage
|
|
||||||
|
|
||||||
msgData.ReplyTo = &MessageData{
|
|
||||||
Name: replyMessage.From.FirstName,
|
|
||||||
Username: replyMessage.From.Username,
|
|
||||||
Text: replyMessage.Text,
|
|
||||||
IsMe: false,
|
|
||||||
ReplyTo: nil,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.history[chatId].Push(msgData)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func tgUserMessageToMessageData(message *telego.Message) MessageData {
|
func (b *Bot) getChatHistory(chatId int64) []Message {
|
||||||
msgData := MessageData{
|
|
||||||
Name: message.From.FirstName,
|
|
||||||
Username: message.From.Username,
|
|
||||||
Text: message.Text,
|
|
||||||
IsMe: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
if message.ReplyToMessage != nil {
|
|
||||||
replyData := tgUserMessageToMessageData(message.ReplyToMessage)
|
|
||||||
msgData.ReplyTo = &replyData
|
|
||||||
}
|
|
||||||
|
|
||||||
return msgData
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) getChatHistory(chatId int64) []MessageData {
|
|
||||||
_, ok := b.history[chatId]
|
_, ok := b.history[chatId]
|
||||||
if !ok {
|
if !ok {
|
||||||
return make([]MessageData, 0)
|
return make([]Message, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.history[chatId].GetAll()
|
return b.history[chatId].GetAll()
|
||||||
|
|
|
@ -48,14 +48,17 @@ func (b *Bot) createLlmRequestContextFromMessage(message *telego.Message) llm.Re
|
||||||
return rc
|
return rc
|
||||||
}
|
}
|
||||||
|
|
||||||
func historyToLlmMessages(history []MessageData) []llm.ChatMessage {
|
func historyToLlmMessages(history []Message) []llm.ChatMessage {
|
||||||
length := len(history)
|
length := len(history)
|
||||||
|
|
||||||
if length > 0 {
|
if length > 0 {
|
||||||
result := make([]llm.ChatMessage, 0, length)
|
result := make([]llm.ChatMessage, 0, length)
|
||||||
|
|
||||||
for _, msg := range history {
|
for _, msg := range history {
|
||||||
result = append(result, messageDataToLlmMessage(msg))
|
result = append(result, llm.ChatMessage{
|
||||||
|
Name: msg.Name,
|
||||||
|
Text: msg.Text,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
@ -63,20 +66,3 @@ func historyToLlmMessages(history []MessageData) []llm.ChatMessage {
|
||||||
|
|
||||||
return make([]llm.ChatMessage, 0)
|
return make([]llm.ChatMessage, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func messageDataToLlmMessage(data MessageData) llm.ChatMessage {
|
|
||||||
llmMessage := llm.ChatMessage{
|
|
||||||
Name: data.Name,
|
|
||||||
Username: data.Username,
|
|
||||||
Text: data.Text,
|
|
||||||
IsMe: data.IsMe,
|
|
||||||
IsUserRequest: data.IsUserRequest,
|
|
||||||
}
|
|
||||||
|
|
||||||
if data.ReplyTo != nil {
|
|
||||||
replyMessage := messageDataToLlmMessage(*data.ReplyTo)
|
|
||||||
llmMessage.ReplyTo = &replyMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
return llmMessage
|
|
||||||
}
|
|
||||||
|
|
28
llm/llm.go
28
llm/llm.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -28,7 +29,7 @@ func NewConnector(baseUrl string, token string) *LlmConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LlmConnector) HandleChatMessage(userMessage ChatMessage, model string, requestContext RequestContext) (string, error) {
|
func (l *LlmConnector) HandleChatMessage(text string, model string, requestContext RequestContext) (string, error) {
|
||||||
systemPrompt := "You're a bot in the Telegram chat.\n" +
|
systemPrompt := "You're a bot in the Telegram chat.\n" +
|
||||||
"You're using a free model called \"" + model + "\".\n\n" +
|
"You're using a free model called \"" + model + "\".\n\n" +
|
||||||
requestContext.Prompt()
|
requestContext.Prompt()
|
||||||
|
@ -51,11 +52,28 @@ func (l *LlmConnector) HandleChatMessage(userMessage ChatMessage, model string,
|
||||||
|
|
||||||
if historyLength > 0 {
|
if historyLength > 0 {
|
||||||
for _, msg := range requestContext.Chat.History {
|
for _, msg := range requestContext.Chat.History {
|
||||||
req.Messages = append(req.Messages, chatMessageToOpenAiChatCompletionMessage(msg))
|
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{
|
||||||
|
Role: msgRole,
|
||||||
|
Content: msgText,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Messages = append(req.Messages, chatMessageToOpenAiChatCompletionMessage(userMessage))
|
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
|
||||||
|
Role: openai.ChatMessageRoleUser,
|
||||||
|
Content: text,
|
||||||
|
})
|
||||||
|
|
||||||
resp, err := l.client.CreateChatCompletion(context.Background(), req)
|
resp, err := l.client.CreateChatCompletion(context.Background(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -146,3 +164,7 @@ func (l *LlmConnector) HasModel(id string) bool {
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func quoteMessage(text string) string {
|
||||||
|
return "> " + strings.ReplaceAll(text, "\n", "\n> ")
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
package llm
|
package llm
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/sashabaranov/go-openai"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RequestContext struct {
|
type RequestContext struct {
|
||||||
Empty bool
|
Empty bool
|
||||||
User UserContext
|
User UserContext
|
||||||
|
@ -26,12 +21,9 @@ type ChatContext struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatMessage struct {
|
type ChatMessage struct {
|
||||||
Name string
|
Name string
|
||||||
Username string
|
Text string
|
||||||
Text string
|
IsMe bool
|
||||||
IsMe bool
|
|
||||||
IsUserRequest bool
|
|
||||||
ReplyTo *ChatMessage
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c RequestContext) Prompt() string {
|
func (c RequestContext) Prompt() string {
|
||||||
|
@ -70,54 +62,3 @@ func (c RequestContext) Prompt() string {
|
||||||
|
|
||||||
return prompt
|
return prompt
|
||||||
}
|
}
|
||||||
|
|
||||||
func chatMessageToOpenAiChatCompletionMessage(message ChatMessage) openai.ChatCompletionMessage {
|
|
||||||
var msgRole string
|
|
||||||
var msgText string
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case message.IsMe:
|
|
||||||
msgRole = openai.ChatMessageRoleAssistant
|
|
||||||
case message.IsUserRequest:
|
|
||||||
msgRole = openai.ChatMessageRoleUser
|
|
||||||
default:
|
|
||||||
msgRole = openai.ChatMessageRoleSystem
|
|
||||||
}
|
|
||||||
|
|
||||||
if message.IsMe {
|
|
||||||
msgText = message.Text
|
|
||||||
} else {
|
|
||||||
msgText = chatMessageToText(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return openai.ChatCompletionMessage{
|
|
||||||
Role: msgRole,
|
|
||||||
Content: msgText,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func chatMessageToText(message ChatMessage) string {
|
|
||||||
var msgText string
|
|
||||||
|
|
||||||
if message.ReplyTo == nil {
|
|
||||||
msgText += "In reply to:"
|
|
||||||
msgText += quoteText(presentUserMessageAsText(*message.ReplyTo)) + "\n\n"
|
|
||||||
}
|
|
||||||
msgText += presentUserMessageAsText(message)
|
|
||||||
|
|
||||||
return msgText
|
|
||||||
}
|
|
||||||
|
|
||||||
func presentUserMessageAsText(message ChatMessage) string {
|
|
||||||
result := message.Name
|
|
||||||
if message.Username != "" {
|
|
||||||
result += " (@" + message.Username + ")"
|
|
||||||
}
|
|
||||||
result += " wrote:\n" + message.Text
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func quoteText(text string) string {
|
|
||||||
return "> " + strings.ReplaceAll(text, "\n", "\n> ")
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue