Fixing a bunch of bugs and making some improvements #19
42
bot/bot.go
42
bot/bot.go
|
@ -38,28 +38,23 @@ func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extract
|
||||||
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 +78,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,7 +106,7 @@ 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,
|
||||||
|
@ -119,16 +114,15 @@ func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
||||||
).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 +145,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 +157,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,7 +172,7 @@ 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,
|
||||||
|
@ -188,7 +182,7 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
_, 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 +203,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 +222,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 +243,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 +255,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,6 +277,8 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,7 +293,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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,
|
||||||
|
|
12
llm/llm.go
12
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
|
||||||
}
|
}
|
||||||
|
@ -86,15 +86,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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue