Compare commits

...

7 commits

Author SHA1 Message Date
Alexey Skobkin 993c71ca71 Merge pull request 'Fixing a bunch of bugs and making some improvements' (#19) from fix_chat_type_middleware_nil_pointer into main
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: #19
2024-03-12 20:08:50 +00:00
Alexey Skobkin 3fa7c2434f
Fix #17. Implementing slog-based logger for telego and passing it into the library.
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2024-03-12 23:05:52 +03:00
Alexey Skobkin d3c0bc28f1
Fix #18. Escaping underscore symbols to avoid Telegram's Bot API "Markdown" (v1) parser errors. 2024-03-12 23:01:05 +03:00
Alexey Skobkin 38fcd1a5a9
Adding API URL suffix to the README.md. 2024-03-12 22:13:32 +03:00
Alexey Skobkin bfacbb9f98
Tweaking summarizer prompt a bit. 2024-03-12 22:12:58 +03:00
Alexey Skobkin a2a37d0256
Trying to fix #13. Adding logging to the middleware. 2024-03-12 22:10:34 +03:00
Alexey Skobkin 5f55cec0e2
fix #15 slog usage. Also adding more logging. 2024-03-12 22:07:22 +03:00
7 changed files with 86 additions and 35 deletions

View file

@ -9,7 +9,7 @@
```shell
docker run \
-e OLLAMA_TOKEN=123 \
-e OLLAMA_BASE_URL=http://ollama.tld:11434 \
-e OLLAMA_BASE_URL=http://ollama.localhost:11434/v1 \
-e TELEGRAM_TOKEN=12345 \
skobkin/telegram-llm-bot
```

View file

@ -24,6 +24,8 @@ type Bot struct {
llm *llm.LlmConnector
extractor *extractor.Extractor
stats *stats.Stats
markdownV1Replacer *strings.Replacer
}
func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extractor) *Bot {
@ -32,34 +34,37 @@ func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extract
llm: llm,
extractor: extractor,
stats: stats.NewStats(),
markdownV1Replacer: strings.NewReplacer(
// https://core.telegram.org/bots/api#markdown-style
"_", "\\_",
//"*", "\\*",
//"`", "\\`",
//"[", "\\[",
),
}
}
func (b *Bot) Run() error {
botUser, err := b.api.GetMe()
if err != nil {
slog.Error("Cannot retrieve api user", err)
slog.Error("Cannot retrieve api user", "error", err)
return ErrGetMe
}
slog.Info("Running api as", map[string]any{
"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)
updates, err := b.api.UpdatesViaLongPolling(nil)
if err != nil {
slog.Error("Cannot get update channel", err)
slog.Error("Cannot get update channel", "error", err)
return ErrUpdatesChannel
}
bh, err := th.NewBotHandler(b.api, updates)
if err != nil {
slog.Error("Cannot initialize bot handler", err)
slog.Error("Cannot initialize bot handler", "error", err)
return ErrHandlerInit
}
@ -83,7 +88,7 @@ func (b *Bot) Run() error {
}
func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
slog.Info("/hey")
slog.Info("/hey", "message-text", update.Message.Text)
b.stats.HeyRequest()
@ -111,24 +116,23 @@ func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
return
}
slog.Debug("Got completion. Going to send.", llmReply)
slog.Debug("Got completion. Going to send.", "llm-reply", llmReply)
message := tu.Message(
chatID,
llmReply,
b.escapeMarkdownV1Symbols(llmReply),
).WithParseMode("Markdown")
_, err = bot.SendMessage(b.reply(update.Message, message))
if err != nil {
slog.Error("Can't send reply message", err)
slog.Error("Can't send reply message", "error", err)
b.trySendReplyError(update.Message)
}
}
func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
slog.Info("/summarize", update.Message.Text)
slog.Info("/summarize", "message-text", update.Message.Text)
b.stats.SummarizeRequest()
@ -151,7 +155,7 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
_, err := url.ParseRequestURI(args[1])
if err != nil {
slog.Error("Provided URL is not valid", args[1])
slog.Error("Provided URL is not valid", "url", args[1])
_, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
chatID,
@ -163,7 +167,7 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
article, err := b.extractor.GetArticleFromUrl(args[1])
if err != nil {
slog.Error("Cannot retrieve an article using extractor", err)
slog.Error("Cannot retrieve an article using extractor", "error", err)
}
llmReply, err := b.llm.Summarize(article.Text, llm.ModelMistralUncensored)
@ -178,17 +182,17 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
return
}
slog.Debug("Got completion. Going to send.", llmReply)
slog.Debug("Got completion. Going to send.", "llm-reply", llmReply)
message := tu.Message(
chatID,
llmReply,
b.escapeMarkdownV1Symbols(llmReply),
).WithParseMode("Markdown")
_, err = bot.SendMessage(b.reply(update.Message, message))
if err != nil {
slog.Error("Can't send reply message", err)
slog.Error("Can't send reply message", "error", err)
b.trySendReplyError(update.Message)
}
@ -209,7 +213,7 @@ func (b *Bot) helpHandler(bot *telego.Bot, update telego.Update) {
"/help - Show this help",
)))
if err != nil {
slog.Error("Cannot send a message", err)
slog.Error("Cannot send a message", "error", err)
b.trySendReplyError(update.Message)
}
@ -228,7 +232,7 @@ func (b *Bot) startHandler(bot *telego.Bot, update telego.Update) {
"Check out /help to learn how to use this bot.",
)))
if err != nil {
slog.Error("Cannot send a message", err)
slog.Error("Cannot send a message", "error", err)
b.trySendReplyError(update.Message)
}
@ -249,7 +253,7 @@ func (b *Bot) statsHandler(bot *telego.Bot, update telego.Update) {
"```",
)).WithParseMode("Markdown"))
if err != nil {
slog.Error("Cannot send a message", err)
slog.Error("Cannot send a message", "error", err)
b.trySendReplyError(update.Message)
}
@ -261,6 +265,8 @@ func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext {
rc := llm.RequestContext{}
if message == nil {
slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc)
return rc
}
@ -281,9 +287,15 @@ func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext {
Type: chat.Type,
}
slog.Debug("request context created", "request-context", rc)
return rc
}
func (b *Bot) escapeMarkdownV1Symbols(input string) string {
return b.markdownV1Replacer.Replace(input)
}
func (b *Bot) reply(originalMessage *telego.Message, newMessage *telego.SendMessageParams) *telego.SendMessageParams {
return newMessage.WithReplyParameters(&telego.ReplyParameters{
MessageID: originalMessage.MessageID,
@ -295,7 +307,7 @@ func (b *Bot) sendTyping(chatId telego.ChatID) {
err := b.api.SendChatAction(tu.ChatAction(chatId, "typing"))
if err != nil {
slog.Error("Cannot set chat action", err)
slog.Error("Cannot set chat action", "error", err)
}
}

24
bot/logger.go Normal file
View file

@ -0,0 +1,24 @@
package bot
import (
"fmt"
"log/slog"
)
type Logger struct {
prefix string
}
func NewLogger(prefix string) Logger {
return Logger{
prefix: prefix,
}
}
func (l Logger) Debugf(format string, args ...any) {
slog.Debug(l.prefix + fmt.Sprint(format, args))
}
func (l Logger) Errorf(format string, args ...any) {
slog.Error(l.prefix + fmt.Sprintf(format, args))
}

View file

@ -3,15 +3,22 @@ package bot
import (
"github.com/mymmrac/telego"
"github.com/mymmrac/telego/telegohandler"
"log/slog"
)
func (b *Bot) chatTypeStatsCounter(bot *telego.Bot, update telego.Update, next telegohandler.Handler) {
message := update.Message
if message == nil {
slog.Info("chat-type-middleware: update has no message. skipping.")
next(bot, update)
return
}
slog.Info("chat-type-middleware: counting message chat type in stats", "type", message.Chat.Type)
switch message.Chat.Type {
case telego.ChatTypeGroup, telego.ChatTypeSupergroup:
b.stats.GroupRequest()

View file

@ -3,6 +3,7 @@ package extractor
import (
"errors"
"github.com/advancedlogic/GoOse"
"log/slog"
)
var (
@ -28,12 +29,18 @@ type Article struct {
}
func (e *Extractor) GetArticleFromUrl(url string) (Article, error) {
slog.Info("extractor: requested extraction from URL ", "url", url)
article, err := e.goose.ExtractFromURL(url)
if err != nil {
slog.Error("extractor: failed extracting from URL", "url", url)
return Article{}, ErrExtractFailed
}
slog.Debug("extractor: article extracted", "article", article)
return Article{
Title: article.Title,
Text: article.CleanedText,

View file

@ -50,15 +50,15 @@ func (l *LlmConnector) HandleSingleRequest(text string, model string, requestCon
resp, err := l.client.CreateChatCompletion(context.Background(), req)
if err != nil {
slog.Error("LLM back-end request failed", err)
slog.Error("llm: LLM back-end request failed", "error", err)
return "", ErrLlmBackendRequestFailed
}
slog.Debug("Received LLM back-end response", resp)
slog.Debug("llm: Received LLM back-end response", "response", resp)
if len(resp.Choices) < 1 {
slog.Error("LLM back-end reply has no choices")
slog.Error("llm: LLM back-end reply has no choices")
return "", ErrNoChoices
}
@ -72,8 +72,9 @@ func (l *LlmConnector) Summarize(text string, model string) (string, error) {
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a short digest editor. Summarize the text you received " +
"as a list of bullet points with most important facts from the text. " +
Content: "You're a text shortener. Give a very brief summary of the main facts " +
"point by point. Format them as a list of bullet points. " +
"Avoid any commentaries and value judgement on the matter. " +
"If possible, use the same language as the original text.",
},
},
@ -86,15 +87,15 @@ func (l *LlmConnector) Summarize(text string, model string) (string, error) {
resp, err := l.client.CreateChatCompletion(context.Background(), req)
if err != nil {
slog.Error("LLM back-end request failed", err)
slog.Error("llm: LLM back-end request failed", "error", err)
return "", ErrLlmBackendRequestFailed
}
slog.Debug("Received LLM back-end response", resp)
slog.Debug("llm: Received LLM back-end response", resp)
if len(resp.Choices) < 1 {
slog.Error("LLM back-end reply has no choices")
slog.Error("llm: LLM back-end reply has no choices")
return "", ErrNoChoices
}

View file

@ -20,7 +20,7 @@ func main() {
llmc := llm.NewConnector(ollamaBaseUrl, ollamaToken)
ext := extractor.NewExtractor()
telegramApi, err := tg.NewBot(telegramToken, tg.WithDefaultLogger(false, true))
telegramApi, err := tg.NewBot(telegramToken, tg.WithLogger(bot.NewLogger("telego: ")))
if err != nil {
fmt.Println(err)
os.Exit(1)
@ -30,7 +30,7 @@ func main() {
err = botService.Run()
if err != nil {
slog.Error("Running bot finished with an error", err)
slog.Error("Running bot finished with an error", "error", err)
os.Exit(1)
}
}