Compare commits
1 commit
main
...
feature_4_
Author | SHA1 | Date | |
---|---|---|---|
af8584b105 |
|
@ -13,11 +13,4 @@ WORKDIR /app
|
||||||
|
|
||||||
COPY --from=builder /build/app .
|
COPY --from=builder /build/app .
|
||||||
|
|
||||||
# Do not forget "/v1" in the end
|
|
||||||
ENV OPENAI_API_BASE_URL="" \
|
|
||||||
OPENAI_API_TOKEN="" \
|
|
||||||
TELEGRAM_TOKEN="" \
|
|
||||||
MODEL_TEXT_REQUEST="llama3.1:8b-instruct-q6_K" \
|
|
||||||
MODEL_SUMMARIZE_REQUEST="llama3.1:8b-instruct-q6_K"
|
|
||||||
|
|
||||||
CMD ["/app/app"]
|
CMD ["/app/app"]
|
||||||
|
|
|
@ -8,10 +8,8 @@
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
docker run \
|
docker run \
|
||||||
-e OPENAI_API_TOKEN=123 \
|
-e OLLAMA_TOKEN=123 \
|
||||||
-e OPENAI_API_BASE_URL=http://ollama.localhost:11434/v1 \
|
-e OLLAMA_BASE_URL=http://ollama.tld:11434 \
|
||||||
-e TELEGRAM_TOKEN=12345 \
|
-e TELEGRAM_TOKEN=12345 \
|
||||||
-e MODEL_TEXT_REQUEST=llama3.1:8b-instruct-q6_K
|
|
||||||
-e MODEL_SUMMARIZE_REQUEST=mistral-nemo:12b-instruct-2407-q4_K_M
|
|
||||||
skobkin/telegram-llm-bot
|
skobkin/telegram-llm-bot
|
||||||
```
|
```
|
||||||
|
|
195
bot/bot.go
195
bot/bot.go
|
@ -6,6 +6,7 @@ import (
|
||||||
th "github.com/mymmrac/telego/telegohandler"
|
th "github.com/mymmrac/telego/telegohandler"
|
||||||
tu "github.com/mymmrac/telego/telegoutil"
|
tu "github.com/mymmrac/telego/telegoutil"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"telegram-ollama-reply-bot/extractor"
|
"telegram-ollama-reply-bot/extractor"
|
||||||
"telegram-ollama-reply-bot/llm"
|
"telegram-ollama-reply-bot/llm"
|
||||||
|
@ -23,54 +24,42 @@ type Bot struct {
|
||||||
llm *llm.LlmConnector
|
llm *llm.LlmConnector
|
||||||
extractor *extractor.Extractor
|
extractor *extractor.Extractor
|
||||||
stats *stats.Stats
|
stats *stats.Stats
|
||||||
models ModelSelection
|
|
||||||
|
|
||||||
markdownV1Replacer *strings.Replacer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBot(
|
func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extractor) *Bot {
|
||||||
api *telego.Bot,
|
|
||||||
llm *llm.LlmConnector,
|
|
||||||
extractor *extractor.Extractor,
|
|
||||||
models ModelSelection,
|
|
||||||
) *Bot {
|
|
||||||
return &Bot{
|
return &Bot{
|
||||||
api: api,
|
api: api,
|
||||||
llm: llm,
|
llm: llm,
|
||||||
extractor: extractor,
|
extractor: extractor,
|
||||||
stats: stats.NewStats(),
|
stats: stats.NewStats(),
|
||||||
models: models,
|
|
||||||
|
|
||||||
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", "error", err)
|
slog.Error("Cannot retrieve api user", err)
|
||||||
|
|
||||||
return ErrGetMe
|
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)
|
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", 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", "error", err)
|
slog.Error("Cannot initialize bot handler", err)
|
||||||
|
|
||||||
return ErrHandlerInit
|
return ErrHandlerInit
|
||||||
}
|
}
|
||||||
|
@ -81,127 +70,28 @@ func (b *Bot) Run() error {
|
||||||
// Middlewares
|
// Middlewares
|
||||||
bh.Use(b.chatTypeStatsCounter)
|
bh.Use(b.chatTypeStatsCounter)
|
||||||
|
|
||||||
// Command handlers
|
// Handlers
|
||||||
bh.Handle(b.startHandler, th.CommandEqual("start"))
|
bh.Handle(b.startHandler, th.CommandEqual("start"))
|
||||||
bh.Handle(b.heyHandler, th.CommandEqual("hey"))
|
bh.Handle(b.heyHandler, th.CommandEqual("hey"))
|
||||||
bh.Handle(b.summarizeHandler, th.CommandEqual("summarize"))
|
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) inlineHandler(bot *telego.Bot, update telego.Update) {
|
|
||||||
iq := update.InlineQuery
|
|
||||||
slog.Info("inline query received", "query", iq.Query)
|
|
||||||
|
|
||||||
slog.Debug("query", "query", iq)
|
|
||||||
|
|
||||||
if len(iq.Query) < 3 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
b.stats.InlineQuery()
|
|
||||||
|
|
||||||
queryParts := strings.SplitN(iq.Query, " ", 2)
|
|
||||||
|
|
||||||
if len(queryParts) < 1 {
|
|
||||||
slog.Debug("Empty query. Skipping.")
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
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) heyHandler(bot *telego.Bot, update telego.Update) {
|
func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
||||||
slog.Info("/hey", "message-text", update.Message.Text)
|
slog.Info("/hey")
|
||||||
|
|
||||||
b.stats.HeyRequest()
|
b.stats.HeyRequest()
|
||||||
|
|
||||||
parts := strings.SplitN(update.Message.Text, " ", 2)
|
|
||||||
userMessage := "Hey!"
|
|
||||||
if len(parts) == 2 {
|
|
||||||
userMessage = parts[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
chatID := tu.ID(update.Message.Chat.ID)
|
chatID := tu.ID(update.Message.Chat.ID)
|
||||||
|
|
||||||
b.sendTyping(chatID)
|
b.sendTyping(chatID)
|
||||||
|
|
||||||
requestContext := createLlmRequestContextFromUpdate(update)
|
llmReply, err := b.llm.HandleSingleRequest(update.Message.Text, llm.ModelMistralUncensored)
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
|
@ -213,23 +103,22 @@ func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
slog.Debug("Got completion. Going to send.", llmReply)
|
||||||
|
|
||||||
message := tu.Message(
|
message := tu.Message(
|
||||||
chatID,
|
chatID,
|
||||||
b.escapeMarkdownV1Symbols(llmReply),
|
llmReply,
|
||||||
).WithParseMode("Markdown")
|
).WithParseMode("Markdown")
|
||||||
|
|
||||||
_, err = bot.SendMessage(b.reply(update.Message, message))
|
_, err = bot.SendMessage(b.reply(update.Message, message))
|
||||||
if err != nil {
|
|
||||||
slog.Error("Can't send reply message", "error", err)
|
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
if err != nil {
|
||||||
|
slog.Error("Can't send reply message", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
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()
|
b.stats.SummarizeRequest()
|
||||||
|
|
||||||
|
@ -237,7 +126,7 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
|
|
||||||
b.sendTyping(chatID)
|
b.sendTyping(chatID)
|
||||||
|
|
||||||
args := strings.SplitN(update.Message.Text, " ", 2)
|
args := strings.Split(update.Message.Text, " ")
|
||||||
|
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
_, _ = bot.SendMessage(tu.Message(
|
_, _ = bot.SendMessage(tu.Message(
|
||||||
|
@ -250,8 +139,9 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isValidAndAllowedUrl(args[1]) {
|
_, err := url.ParseRequestURI(args[1])
|
||||||
slog.Error("Provided text is not a valid URL", "text", args[1])
|
if err != nil {
|
||||||
|
slog.Error("Provided URL is not valid", args[1])
|
||||||
|
|
||||||
_, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
|
_, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
|
||||||
chatID,
|
chatID,
|
||||||
|
@ -263,10 +153,10 @@ 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", "error", err)
|
slog.Error("Cannot retrieve an article using extractor", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
llmReply, err := b.llm.Summarize(article.Text, b.models.SummarizeModel)
|
llmReply, err := b.llm.Summarize(article.Text, llm.ModelMistralUncensored)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Cannot get reply from LLM connector")
|
slog.Error("Cannot get reply from LLM connector")
|
||||||
|
|
||||||
|
@ -278,19 +168,17 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
slog.Debug("Got completion. Going to send.", llmReply)
|
||||||
|
|
||||||
message := tu.Message(
|
message := tu.Message(
|
||||||
chatID,
|
chatID,
|
||||||
b.escapeMarkdownV1Symbols(llmReply),
|
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", "error", err)
|
slog.Error("Can't send reply message", err)
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,9 +197,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", "error", err)
|
slog.Error("Cannot send a message", err)
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,9 +214,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", "error", err)
|
slog.Error("Cannot send a message", err)
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -349,12 +233,21 @@ 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", "error", err)
|
slog.Error("Cannot send a message", err)
|
||||||
|
|
||||||
b.trySendReplyError(update.Message)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) escapeMarkdownV1Symbols(input string) string {
|
func (b *Bot) reply(originalMessage *telego.Message, newMessage *telego.SendMessageParams) *telego.SendMessageParams {
|
||||||
return b.markdownV1Replacer.Replace(input)
|
return newMessage.WithReplyParameters(&telego.ReplyParameters{
|
||||||
|
MessageID: originalMessage.MessageID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) sendTyping(chatId telego.ChatID) {
|
||||||
|
slog.Info("Setting 'typing' chat action")
|
||||||
|
|
||||||
|
err := b.api.SendChatAction(tu.ChatAction(chatId, "typing"))
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Cannot set chat action", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,72 +0,0 @@
|
||||||
package bot
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/mymmrac/telego"
|
|
||||||
tu "github.com/mymmrac/telego/telegoutil"
|
|
||||||
"log/slog"
|
|
||||||
"net/url"
|
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
allowedUrlSchemes = []string{"http", "https"}
|
|
||||||
)
|
|
||||||
|
|
||||||
func (b *Bot) reply(originalMessage *telego.Message, newMessage *telego.SendMessageParams) *telego.SendMessageParams {
|
|
||||||
return newMessage.WithReplyParameters(&telego.ReplyParameters{
|
|
||||||
MessageID: originalMessage.MessageID,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) sendTyping(chatId telego.ChatID) {
|
|
||||||
slog.Debug("Setting 'typing' chat action")
|
|
||||||
|
|
||||||
err := b.api.SendChatAction(tu.ChatAction(chatId, "typing"))
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("Cannot set chat action", "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) trySendReplyError(message *telego.Message) {
|
|
||||||
if message == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _ = b.api.SendMessage(b.reply(message, tu.Message(
|
|
||||||
tu.ID(message.Chat.ID),
|
|
||||||
"Error occurred while trying to send reply.",
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) trySendInlineQueryError(iq *telego.InlineQuery, text string) {
|
|
||||||
if iq == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = b.api.AnswerInlineQuery(tu.InlineQuery(
|
|
||||||
iq.ID,
|
|
||||||
tu.ResultArticle(
|
|
||||||
string("error_"+iq.ID),
|
|
||||||
"Error: "+text,
|
|
||||||
tu.TextMessage(text),
|
|
||||||
),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
func isValidAndAllowedUrl(text string) bool {
|
|
||||||
u, err := url.ParseRequestURI(text)
|
|
||||||
if err != nil {
|
|
||||||
slog.Debug("Provided text is not an URL", "text", text)
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !slices.Contains(allowedUrlSchemes, strings.ToLower(u.Scheme)) {
|
|
||||||
slog.Debug("Provided URL has disallowed scheme", "scheme", u.Scheme, "allowed-schemes", allowedUrlSchemes)
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
|
@ -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 (
|
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()
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
package bot
|
|
||||||
|
|
||||||
type ModelSelection struct {
|
|
||||||
TextRequestModel string
|
|
||||||
SummarizeModel string
|
|
||||||
}
|
|
|
@ -1,60 +0,0 @@
|
||||||
package bot
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/mymmrac/telego"
|
|
||||||
"log/slog"
|
|
||||||
"telegram-ollama-reply-bot/llm"
|
|
||||||
)
|
|
||||||
|
|
||||||
func createLlmRequestContextFromUpdate(update telego.Update) llm.RequestContext {
|
|
||||||
message := update.Message
|
|
||||||
iq := update.InlineQuery
|
|
||||||
|
|
||||||
rc := llm.RequestContext{
|
|
||||||
Empty: true,
|
|
||||||
Inline: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case message == nil && iq == nil:
|
|
||||||
slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc)
|
|
||||||
|
|
||||||
return rc
|
|
||||||
case iq != nil:
|
|
||||||
rc.Inline = true
|
|
||||||
}
|
|
||||||
|
|
||||||
rc.Empty = false
|
|
||||||
|
|
||||||
var user *telego.User
|
|
||||||
|
|
||||||
if rc.Inline {
|
|
||||||
user = &iq.From
|
|
||||||
} else {
|
|
||||||
user = message.From
|
|
||||||
}
|
|
||||||
|
|
||||||
if user != nil {
|
|
||||||
rc.User = llm.UserContext{
|
|
||||||
Username: user.Username,
|
|
||||||
FirstName: user.FirstName,
|
|
||||||
LastName: user.LastName,
|
|
||||||
IsPremium: user.IsPremium,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !rc.Inline {
|
|
||||||
// TODO: implement retrieval of chat description
|
|
||||||
chat := message.Chat
|
|
||||||
rc.Chat = llm.ChatContext{
|
|
||||||
Title: chat.Title,
|
|
||||||
// TODO: fill when ChatFullInfo retrieved
|
|
||||||
//Description: chat.Description,
|
|
||||||
Type: chat.Type,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Debug("request context created", "request-context", rc)
|
|
||||||
|
|
||||||
return rc
|
|
||||||
}
|
|
27
config/config.go
Normal file
27
config/config.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Telegram TelegramConfig
|
||||||
|
Ollama OllamaConfig
|
||||||
|
Stats StatsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
type StatsConfig struct {
|
||||||
|
Enabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type TelegramConfig struct {
|
||||||
|
Token string
|
||||||
|
AdministratorIds []int64
|
||||||
|
OnlyAllowedChats bool
|
||||||
|
AllowedChats []int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type OllamaConfig struct {
|
||||||
|
BaseUrl string
|
||||||
|
Token string
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadFromEnvAndFile() Config {
|
||||||
|
|
||||||
|
}
|
|
@ -3,7 +3,6 @@ package extractor
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/advancedlogic/GoOse"
|
"github.com/advancedlogic/GoOse"
|
||||||
"log/slog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -29,18 +28,12 @@ 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,
|
||||||
|
|
68
llm/llm.go
68
llm/llm.go
|
@ -10,6 +10,8 @@ import (
|
||||||
var (
|
var (
|
||||||
ErrLlmBackendRequestFailed = errors.New("llm back-end request failed")
|
ErrLlmBackendRequestFailed = errors.New("llm back-end request failed")
|
||||||
ErrNoChoices = errors.New("no choices in LLM response")
|
ErrNoChoices = errors.New("no choices in LLM response")
|
||||||
|
|
||||||
|
ModelMistralUncensored = "dolphin-mistral"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LlmConnector struct {
|
type LlmConnector struct {
|
||||||
|
@ -27,21 +29,13 @@ func NewConnector(baseUrl string, token string) *LlmConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LlmConnector) HandleSingleRequest(text string, model string, requestContext RequestContext) (string, error) {
|
func (l *LlmConnector) HandleSingleRequest(text string, model string) (string, error) {
|
||||||
systemPrompt := "You're a bot in the Telegram chat.\n" +
|
|
||||||
"You're using a free model called \"" + model + "\".\n" +
|
|
||||||
"Currently you're not able to access chat history, so each message will be replied from a clean slate."
|
|
||||||
|
|
||||||
if !requestContext.Empty {
|
|
||||||
systemPrompt += "\n" + requestContext.Prompt()
|
|
||||||
}
|
|
||||||
|
|
||||||
req := openai.ChatCompletionRequest{
|
req := openai.ChatCompletionRequest{
|
||||||
Model: model,
|
Model: model,
|
||||||
Messages: []openai.ChatCompletionMessage{
|
Messages: []openai.ChatCompletionMessage{
|
||||||
{
|
{
|
||||||
Role: openai.ChatMessageRoleSystem,
|
Role: openai.ChatMessageRoleSystem,
|
||||||
Content: systemPrompt,
|
Content: "You're a bot in the Telegram chat. You are replying to questions directed to you.",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -53,15 +47,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: LLM back-end request failed", "error", err)
|
slog.Error("LLM back-end request failed", err)
|
||||||
|
|
||||||
return "", ErrLlmBackendRequestFailed
|
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 {
|
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
|
return "", ErrNoChoices
|
||||||
}
|
}
|
||||||
|
@ -75,11 +69,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're a text shortener. Give a very brief summary of the main facts " +
|
Content: "You are a short digest editor. Summarize the text you received " +
|
||||||
"point by point. Format them as a list of bullet points each starting with \"-\". " +
|
"as a list of bullet points with most important facts from the text. " +
|
||||||
"Avoid any commentaries and value judgement on the matter. " +
|
"If possible, use the same language as the original text.",
|
||||||
"If possible, respond in the same language as the original text." +
|
|
||||||
"Do not use any non-ASCII characters.",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -91,52 +83,18 @@ 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: LLM back-end request failed", "error", err)
|
slog.Error("LLM back-end request failed", err)
|
||||||
|
|
||||||
return "", ErrLlmBackendRequestFailed
|
return "", ErrLlmBackendRequestFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("llm: Received LLM back-end response", resp)
|
slog.Debug("Received LLM back-end response", resp)
|
||||||
|
|
||||||
if len(resp.Choices) < 1 {
|
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
|
return "", ErrNoChoices
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp.Choices[0].Message.Content, nil
|
return resp.Choices[0].Message.Content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LlmConnector) GetModels() []string {
|
|
||||||
var result []string
|
|
||||||
|
|
||||||
models, err := l.client.ListModels(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("llm: Model list request failed", "error", err)
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Info("Model list retrieved", "models", models)
|
|
||||||
|
|
||||||
for _, model := range models.Models {
|
|
||||||
result = append(result, model.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *LlmConnector) HasModel(id string) bool {
|
|
||||||
model, err := l.client.GetModel(context.Background(), id)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("llm: Model request failed", "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Debug("llm: Returned model", "model", model)
|
|
||||||
|
|
||||||
if model.ID != "" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,55 +0,0 @@
|
||||||
package llm
|
|
||||||
|
|
||||||
type RequestContext struct {
|
|
||||||
Empty bool
|
|
||||||
Inline bool
|
|
||||||
User UserContext
|
|
||||||
Chat ChatContext
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserContext struct {
|
|
||||||
Username string
|
|
||||||
FirstName string
|
|
||||||
LastName string
|
|
||||||
IsPremium bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type ChatContext struct {
|
|
||||||
Title string
|
|
||||||
Description string
|
|
||||||
Type string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c RequestContext) Prompt() string {
|
|
||||||
if c.Empty {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt := ""
|
|
||||||
if !c.Inline {
|
|
||||||
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.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 += "User profile:" +
|
|
||||||
"First name: \"" + c.User.FirstName + "\"\n"
|
|
||||||
if c.User.Username != "" {
|
|
||||||
prompt += "Username: @" + c.User.Username + ".\n"
|
|
||||||
}
|
|
||||||
if c.User.LastName != "" {
|
|
||||||
prompt += "Last name: \"" + c.User.LastName + "\"\n"
|
|
||||||
}
|
|
||||||
if c.User.IsPremium {
|
|
||||||
prompt += "Telegram Premium subscription: active."
|
|
||||||
}
|
|
||||||
|
|
||||||
return prompt
|
|
||||||
}
|
|
31
main.go
31
main.go
|
@ -12,44 +12,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
apiToken := os.Getenv("OPENAI_API_TOKEN")
|
ollamaToken := os.Getenv("OLLAMA_TOKEN")
|
||||||
apiBaseUrl := os.Getenv("OPENAI_API_BASE_URL")
|
ollamaBaseUrl := os.Getenv("OLLAMA_BASE_URL")
|
||||||
|
|
||||||
models := bot.ModelSelection{
|
|
||||||
TextRequestModel: os.Getenv("MODEL_TEXT_REQUEST"),
|
|
||||||
SummarizeModel: os.Getenv("MODEL_SUMMARIZE_REQUEST"),
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Info("Selected", "models", models)
|
|
||||||
|
|
||||||
telegramToken := os.Getenv("TELEGRAM_TOKEN")
|
telegramToken := os.Getenv("TELEGRAM_TOKEN")
|
||||||
|
|
||||||
llmc := llm.NewConnector(apiBaseUrl, apiToken)
|
llmc := llm.NewConnector(ollamaBaseUrl, ollamaToken)
|
||||||
|
|
||||||
slog.Info("Checking models availability")
|
|
||||||
|
|
||||||
for _, model := range []string{models.TextRequestModel, models.SummarizeModel} {
|
|
||||||
if !llmc.HasModel(model) {
|
|
||||||
slog.Error("Model not unavailable", "model", model)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Info("All needed models are available")
|
|
||||||
|
|
||||||
ext := extractor.NewExtractor()
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
botService := bot.NewBot(telegramApi, llmc, ext, models)
|
botService := bot.NewBot(telegramApi, llmc, ext)
|
||||||
|
|
||||||
err = botService.Run()
|
err = botService.Run()
|
||||||
if err != nil {
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ type Stats struct {
|
||||||
|
|
||||||
GroupRequests uint64
|
GroupRequests uint64
|
||||||
PrivateRequests uint64
|
PrivateRequests uint64
|
||||||
InlineQueries uint64
|
|
||||||
|
|
||||||
HeyRequests uint64
|
HeyRequests uint64
|
||||||
SummarizeRequests uint64
|
SummarizeRequests uint64
|
||||||
|
@ -25,7 +24,6 @@ func NewStats() *Stats {
|
||||||
|
|
||||||
GroupRequests: 0,
|
GroupRequests: 0,
|
||||||
PrivateRequests: 0,
|
PrivateRequests: 0,
|
||||||
InlineQueries: 0,
|
|
||||||
|
|
||||||
HeyRequests: 0,
|
HeyRequests: 0,
|
||||||
SummarizeRequests: 0,
|
SummarizeRequests: 0,
|
||||||
|
@ -38,7 +36,6 @@ func (s *Stats) MarshalJSON() ([]byte, error) {
|
||||||
|
|
||||||
GroupRequests uint64 `json:"group_requests"`
|
GroupRequests uint64 `json:"group_requests"`
|
||||||
PrivateRequests uint64 `json:"private_requests"`
|
PrivateRequests uint64 `json:"private_requests"`
|
||||||
InlineQueries uint64 `json:"inline_queries"`
|
|
||||||
|
|
||||||
HeyRequests uint64 `json:"hey_requests"`
|
HeyRequests uint64 `json:"hey_requests"`
|
||||||
SummarizeRequests uint64 `json:"summarize_requests"`
|
SummarizeRequests uint64 `json:"summarize_requests"`
|
||||||
|
@ -47,7 +44,6 @@ func (s *Stats) MarshalJSON() ([]byte, error) {
|
||||||
|
|
||||||
GroupRequests: s.GroupRequests,
|
GroupRequests: s.GroupRequests,
|
||||||
PrivateRequests: s.PrivateRequests,
|
PrivateRequests: s.PrivateRequests,
|
||||||
InlineQueries: s.InlineQueries,
|
|
||||||
|
|
||||||
HeyRequests: s.HeyRequests,
|
HeyRequests: s.HeyRequests,
|
||||||
SummarizeRequests: s.SummarizeRequests,
|
SummarizeRequests: s.SummarizeRequests,
|
||||||
|
@ -63,12 +59,6 @@ func (s *Stats) String() string {
|
||||||
return string(data)
|
return string(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stats) InlineQuery() {
|
|
||||||
s.mu.Lock()
|
|
||||||
defer s.mu.Unlock()
|
|
||||||
s.InlineQueries++
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stats) GroupRequest() {
|
func (s *Stats) GroupRequest() {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
Loading…
Reference in a new issue