Compare commits

..

1 Commits

Author SHA1 Message Date
Alexey Skobkin af8584b105
Configuration draft.
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details
2024-03-11 23:59:11 +03:00
12 changed files with 83 additions and 398 deletions

View File

@ -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
```

View File

@ -6,6 +6,7 @@ import (
th "github.com/mymmrac/telego/telegohandler"
tu "github.com/mymmrac/telego/telegoutil"
"log/slog"
"net/url"
"strings"
"telegram-ollama-reply-bot/extractor"
"telegram-ollama-reply-bot/llm"
@ -23,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 {
@ -33,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
}
@ -74,127 +70,28 @@ func (b *Bot) Run() error {
// Middlewares
bh.Use(b.chatTypeStatsCounter)
// Command handlers
// Handlers
bh.Handle(b.startHandler, th.CommandEqual("start"))
bh.Handle(b.heyHandler, th.CommandEqual("hey"))
bh.Handle(b.summarizeHandler, th.CommandEqual("summarize"))
bh.Handle(b.statsHandler, th.CommandEqual("stats"))
bh.Handle(b.helpHandler, th.CommandEqual("help"))
// Inline query handlers
bh.Handle(b.inlineHandler, th.AnyInlineQuery())
bh.Start()
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, llm.ModelLlama3Uncensored )
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, llm.ModelLlama3Uncensored, 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) {
slog.Info("/hey", "message-text", update.Message.Text)
slog.Info("/hey")
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)
b.sendTyping(chatID)
requestContext := createLlmRequestContextFromUpdate(update)
llmReply, err := b.llm.HandleSingleRequest(userMessage, llm.ModelLlama3Uncensored, requestContext)
llmReply, err := b.llm.HandleSingleRequest(update.Message.Text, llm.ModelMistralUncensored)
if err != nil {
slog.Error("Cannot get reply from LLM connector")
@ -206,23 +103,22 @@ func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
return
}
slog.Debug("Got completion. Going to send.", "llm-completion", 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)
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) {
slog.Info("/summarize", "message-text", update.Message.Text)
slog.Info("/summarize", update.Message.Text)
b.stats.SummarizeRequest()
@ -230,7 +126,7 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
b.sendTyping(chatID)
args := strings.SplitN(update.Message.Text, " ", 2)
args := strings.Split(update.Message.Text, " ")
if len(args) < 2 {
_, _ = bot.SendMessage(tu.Message(
@ -243,8 +139,9 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
return
}
if !isValidAndAllowedUrl(args[1]) {
slog.Error("Provided text is not a valid URL", "text", args[1])
_, err := url.ParseRequestURI(args[1])
if err != nil {
slog.Error("Provided URL is not valid", args[1])
_, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
chatID,
@ -256,7 +153,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)
@ -271,19 +168,17 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
return
}
slog.Debug("Got completion. Going to send.", "llm-completion", 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)
b.trySendReplyError(update.Message)
slog.Error("Can't send reply message", err)
}
}
@ -302,9 +197,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)
b.trySendReplyError(update.Message)
slog.Error("Cannot send a message", err)
}
}
@ -321,9 +214,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)
b.trySendReplyError(update.Message)
slog.Error("Cannot send a message", err)
}
}
@ -342,12 +233,21 @@ func (b *Bot) statsHandler(bot *telego.Bot, update telego.Update) {
"```",
)).WithParseMode("Markdown"))
if err != nil {
slog.Error("Cannot send a message", "error", err)
b.trySendReplyError(update.Message)
slog.Error("Cannot send a message", err)
}
}
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,
})
}
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)
}
}

View File

@ -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
}

View File

@ -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))
}

View File

@ -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()

View File

@ -1,58 +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 {
chat := message.Chat
rc.Chat = llm.ChatContext{
Title: chat.Title,
Description: chat.Description,
Type: chat.Type,
}
}
slog.Debug("request context created", "request-context", rc)
return rc
}

27
config/config.go Normal file
View 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 {
}

View File

@ -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,

View File

@ -11,8 +11,7 @@ var (
ErrLlmBackendRequestFailed = errors.New("llm back-end request failed")
ErrNoChoices = errors.New("no choices in LLM response")
ModelMistralUncensored = "dolphin-mistral:7b-v2.8-q4_K_M"
ModelLlama3Uncensored = "dolphin-llama3:8b-v2.9-q4_K_M"
ModelMistralUncensored = "dolphin-mistral"
)
type LlmConnector struct {
@ -30,21 +29,13 @@ func NewConnector(baseUrl string, token string) *LlmConnector {
}
}
func (l *LlmConnector) HandleSingleRequest(text string, model string, requestContext RequestContext) (string, error) {
systemPrompt := "You're a bot in the Telegram chat. " +
"You're using a free model called \"" + model + "\". " +
"You see only messages addressed to you using commands due to privacy settings."
if !requestContext.Empty {
systemPrompt += " " + requestContext.Prompt()
}
func (l *LlmConnector) HandleSingleRequest(text string, model string) (string, error) {
req := openai.ChatCompletionRequest{
Model: model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: systemPrompt,
Content: "You're a bot in the Telegram chat. You are replying to questions directed to you.",
},
},
}
@ -56,15 +47,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
}
@ -78,9 +69,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.",
},
},
@ -93,15 +83,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
}

View File

@ -1,54 +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 += "According to their profile, first name of the user who wrote you is \"" + c.User.FirstName + "\". "
if c.User.Username != "" {
prompt += "Their username is @" + c.User.Username + ". "
}
if c.User.LastName != "" {
prompt += "Their last name is \"" + c.User.LastName + "\". "
}
if c.User.IsPremium {
prompt += "They have Telegram Premium subscription. "
}
return prompt
}

View File

@ -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)
}
}

View File

@ -13,7 +13,6 @@ type Stats struct {
GroupRequests uint64
PrivateRequests uint64
InlineQueries uint64
HeyRequests uint64
SummarizeRequests uint64
@ -25,7 +24,6 @@ func NewStats() *Stats {
GroupRequests: 0,
PrivateRequests: 0,
InlineQueries: 0,
HeyRequests: 0,
SummarizeRequests: 0,
@ -38,7 +36,6 @@ func (s *Stats) MarshalJSON() ([]byte, error) {
GroupRequests uint64 `json:"group_requests"`
PrivateRequests uint64 `json:"private_requests"`
InlineQueries uint64 `json:"inline_queries"`
HeyRequests uint64 `json:"hey_requests"`
SummarizeRequests uint64 `json:"summarize_requests"`
@ -47,7 +44,6 @@ func (s *Stats) MarshalJSON() ([]byte, error) {
GroupRequests: s.GroupRequests,
PrivateRequests: s.PrivateRequests,
InlineQueries: s.InlineQueries,
HeyRequests: s.HeyRequests,
SummarizeRequests: s.SummarizeRequests,
@ -63,12 +59,6 @@ func (s *Stats) String() string {
return string(data)
}
func (s *Stats) InlineQuery() {
s.mu.Lock()
defer s.mu.Unlock()
s.InlineQueries++
}
func (s *Stats) GroupRequest() {
s.mu.Lock()
defer s.mu.Unlock()