Compare commits
7 commits
ec56188332
...
993c71ca71
Author | SHA1 | Date | |
---|---|---|---|
Alexey Skobkin | 993c71ca71 | ||
3fa7c2434f | |||
d3c0bc28f1 | |||
38fcd1a5a9 | |||
bfacbb9f98 | |||
a2a37d0256 | |||
5f55cec0e2 |
|
@ -9,7 +9,7 @@
|
||||||
```shell
|
```shell
|
||||||
docker run \
|
docker run \
|
||||||
-e OLLAMA_TOKEN=123 \
|
-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 \
|
-e TELEGRAM_TOKEN=12345 \
|
||||||
skobkin/telegram-llm-bot
|
skobkin/telegram-llm-bot
|
||||||
```
|
```
|
||||||
|
|
60
bot/bot.go
60
bot/bot.go
|
@ -24,6 +24,8 @@ type Bot struct {
|
||||||
llm *llm.LlmConnector
|
llm *llm.LlmConnector
|
||||||
extractor *extractor.Extractor
|
extractor *extractor.Extractor
|
||||||
stats *stats.Stats
|
stats *stats.Stats
|
||||||
|
|
||||||
|
markdownV1Replacer *strings.Replacer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extractor) *Bot {
|
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,
|
llm: llm,
|
||||||
extractor: extractor,
|
extractor: extractor,
|
||||||
stats: stats.NewStats(),
|
stats: stats.NewStats(),
|
||||||
|
|
||||||
|
markdownV1Replacer: strings.NewReplacer(
|
||||||
|
// https://core.telegram.org/bots/api#markdown-style
|
||||||
|
"_", "\\_",
|
||||||
|
//"*", "\\*",
|
||||||
|
//"`", "\\`",
|
||||||
|
//"[", "\\[",
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) Run() error {
|
func (b *Bot) Run() error {
|
||||||
botUser, err := b.api.GetMe()
|
botUser, err := b.api.GetMe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Cannot retrieve api user", err)
|
slog.Error("Cannot retrieve api user", "error", err)
|
||||||
|
|
||||||
return ErrGetMe
|
return ErrGetMe
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("Running api as", map[string]any{
|
slog.Info("Running api as", "id", botUser.ID, "username", botUser.Username, "name", botUser.FirstName, "is_bot", botUser.IsBot)
|
||||||
"id": botUser.ID,
|
|
||||||
"username": botUser.Username,
|
|
||||||
"name": botUser.FirstName,
|
|
||||||
"is_bot": botUser.IsBot,
|
|
||||||
})
|
|
||||||
|
|
||||||
updates, err := b.api.UpdatesViaLongPolling(nil)
|
updates, err := b.api.UpdatesViaLongPolling(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Cannot get update channel", err)
|
slog.Error("Cannot get update channel", "error", err)
|
||||||
|
|
||||||
return ErrUpdatesChannel
|
return ErrUpdatesChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
bh, err := th.NewBotHandler(b.api, updates)
|
bh, err := th.NewBotHandler(b.api, updates)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Cannot initialize bot handler", err)
|
slog.Error("Cannot initialize bot handler", "error", err)
|
||||||
|
|
||||||
return ErrHandlerInit
|
return ErrHandlerInit
|
||||||
}
|
}
|
||||||
|
@ -83,7 +88,7 @@ func (b *Bot) Run() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
||||||
slog.Info("/hey")
|
slog.Info("/hey", "message-text", update.Message.Text)
|
||||||
|
|
||||||
b.stats.HeyRequest()
|
b.stats.HeyRequest()
|
||||||
|
|
||||||
|
@ -111,24 +116,23 @@ func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("Got completion. Going to send.", llmReply)
|
slog.Debug("Got completion. Going to send.", "llm-reply", llmReply)
|
||||||
|
|
||||||
message := tu.Message(
|
message := tu.Message(
|
||||||
chatID,
|
chatID,
|
||||||
llmReply,
|
b.escapeMarkdownV1Symbols(llmReply),
|
||||||
).WithParseMode("Markdown")
|
).WithParseMode("Markdown")
|
||||||
|
|
||||||
_, err = bot.SendMessage(b.reply(update.Message, message))
|
_, err = bot.SendMessage(b.reply(update.Message, message))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Can't send reply message", err)
|
slog.Error("Can't send reply message", "error", err)
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
b.trySendReplyError(update.Message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
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()
|
b.stats.SummarizeRequest()
|
||||||
|
|
||||||
|
@ -151,7 +155,7 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
|
|
||||||
_, err := url.ParseRequestURI(args[1])
|
_, err := url.ParseRequestURI(args[1])
|
||||||
if err != nil {
|
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(
|
_, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
|
||||||
chatID,
|
chatID,
|
||||||
|
@ -163,7 +167,7 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
|
|
||||||
article, err := b.extractor.GetArticleFromUrl(args[1])
|
article, err := b.extractor.GetArticleFromUrl(args[1])
|
||||||
if err != nil {
|
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)
|
llmReply, err := b.llm.Summarize(article.Text, llm.ModelMistralUncensored)
|
||||||
|
@ -178,17 +182,17 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("Got completion. Going to send.", llmReply)
|
slog.Debug("Got completion. Going to send.", "llm-reply", llmReply)
|
||||||
|
|
||||||
message := tu.Message(
|
message := tu.Message(
|
||||||
chatID,
|
chatID,
|
||||||
llmReply,
|
b.escapeMarkdownV1Symbols(llmReply),
|
||||||
).WithParseMode("Markdown")
|
).WithParseMode("Markdown")
|
||||||
|
|
||||||
_, err = bot.SendMessage(b.reply(update.Message, message))
|
_, err = bot.SendMessage(b.reply(update.Message, message))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Can't send reply message", err)
|
slog.Error("Can't send reply message", "error", err)
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
b.trySendReplyError(update.Message)
|
||||||
}
|
}
|
||||||
|
@ -209,7 +213,7 @@ func (b *Bot) helpHandler(bot *telego.Bot, update telego.Update) {
|
||||||
"/help - Show this help",
|
"/help - Show this help",
|
||||||
)))
|
)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Cannot send a message", err)
|
slog.Error("Cannot send a message", "error", err)
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
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.",
|
"Check out /help to learn how to use this bot.",
|
||||||
)))
|
)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Cannot send a message", err)
|
slog.Error("Cannot send a message", "error", err)
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
b.trySendReplyError(update.Message)
|
||||||
}
|
}
|
||||||
|
@ -249,7 +253,7 @@ func (b *Bot) statsHandler(bot *telego.Bot, update telego.Update) {
|
||||||
"```",
|
"```",
|
||||||
)).WithParseMode("Markdown"))
|
)).WithParseMode("Markdown"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Cannot send a message", err)
|
slog.Error("Cannot send a message", "error", err)
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
b.trySendReplyError(update.Message)
|
||||||
}
|
}
|
||||||
|
@ -261,6 +265,8 @@ func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext {
|
||||||
rc := llm.RequestContext{}
|
rc := llm.RequestContext{}
|
||||||
|
|
||||||
if message == nil {
|
if message == nil {
|
||||||
|
slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc)
|
||||||
|
|
||||||
return rc
|
return rc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,9 +287,15 @@ func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext {
|
||||||
Type: chat.Type,
|
Type: chat.Type,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("request context created", "request-context", rc)
|
||||||
|
|
||||||
return 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 {
|
func (b *Bot) reply(originalMessage *telego.Message, newMessage *telego.SendMessageParams) *telego.SendMessageParams {
|
||||||
return newMessage.WithReplyParameters(&telego.ReplyParameters{
|
return newMessage.WithReplyParameters(&telego.ReplyParameters{
|
||||||
MessageID: originalMessage.MessageID,
|
MessageID: originalMessage.MessageID,
|
||||||
|
@ -295,7 +307,7 @@ func (b *Bot) sendTyping(chatId telego.ChatID) {
|
||||||
|
|
||||||
err := b.api.SendChatAction(tu.ChatAction(chatId, "typing"))
|
err := b.api.SendChatAction(tu.ChatAction(chatId, "typing"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Cannot set chat action", err)
|
slog.Error("Cannot set chat action", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
bot/logger.go
Normal file
24
bot/logger.go
Normal 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))
|
||||||
|
}
|
|
@ -3,15 +3,22 @@ package bot
|
||||||
import (
|
import (
|
||||||
"github.com/mymmrac/telego"
|
"github.com/mymmrac/telego"
|
||||||
"github.com/mymmrac/telego/telegohandler"
|
"github.com/mymmrac/telego/telegohandler"
|
||||||
|
"log/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bot) chatTypeStatsCounter(bot *telego.Bot, update telego.Update, next telegohandler.Handler) {
|
func (b *Bot) chatTypeStatsCounter(bot *telego.Bot, update telego.Update, next telegohandler.Handler) {
|
||||||
message := update.Message
|
message := update.Message
|
||||||
|
|
||||||
if message == nil {
|
if message == nil {
|
||||||
|
slog.Info("chat-type-middleware: update has no message. skipping.")
|
||||||
|
|
||||||
next(bot, update)
|
next(bot, update)
|
||||||
|
|
||||||
|
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:
|
||||||
b.stats.GroupRequest()
|
b.stats.GroupRequest()
|
||||||
|
|
|
@ -3,6 +3,7 @@ package extractor
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/advancedlogic/GoOse"
|
"github.com/advancedlogic/GoOse"
|
||||||
|
"log/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -28,12 +29,18 @@ type Article struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Extractor) GetArticleFromUrl(url string) (Article, error) {
|
func (e *Extractor) GetArticleFromUrl(url string) (Article, error) {
|
||||||
|
slog.Info("extractor: requested extraction from URL ", "url", url)
|
||||||
|
|
||||||
article, err := e.goose.ExtractFromURL(url)
|
article, err := e.goose.ExtractFromURL(url)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
slog.Error("extractor: failed extracting from URL", "url", url)
|
||||||
|
|
||||||
return Article{}, ErrExtractFailed
|
return Article{}, ErrExtractFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("extractor: article extracted", "article", article)
|
||||||
|
|
||||||
return Article{
|
return Article{
|
||||||
Title: article.Title,
|
Title: article.Title,
|
||||||
Text: article.CleanedText,
|
Text: article.CleanedText,
|
||||||
|
|
17
llm/llm.go
17
llm/llm.go
|
@ -50,15 +50,15 @@ func (l *LlmConnector) HandleSingleRequest(text string, model string, requestCon
|
||||||
|
|
||||||
resp, err := l.client.CreateChatCompletion(context.Background(), req)
|
resp, err := l.client.CreateChatCompletion(context.Background(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("LLM back-end request failed", err)
|
slog.Error("llm: LLM back-end request failed", "error", err)
|
||||||
|
|
||||||
return "", ErrLlmBackendRequestFailed
|
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 {
|
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
|
return "", ErrNoChoices
|
||||||
}
|
}
|
||||||
|
@ -72,8 +72,9 @@ func (l *LlmConnector) Summarize(text string, model string) (string, error) {
|
||||||
Messages: []openai.ChatCompletionMessage{
|
Messages: []openai.ChatCompletionMessage{
|
||||||
{
|
{
|
||||||
Role: openai.ChatMessageRoleSystem,
|
Role: openai.ChatMessageRoleSystem,
|
||||||
Content: "You are a short digest editor. Summarize the text you received " +
|
Content: "You're a text shortener. Give a very brief summary of the main facts " +
|
||||||
"as a list of bullet points with most important facts from the text. " +
|
"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.",
|
"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)
|
resp, err := l.client.CreateChatCompletion(context.Background(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("LLM back-end request failed", err)
|
slog.Error("llm: LLM back-end request failed", "error", err)
|
||||||
|
|
||||||
return "", ErrLlmBackendRequestFailed
|
return "", ErrLlmBackendRequestFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("Received LLM back-end response", resp)
|
slog.Debug("llm: Received LLM back-end response", resp)
|
||||||
|
|
||||||
if len(resp.Choices) < 1 {
|
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
|
return "", ErrNoChoices
|
||||||
}
|
}
|
||||||
|
|
4
main.go
4
main.go
|
@ -20,7 +20,7 @@ func main() {
|
||||||
llmc := llm.NewConnector(ollamaBaseUrl, ollamaToken)
|
llmc := llm.NewConnector(ollamaBaseUrl, ollamaToken)
|
||||||
ext := extractor.NewExtractor()
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -30,7 +30,7 @@ func main() {
|
||||||
|
|
||||||
err = botService.Run()
|
err = botService.Run()
|
||||||
if err != nil {
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue