Compare commits
No commits in common. "993c71ca712b49c132a4480be63f2d466cfb14d4" and "ec56188332f32364e4cacfeea564dc94b0623be9" have entirely different histories.
993c71ca71
...
ec56188332
|
@ -9,7 +9,7 @@
|
|||
```shell
|
||||
docker run \
|
||||
-e OLLAMA_TOKEN=123 \
|
||||
-e OLLAMA_BASE_URL=http://ollama.localhost:11434/v1 \
|
||||
-e OLLAMA_BASE_URL=http://ollama.tld:11434 \
|
||||
-e TELEGRAM_TOKEN=12345 \
|
||||
skobkin/telegram-llm-bot
|
||||
```
|
||||
|
|
60
bot/bot.go
60
bot/bot.go
|
@ -24,8 +24,6 @@ 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 {
|
||||
|
@ -34,37 +32,34 @@ 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", "error", err)
|
||||
slog.Error("Cannot retrieve api user", err)
|
||||
|
||||
return ErrGetMe
|
||||
}
|
||||
|
||||
slog.Info("Running api as", "id", botUser.ID, "username", botUser.Username, "name", botUser.FirstName, "is_bot", botUser.IsBot)
|
||||
slog.Info("Running api as", map[string]any{
|
||||
"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", "error", err)
|
||||
slog.Error("Cannot get update channel", err)
|
||||
|
||||
return ErrUpdatesChannel
|
||||
}
|
||||
|
||||
bh, err := th.NewBotHandler(b.api, updates)
|
||||
if err != nil {
|
||||
slog.Error("Cannot initialize bot handler", "error", err)
|
||||
slog.Error("Cannot initialize bot handler", err)
|
||||
|
||||
return ErrHandlerInit
|
||||
}
|
||||
|
@ -88,7 +83,7 @@ func (b *Bot) Run() error {
|
|||
}
|
||||
|
||||
func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
||||
slog.Info("/hey", "message-text", update.Message.Text)
|
||||
slog.Info("/hey")
|
||||
|
||||
b.stats.HeyRequest()
|
||||
|
||||
|
@ -116,23 +111,24 @@ func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
|||
return
|
||||
}
|
||||
|
||||
slog.Debug("Got completion. Going to send.", "llm-reply", llmReply)
|
||||
slog.Debug("Got completion. Going to send.", llmReply)
|
||||
|
||||
message := tu.Message(
|
||||
chatID,
|
||||
b.escapeMarkdownV1Symbols(llmReply),
|
||||
llmReply,
|
||||
).WithParseMode("Markdown")
|
||||
|
||||
_, err = bot.SendMessage(b.reply(update.Message, message))
|
||||
|
||||
if err != nil {
|
||||
slog.Error("Can't send reply message", "error", err)
|
||||
slog.Error("Can't send reply message", err)
|
||||
|
||||
b.trySendReplyError(update.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||
slog.Info("/summarize", "message-text", update.Message.Text)
|
||||
slog.Info("/summarize", update.Message.Text)
|
||||
|
||||
b.stats.SummarizeRequest()
|
||||
|
||||
|
@ -155,7 +151,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", "url", args[1])
|
||||
slog.Error("Provided URL is not valid", args[1])
|
||||
|
||||
_, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
|
||||
chatID,
|
||||
|
@ -167,7 +163,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", "error", err)
|
||||
slog.Error("Cannot retrieve an article using extractor", err)
|
||||
}
|
||||
|
||||
llmReply, err := b.llm.Summarize(article.Text, llm.ModelMistralUncensored)
|
||||
|
@ -182,17 +178,17 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
|||
return
|
||||
}
|
||||
|
||||
slog.Debug("Got completion. Going to send.", "llm-reply", llmReply)
|
||||
slog.Debug("Got completion. Going to send.", llmReply)
|
||||
|
||||
message := tu.Message(
|
||||
chatID,
|
||||
b.escapeMarkdownV1Symbols(llmReply),
|
||||
llmReply,
|
||||
).WithParseMode("Markdown")
|
||||
|
||||
_, err = bot.SendMessage(b.reply(update.Message, message))
|
||||
|
||||
if err != nil {
|
||||
slog.Error("Can't send reply message", "error", err)
|
||||
slog.Error("Can't send reply message", err)
|
||||
|
||||
b.trySendReplyError(update.Message)
|
||||
}
|
||||
|
@ -213,7 +209,7 @@ func (b *Bot) helpHandler(bot *telego.Bot, update telego.Update) {
|
|||
"/help - Show this help",
|
||||
)))
|
||||
if err != nil {
|
||||
slog.Error("Cannot send a message", "error", err)
|
||||
slog.Error("Cannot send a message", err)
|
||||
|
||||
b.trySendReplyError(update.Message)
|
||||
}
|
||||
|
@ -232,7 +228,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", "error", err)
|
||||
slog.Error("Cannot send a message", err)
|
||||
|
||||
b.trySendReplyError(update.Message)
|
||||
}
|
||||
|
@ -253,7 +249,7 @@ func (b *Bot) statsHandler(bot *telego.Bot, update telego.Update) {
|
|||
"```",
|
||||
)).WithParseMode("Markdown"))
|
||||
if err != nil {
|
||||
slog.Error("Cannot send a message", "error", err)
|
||||
slog.Error("Cannot send a message", err)
|
||||
|
||||
b.trySendReplyError(update.Message)
|
||||
}
|
||||
|
@ -265,8 +261,6 @@ 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
|
||||
}
|
||||
|
||||
|
@ -287,15 +281,9 @@ 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,
|
||||
|
@ -307,7 +295,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", "error", err)
|
||||
slog.Error("Cannot set chat action", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
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,22 +3,15 @@ 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()
|
||||
|
|
|
@ -3,7 +3,6 @@ package extractor
|
|||
import (
|
||||
"errors"
|
||||
"github.com/advancedlogic/GoOse"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -29,18 +28,12 @@ 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,
|
||||
|
|
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)
|
||||
if err != nil {
|
||||
slog.Error("llm: LLM back-end request failed", "error", err)
|
||||
slog.Error("LLM back-end request failed", err)
|
||||
|
||||
return "", ErrLlmBackendRequestFailed
|
||||
}
|
||||
|
||||
slog.Debug("llm: Received LLM back-end response", "response", resp)
|
||||
slog.Debug("Received LLM back-end response", resp)
|
||||
|
||||
if len(resp.Choices) < 1 {
|
||||
slog.Error("llm: LLM back-end reply has no choices")
|
||||
slog.Error("LLM back-end reply has no choices")
|
||||
|
||||
return "", ErrNoChoices
|
||||
}
|
||||
|
@ -72,9 +72,8 @@ func (l *LlmConnector) Summarize(text string, model string) (string, error) {
|
|||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleSystem,
|
||||
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. " +
|
||||
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. " +
|
||||
"If possible, use the same language as the original text.",
|
||||
},
|
||||
},
|
||||
|
@ -87,15 +86,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: LLM back-end request failed", "error", err)
|
||||
slog.Error("LLM back-end request failed", err)
|
||||
|
||||
return "", ErrLlmBackendRequestFailed
|
||||
}
|
||||
|
||||
slog.Debug("llm: Received LLM back-end response", resp)
|
||||
slog.Debug("Received LLM back-end response", resp)
|
||||
|
||||
if len(resp.Choices) < 1 {
|
||||
slog.Error("llm: LLM back-end reply has no choices")
|
||||
slog.Error("LLM back-end reply has no choices")
|
||||
|
||||
return "", ErrNoChoices
|
||||
}
|
||||
|
|
4
main.go
4
main.go
|
@ -20,7 +20,7 @@ func main() {
|
|||
llmc := llm.NewConnector(ollamaBaseUrl, ollamaToken)
|
||||
ext := extractor.NewExtractor()
|
||||
|
||||
telegramApi, err := tg.NewBot(telegramToken, tg.WithLogger(bot.NewLogger("telego: ")))
|
||||
telegramApi, err := tg.NewBot(telegramToken, tg.WithDefaultLogger(false, true))
|
||||
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", "error", err)
|
||||
slog.Error("Running bot finished with an error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue