Compare commits

..

No commits in common. "5d616dd31ee4886c7486559c4eacf6b143953131" and "8326333bcf94f3b49e25727ee2c2996af3ce55dd" have entirely different histories.

8 changed files with 172 additions and 279 deletions

View file

@ -18,20 +18,12 @@ var (
ErrHandlerInit = errors.New("cannot initialize handler") ErrHandlerInit = errors.New("cannot initialize handler")
) )
type BotInfo struct {
Id int64
Username string
Name string
}
type Bot struct { type Bot struct {
api *telego.Bot api *telego.Bot
llm *llm.LlmConnector llm *llm.LlmConnector
extractor *extractor.Extractor extractor *extractor.Extractor
stats *stats.Stats stats *stats.Stats
models ModelSelection models ModelSelection
history map[int64]*MessageRingBuffer
profile BotInfo
markdownV1Replacer *strings.Replacer markdownV1Replacer *strings.Replacer
} }
@ -48,8 +40,6 @@ func NewBot(
extractor: extractor, extractor: extractor,
stats: stats.NewStats(), stats: stats.NewStats(),
models: models, models: models,
history: make(map[int64]*MessageRingBuffer),
profile: BotInfo{0, "", ""},
markdownV1Replacer: strings.NewReplacer( markdownV1Replacer: strings.NewReplacer(
// https://core.telegram.org/bots/api#markdown-style // https://core.telegram.org/bots/api#markdown-style
@ -71,12 +61,6 @@ func (b *Bot) Run() error {
slog.Info("Running api as", "id", botUser.ID, "username", botUser.Username, "name", botUser.FirstName, "is_bot", botUser.IsBot) slog.Info("Running api as", "id", botUser.ID, "username", botUser.Username, "name", botUser.FirstName, "is_bot", botUser.IsBot)
b.profile = BotInfo{
Id: botUser.ID,
Username: botUser.Username,
Name: botUser.FirstName,
}
updates, err := b.api.UpdatesViaLongPolling(nil) updates, err := b.api.UpdatesViaLongPolling(nil)
if err != nil { if err != nil {
slog.Error("Cannot get update channel", "error", err) slog.Error("Cannot get update channel", "error", err)
@ -95,60 +79,133 @@ func (b *Bot) Run() error {
defer b.api.StopLongPolling() defer b.api.StopLongPolling()
// Middlewares // Middlewares
bh.Use(b.chatHistory)
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.heyHandler, th.CommandEqual("hey"))
bh.Handle(b.summarizeHandler, th.CommandEqual("summarize"))
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"))
// Inline query handlers
bh.Handle(b.inlineHandler, th.AnyInlineQuery())
bh.Start() bh.Start()
return nil return nil
} }
func (b *Bot) textMessageHandler(bot *telego.Bot, update telego.Update) { func (b *Bot) inlineHandler(bot *telego.Bot, update telego.Update) {
slog.Debug("/any-message") iq := update.InlineQuery
slog.Info("inline query received", "query", iq.Query)
message := update.Message slog.Debug("query", "query", iq)
switch { if len(iq.Query) < 3 {
// Mentions return
case b.isMentionOfMe(update): }
slog.Info("/any-message", "type", "mention")
b.processMention(message) b.stats.InlineQuery()
// Replies
case b.isReplyToMe(update): queryParts := strings.SplitN(iq.Query, " ", 2)
slog.Info("/any-message", "type", "reply")
b.processMention(message) if len(queryParts) < 1 {
// Private chat slog.Debug("Empty query. Skipping.")
case b.isPrivateWithMe(update):
slog.Info("/any-message", "type", "private") return
b.processMention(message) }
default:
slog.Debug("/any-message", "info", "Message is not mention, reply or private chat. Skipping.") var response *telego.AnswerInlineQueryParams
switch isValidAndAllowedUrl(queryParts[0]) {
case true:
slog.Info("Inline /summarize request", "url", queryParts[0])
b.stats.SummarizeRequest()
article, err := b.extractor.GetArticleFromUrl(queryParts[0])
if err != nil {
slog.Error("Cannot retrieve an article using extractor", "error", err)
}
llmReply, err := b.llm.Summarize(article.Text, b.models.TextRequestModel)
if err != nil {
slog.Error("Cannot get reply from LLM connector")
b.trySendInlineQueryError(iq, "LLM request error. Try again later.")
return
}
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
response = tu.InlineQuery(
iq.ID,
tu.ResultArticle(
"reply_"+iq.ID,
"Summary for "+queryParts[0],
tu.TextMessage(b.escapeMarkdownV1Symbols(llmReply)).WithParseMode("Markdown"),
),
)
case false:
b.stats.HeyRequest()
slog.Info("Inline /hey request", "text", iq.Query)
requestContext := createLlmRequestContextFromUpdate(update)
llmReply, err := b.llm.HandleSingleRequest(iq.Query, b.models.TextRequestModel, requestContext)
if err != nil {
slog.Error("Cannot get reply from LLM connector")
b.trySendInlineQueryError(iq, "LLM request error. Try again later.")
return
}
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
response = tu.InlineQuery(
iq.ID,
tu.ResultArticle(
"reply_"+iq.ID,
"LLM reply to\""+iq.Query+"\"",
tu.TextMessage(b.escapeMarkdownV1Symbols(llmReply)).WithParseMode("Markdown"),
),
)
}
err := bot.AnswerInlineQuery(response)
if err != nil {
slog.Error("Can't answer to inline query", "error", err)
b.trySendInlineQueryError(iq, "Couldn't send intended reply, sorry")
} }
} }
func (b *Bot) processMention(message *telego.Message) { func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
b.stats.Mention() slog.Info("/hey", "message-text", update.Message.Text)
slog.Info("/mention", "chat", message.Chat.ID) b.stats.HeyRequest()
chatID := tu.ID(message.Chat.ID) parts := strings.SplitN(update.Message.Text, " ", 2)
userMessage := "Hey!"
if len(parts) == 2 {
userMessage = parts[1]
}
chatID := tu.ID(update.Message.Chat.ID)
b.sendTyping(chatID) b.sendTyping(chatID)
requestContext := b.createLlmRequestContextFromMessage(message) requestContext := createLlmRequestContextFromUpdate(update)
llmReply, err := b.llm.HandleChatMessage(message.Text, b.models.TextRequestModel, requestContext) llmReply, err := b.llm.HandleSingleRequest(userMessage, 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")
_, _ = b.api.SendMessage(b.reply(message, tu.Message( _, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
chatID, chatID,
"LLM request error. Try again later.", "LLM request error. Try again later.",
))) )))
@ -158,21 +215,17 @@ func (b *Bot) processMention(message *telego.Message) {
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply) slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
reply := tu.Message( message := tu.Message(
chatID, chatID,
b.escapeMarkdownV1Symbols(llmReply), b.escapeMarkdownV1Symbols(llmReply),
).WithParseMode("Markdown") ).WithParseMode("Markdown")
_, err = b.api.SendMessage(b.reply(message, reply)) _, err = bot.SendMessage(b.reply(update.Message, message))
if err != nil { if err != nil {
slog.Error("Can't send reply message", "error", err) slog.Error("Can't send reply message", "error", err)
b.trySendReplyError(message) b.trySendReplyError(update.Message)
return
} }
b.saveBotReplyToHistory(message, llmReply)
} }
func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) { func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
@ -253,9 +306,7 @@ func (b *Bot) helpHandler(bot *telego.Bot, update telego.Update) {
"Instructions:\r\n"+ "Instructions:\r\n"+
"/hey <text> - Ask something from LLM\r\n"+ "/hey <text> - Ask something from LLM\r\n"+
"/summarize <link> - Summarize text from the provided link\r\n"+ "/summarize <link> - Summarize text from the provided link\r\n"+
"/s <link> - Shorter version\r\n"+ "/help - Show this help",
"/help - Show this help\r\n\r\n"+
"Mention bot or reply to it's message to communicate with it",
))) )))
if err != nil { if err != nil {
slog.Error("Cannot send a message", "error", err) slog.Error("Cannot send a message", "error", err)

View file

@ -39,40 +39,19 @@ func (b *Bot) trySendReplyError(message *telego.Message) {
))) )))
} }
func (b *Bot) isMentionOfMe(update telego.Update) bool { func (b *Bot) trySendInlineQueryError(iq *telego.InlineQuery, text string) {
if update.Message == nil { if iq == nil {
return false return
} }
return strings.Contains(update.Message.Text, "@"+b.profile.Username) _ = b.api.AnswerInlineQuery(tu.InlineQuery(
} iq.ID,
tu.ResultArticle(
func (b *Bot) isReplyToMe(update telego.Update) bool { string("error_"+iq.ID),
message := update.Message "Error: "+text,
tu.TextMessage(text),
if message == nil { ),
return false ))
}
if message.ReplyToMessage == nil {
return false
}
if message.ReplyToMessage.From == nil {
return false
}
replyToMessage := message.ReplyToMessage
return replyToMessage != nil && replyToMessage.From.ID == b.profile.Id
}
func (b *Bot) isPrivateWithMe(update telego.Update) bool {
message := update.Message
if message == nil {
return false
}
return message.Chat.Type == telego.ChatTypePrivate
} }
func isValidAndAllowedUrl(text string) bool { func isValidAndAllowedUrl(text string) bool {

View file

@ -1,90 +0,0 @@
package bot
import (
"github.com/mymmrac/telego"
"log/slog"
)
const HistoryLength = 50
type MessageRingBuffer struct {
messages []Message
capacity int
}
func NewMessageBuffer(capacity int) *MessageRingBuffer {
return &MessageRingBuffer{
messages: make([]Message, 0, capacity),
capacity: capacity,
}
}
func (b *MessageRingBuffer) Push(element Message) {
if len(b.messages) >= b.capacity {
b.messages = b.messages[1:]
}
b.messages = append(b.messages, element)
}
func (b *MessageRingBuffer) GetAll() []Message {
return b.messages
}
type Message struct {
Name string
Text string
}
func (b *Bot) saveChatMessageToHistory(message *telego.Message) {
chatId := message.Chat.ID
slog.Info(
"history-message-save",
"chat", chatId,
"from_id", message.From.ID,
"from_name", message.From.FirstName,
"text", message.Text,
)
_, ok := b.history[chatId]
if !ok {
b.history[chatId] = NewMessageBuffer(HistoryLength)
}
b.history[chatId].Push(Message{
Name: message.From.FirstName,
Text: message.Text,
})
}
func (b *Bot) saveBotReplyToHistory(message *telego.Message, reply string) {
chatId := message.Chat.ID
slog.Info(
"history-reply-save",
"chat", chatId,
"to_id", message.From.ID,
"to_name", message.From.FirstName,
"text", reply,
)
_, ok := b.history[chatId]
if !ok {
b.history[chatId] = NewMessageBuffer(HistoryLength)
}
b.history[chatId].Push(Message{
Name: b.profile.Username,
Text: reply,
})
}
func (b *Bot) getChatHistory(chatId int64) []Message {
_, ok := b.history[chatId]
if !ok {
return make([]Message, 0)
}
return b.history[chatId].GetAll()
}

View file

@ -10,41 +10,21 @@ func (b *Bot) chatTypeStatsCounter(bot *telego.Bot, update telego.Update, next t
message := update.Message message := update.Message
if message == nil { if message == nil {
slog.Info("stats-middleware: update has no message. skipping.") slog.Info("chat-type-middleware: update has no message. skipping.")
next(bot, update) next(bot, update)
return return
} }
slog.Info("chat-type-middleware: counting message chat type in stats", "type", message.Chat.Type)
switch message.Chat.Type { switch message.Chat.Type {
case telego.ChatTypeGroup, telego.ChatTypeSupergroup: case telego.ChatTypeGroup, telego.ChatTypeSupergroup:
if b.isMentionOfMe(update) || b.isReplyToMe(update) { b.stats.GroupRequest()
slog.Info("stats-middleware: counting message chat type in stats", "type", message.Chat.Type)
b.stats.GroupRequest()
}
case telego.ChatTypePrivate: case telego.ChatTypePrivate:
slog.Info("stats-middleware: counting message chat type in stats", "type", message.Chat.Type)
b.stats.PrivateRequest() b.stats.PrivateRequest()
} }
next(bot, update) next(bot, update)
} }
func (b *Bot) chatHistory(bot *telego.Bot, update telego.Update, next telegohandler.Handler) {
message := update.Message
if message == nil {
slog.Info("chat-history-middleware: update has no message. skipping.")
next(bot, update)
return
}
slog.Info("chat-history-middleware: saving message to history for", "chat_id", message.Chat.ID)
b.saveChatMessageToHistory(message)
next(bot, update)
}

View file

@ -6,20 +6,33 @@ import (
"telegram-ollama-reply-bot/llm" "telegram-ollama-reply-bot/llm"
) )
func (b *Bot) createLlmRequestContextFromMessage(message *telego.Message) llm.RequestContext { func createLlmRequestContextFromUpdate(update telego.Update) llm.RequestContext {
message := update.Message
iq := update.InlineQuery
rc := llm.RequestContext{ rc := llm.RequestContext{
Empty: true, Empty: true,
Inline: false,
} }
if message == nil { switch {
case message == nil && iq == nil:
slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc) slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc)
return rc return rc
case iq != nil:
rc.Inline = true
} }
rc.Empty = false rc.Empty = false
user := message.From var user *telego.User
if rc.Inline {
user = &iq.From
} else {
user = message.From
}
if user != nil { if user != nil {
rc.User = llm.UserContext{ rc.User = llm.UserContext{
@ -30,39 +43,18 @@ func (b *Bot) createLlmRequestContextFromMessage(message *telego.Message) llm.Re
} }
} }
// TODO: implement retrieval of chat description if !rc.Inline {
chat := message.Chat // TODO: implement retrieval of chat description
chat := message.Chat
history := b.getChatHistory(chat.ID) rc.Chat = llm.ChatContext{
Title: chat.Title,
rc.Chat = llm.ChatContext{ // TODO: fill when ChatFullInfo retrieved
Title: chat.Title, //Description: chat.Description,
// TODO: fill when ChatFullInfo retrieved Type: chat.Type,
//Description: chat.Description, }
Type: chat.Type,
History: historyToLlmMessages(history),
} }
slog.Debug("request context created", "request-context", rc) slog.Debug("request context created", "request-context", rc)
return rc return rc
} }
func historyToLlmMessages(history []Message) []llm.ChatMessage {
length := len(history)
if length > 0 {
result := make([]llm.ChatMessage, 0, length)
for _, msg := range history {
result = append(result, llm.ChatMessage{
Name: msg.Name,
Text: msg.Text,
})
}
return result
}
return make([]llm.ChatMessage, 0)
}

View file

@ -5,8 +5,6 @@ import (
"errors" "errors"
"github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai"
"log/slog" "log/slog"
"strconv"
"strings"
) )
var ( var (
@ -29,15 +27,13 @@ func NewConnector(baseUrl string, token string) *LlmConnector {
} }
} }
func (l *LlmConnector) HandleChatMessage(text string, model string, requestContext RequestContext) (string, error) { func (l *LlmConnector) HandleSingleRequest(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" +
requestContext.Prompt() "Currently you're not able to access chat history, so each message will be replied from a clean slate."
historyLength := len(requestContext.Chat.History) if !requestContext.Empty {
systemPrompt += "\n" + requestContext.Prompt()
if historyLength > 0 {
systemPrompt += "\nYou have an access to last " + strconv.Itoa(historyLength) + "messages in this chat."
} }
req := openai.ChatCompletionRequest{ req := openai.ChatCompletionRequest{
@ -50,15 +46,6 @@ func (l *LlmConnector) HandleChatMessage(text string, model string, requestConte
}, },
} }
if historyLength > 0 {
for _, msg := range requestContext.Chat.History {
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: msg.Name + ":\n\n" + quoteMessage(msg.Text),
})
}
}
req.Messages = append(req.Messages, openai.ChatCompletionMessage{ req.Messages = append(req.Messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser, Role: openai.ChatMessageRoleUser,
Content: text, Content: text,
@ -153,7 +140,3 @@ func (l *LlmConnector) HasModel(id string) bool {
return false return false
} }
func quoteMessage(text string) string {
return "> " + strings.ReplaceAll(text, "\n", "\n> ")
}

View file

@ -1,9 +1,10 @@
package llm package llm
type RequestContext struct { type RequestContext struct {
Empty bool Empty bool
User UserContext Inline bool
Chat ChatContext User UserContext
Chat ChatContext
} }
type UserContext struct { type UserContext struct {
@ -17,12 +18,6 @@ type ChatContext struct {
Title string Title string
Description string Description string
Type string Type string
History []ChatMessage
}
type ChatMessage struct {
Name string
Text string
} }
func (c RequestContext) Prompt() string { func (c RequestContext) Prompt() string {
@ -31,17 +26,20 @@ func (c RequestContext) Prompt() string {
} }
prompt := "" prompt := ""
if !c.Inline {
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.Title != "" {
prompt += "Chat is called \"" + c.Chat.Title + "\". "
if c.Chat.Title != "" { }
prompt += "Chat is called \"" + c.Chat.Title + "\". " if c.Chat.Description != "" {
} prompt += "Chat description is \"" + c.Chat.Description + "\". "
if c.Chat.Description != "" { }
prompt += "Chat description is \"" + c.Chat.Description + "\". " } else {
prompt += "You're responding to inline query, so you're not in the chat right now. "
} }
prompt += "Profile of the user who mentioned you in the chat:" + prompt += "User profile:" +
"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"

View file

@ -15,7 +15,7 @@ type Stats struct {
PrivateRequests uint64 PrivateRequests uint64
InlineQueries uint64 InlineQueries uint64
Mentions uint64 HeyRequests uint64
SummarizeRequests uint64 SummarizeRequests uint64
} }
@ -27,7 +27,7 @@ func NewStats() *Stats {
PrivateRequests: 0, PrivateRequests: 0,
InlineQueries: 0, InlineQueries: 0,
Mentions: 0, HeyRequests: 0,
SummarizeRequests: 0, SummarizeRequests: 0,
} }
} }
@ -40,7 +40,7 @@ func (s *Stats) MarshalJSON() ([]byte, error) {
PrivateRequests uint64 `json:"private_requests"` PrivateRequests uint64 `json:"private_requests"`
InlineQueries uint64 `json:"inline_queries"` InlineQueries uint64 `json:"inline_queries"`
Mentions uint64 `json:"mentions"` HeyRequests uint64 `json:"hey_requests"`
SummarizeRequests uint64 `json:"summarize_requests"` SummarizeRequests uint64 `json:"summarize_requests"`
}{ }{
Uptime: time.Now().Sub(s.RunningSince).String(), Uptime: time.Now().Sub(s.RunningSince).String(),
@ -49,7 +49,7 @@ func (s *Stats) MarshalJSON() ([]byte, error) {
PrivateRequests: s.PrivateRequests, PrivateRequests: s.PrivateRequests,
InlineQueries: s.InlineQueries, InlineQueries: s.InlineQueries,
Mentions: s.Mentions, HeyRequests: s.HeyRequests,
SummarizeRequests: s.SummarizeRequests, SummarizeRequests: s.SummarizeRequests,
}) })
} }
@ -81,10 +81,10 @@ func (s *Stats) PrivateRequest() {
s.PrivateRequests++ s.PrivateRequests++
} }
func (s *Stats) Mention() { func (s *Stats) HeyRequest() {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
s.Mentions++ s.HeyRequests++
} }
func (s *Stats) SummarizeRequest() { func (s *Stats) SummarizeRequest() {