Compare commits
1 commit
main
...
feature_4_
Author | SHA1 | Date | |
---|---|---|---|
af8584b105 |
|
@ -13,11 +13,4 @@ WORKDIR /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"]
|
||||
|
|
19
README.md
19
README.md
|
@ -1,24 +1,15 @@
|
|||
# Telegram Ollama Bot
|
||||
# telegram-ollama-reply-bot
|
||||
|
||||
[![Build Status](https://ci.skobk.in/api/badges/skobkin/telegram-ollama-reply-bot/status.svg)](https://ci.skobk.in/skobkin/telegram-ollama-reply-bot)
|
||||
|
||||
![Project Banner](/img/banner.jpeg)
|
||||
# Usage
|
||||
|
||||
## Functionality
|
||||
|
||||
- Context-dependent dialogue in chats
|
||||
- Summarization of articles by provided link
|
||||
|
||||
## Usage
|
||||
|
||||
### Docker
|
||||
## Docker
|
||||
|
||||
```shell
|
||||
docker run \
|
||||
-e OPENAI_API_TOKEN=123 \
|
||||
-e OPENAI_API_BASE_URL=http://ollama.localhost:11434/v1 \
|
||||
-e OLLAMA_TOKEN=123 \
|
||||
-e OLLAMA_BASE_URL=http://ollama.tld:11434 \
|
||||
-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
|
||||
```
|
||||
|
|
172
bot/bot.go
172
bot/bot.go
|
@ -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"
|
||||
|
@ -18,75 +19,47 @@ var (
|
|||
ErrHandlerInit = errors.New("cannot initialize handler")
|
||||
)
|
||||
|
||||
type BotInfo struct {
|
||||
Id int64
|
||||
Username string
|
||||
Name string
|
||||
}
|
||||
|
||||
type Bot struct {
|
||||
api *telego.Bot
|
||||
llm *llm.LlmConnector
|
||||
extractor *extractor.Extractor
|
||||
stats *stats.Stats
|
||||
models ModelSelection
|
||||
history map[int64]*MessageHistory
|
||||
profile BotInfo
|
||||
|
||||
markdownV1Replacer *strings.Replacer
|
||||
}
|
||||
|
||||
func NewBot(
|
||||
api *telego.Bot,
|
||||
llm *llm.LlmConnector,
|
||||
extractor *extractor.Extractor,
|
||||
models ModelSelection,
|
||||
) *Bot {
|
||||
func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extractor) *Bot {
|
||||
return &Bot{
|
||||
api: api,
|
||||
llm: llm,
|
||||
extractor: extractor,
|
||||
stats: stats.NewStats(),
|
||||
models: models,
|
||||
history: make(map[int64]*MessageHistory),
|
||||
profile: BotInfo{0, "", ""},
|
||||
|
||||
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)
|
||||
|
||||
b.profile = BotInfo{
|
||||
Id: botUser.ID,
|
||||
Username: botUser.Username,
|
||||
Name: botUser.FirstName,
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -95,66 +68,34 @@ func (b *Bot) Run() error {
|
|||
defer b.api.StopLongPolling()
|
||||
|
||||
// Middlewares
|
||||
bh.Use(b.chatHistory)
|
||||
bh.Use(b.chatTypeStatsCounter)
|
||||
|
||||
// Command handlers
|
||||
// Handlers
|
||||
bh.Handle(b.startHandler, th.CommandEqual("start"))
|
||||
bh.Handle(b.summarizeHandler, th.Or(th.CommandEqual("summarize"), th.CommandEqual("s")))
|
||||
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"))
|
||||
bh.Handle(b.textMessageHandler, th.AnyMessageWithText())
|
||||
|
||||
bh.Start()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bot) textMessageHandler(bot *telego.Bot, update telego.Update) {
|
||||
slog.Debug("/any-message")
|
||||
func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
||||
slog.Info("/hey")
|
||||
|
||||
message := update.Message
|
||||
b.stats.HeyRequest()
|
||||
|
||||
switch {
|
||||
// Mentions
|
||||
case b.isMentionOfMe(update):
|
||||
slog.Info("/any-message", "type", "mention")
|
||||
b.processMention(message)
|
||||
// Replies
|
||||
case b.isReplyToMe(update):
|
||||
slog.Info("/any-message", "type", "reply")
|
||||
b.processMention(message)
|
||||
// Private chat
|
||||
case b.isPrivateWithMe(update):
|
||||
slog.Info("/any-message", "type", "private")
|
||||
b.processMention(message)
|
||||
default:
|
||||
slog.Debug("/any-message", "info", "MessageData is not mention, reply or private chat. Skipping.")
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) processMention(message *telego.Message) {
|
||||
b.stats.Mention()
|
||||
|
||||
slog.Info("/mention", "chat", message.Chat.ID)
|
||||
|
||||
chatID := tu.ID(message.Chat.ID)
|
||||
chatID := tu.ID(update.Message.Chat.ID)
|
||||
|
||||
b.sendTyping(chatID)
|
||||
|
||||
requestContext := b.createLlmRequestContextFromMessage(message)
|
||||
|
||||
userMessageData := tgUserMessageToMessageData(message, true)
|
||||
|
||||
llmReply, err := b.llm.HandleChatMessage(
|
||||
messageDataToLlmMessage(userMessageData),
|
||||
b.models.TextRequestModel,
|
||||
requestContext,
|
||||
)
|
||||
llmReply, err := b.llm.HandleSingleRequest(update.Message.Text, llm.ModelMistralUncensored)
|
||||
if err != nil {
|
||||
slog.Error("Cannot get reply from LLM connector")
|
||||
|
||||
_, _ = b.api.SendMessage(b.reply(message, tu.Message(
|
||||
_, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
|
||||
chatID,
|
||||
"LLM request error. Try again later.",
|
||||
)))
|
||||
|
@ -162,27 +103,22 @@ func (b *Bot) processMention(message *telego.Message) {
|
|||
return
|
||||
}
|
||||
|
||||
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
||||
slog.Debug("Got completion. Going to send.", llmReply)
|
||||
|
||||
reply := tu.Message(
|
||||
message := tu.Message(
|
||||
chatID,
|
||||
b.escapeMarkdownV1Symbols(llmReply),
|
||||
llmReply,
|
||||
).WithParseMode("Markdown")
|
||||
|
||||
_, err = b.api.SendMessage(b.reply(message, reply))
|
||||
_, err = bot.SendMessage(b.reply(update.Message, message))
|
||||
|
||||
if err != nil {
|
||||
slog.Error("Can't send reply message", "error", err)
|
||||
|
||||
b.trySendReplyError(message)
|
||||
|
||||
return
|
||||
slog.Error("Can't send reply message", err)
|
||||
}
|
||||
|
||||
b.saveBotReplyToHistory(message, llmReply)
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
|
@ -190,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(
|
||||
|
@ -203,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,
|
||||
|
@ -216,10 +153,10 @@ 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, b.models.SummarizeModel)
|
||||
llmReply, err := b.llm.Summarize(article.Text, llm.ModelMistralUncensored)
|
||||
if err != nil {
|
||||
slog.Error("Cannot get reply from LLM connector")
|
||||
|
||||
|
@ -231,24 +168,18 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
|||
return
|
||||
}
|
||||
|
||||
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
||||
|
||||
replyMarkdown := b.escapeMarkdownV1Symbols(llmReply)
|
||||
slog.Debug("Got completion. Going to send.", llmReply)
|
||||
|
||||
message := tu.Message(
|
||||
chatID,
|
||||
replyMarkdown,
|
||||
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)
|
||||
}
|
||||
|
||||
b.saveBotReplyToHistory(update.Message, replyMarkdown)
|
||||
}
|
||||
|
||||
func (b *Bot) helpHandler(bot *telego.Bot, update telego.Update) {
|
||||
|
@ -263,14 +194,10 @@ func (b *Bot) helpHandler(bot *telego.Bot, update telego.Update) {
|
|||
"Instructions:\r\n"+
|
||||
"/hey <text> - Ask something from LLM\r\n"+
|
||||
"/summarize <link> - Summarize text from the provided link\r\n"+
|
||||
"/s <link> - Shorter version\r\n"+
|
||||
"/help - Show this help\r\n\r\n"+
|
||||
"Mention bot or reply to it's message to communicate with it",
|
||||
"/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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -287,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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -308,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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,93 +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) isMentionOfMe(update telego.Update) bool {
|
||||
if update.Message == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return strings.Contains(update.Message.Text, "@"+b.profile.Username)
|
||||
}
|
||||
|
||||
func (b *Bot) isReplyToMe(update telego.Update) bool {
|
||||
message := update.Message
|
||||
|
||||
if message == nil {
|
||||
return false
|
||||
}
|
||||
if message.ReplyToMessage == nil {
|
||||
return false
|
||||
}
|
||||
if message.ReplyToMessage.From == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
replyToMessage := message.ReplyToMessage
|
||||
|
||||
return replyToMessage != nil && replyToMessage.From.ID == b.profile.Id
|
||||
}
|
||||
|
||||
func (b *Bot) isPrivateWithMe(update telego.Update) bool {
|
||||
message := update.Message
|
||||
|
||||
if message == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return message.Chat.Type == telego.ChatTypePrivate
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
|
@ -1,126 +0,0 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"github.com/mymmrac/telego"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
const HistoryLength = 150
|
||||
|
||||
type MessageData struct {
|
||||
Name string
|
||||
Username string
|
||||
Text string
|
||||
IsMe bool
|
||||
IsUserRequest bool
|
||||
ReplyTo *MessageData
|
||||
}
|
||||
|
||||
type MessageHistory struct {
|
||||
messages []MessageData
|
||||
capacity int
|
||||
}
|
||||
|
||||
func NewMessageHistory(capacity int) *MessageHistory {
|
||||
return &MessageHistory{
|
||||
messages: make([]MessageData, 0, capacity),
|
||||
capacity: capacity,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *MessageHistory) Push(element MessageData) {
|
||||
if len(b.messages) >= b.capacity {
|
||||
b.messages = b.messages[1:]
|
||||
}
|
||||
|
||||
b.messages = append(b.messages, element)
|
||||
}
|
||||
|
||||
func (b *MessageHistory) GetAll() []MessageData {
|
||||
return b.messages
|
||||
}
|
||||
|
||||
func (b *Bot) saveChatMessageToHistory(message *telego.Message) {
|
||||
chatId := message.Chat.ID
|
||||
|
||||
slog.Info(
|
||||
"history-message-save",
|
||||
"chat", chatId,
|
||||
"from_id", message.From.ID,
|
||||
"from_name", message.From.FirstName,
|
||||
"text", message.Text,
|
||||
)
|
||||
|
||||
_, ok := b.history[chatId]
|
||||
if !ok {
|
||||
b.history[chatId] = NewMessageHistory(HistoryLength)
|
||||
}
|
||||
|
||||
msgData := tgUserMessageToMessageData(message, false)
|
||||
|
||||
b.history[chatId].Push(msgData)
|
||||
}
|
||||
|
||||
func (b *Bot) saveBotReplyToHistory(replyTo *telego.Message, text string) {
|
||||
chatId := replyTo.Chat.ID
|
||||
|
||||
slog.Info(
|
||||
"history-reply-save",
|
||||
"chat", chatId,
|
||||
"to_id", replyTo.From.ID,
|
||||
"to_name", replyTo.From.FirstName,
|
||||
"text", text,
|
||||
)
|
||||
|
||||
_, ok := b.history[chatId]
|
||||
if !ok {
|
||||
b.history[chatId] = NewMessageHistory(HistoryLength)
|
||||
}
|
||||
|
||||
msgData := MessageData{
|
||||
Name: b.profile.Name,
|
||||
Username: b.profile.Username,
|
||||
Text: text,
|
||||
IsMe: true,
|
||||
}
|
||||
|
||||
if replyTo.ReplyToMessage != nil {
|
||||
replyMessage := replyTo.ReplyToMessage
|
||||
|
||||
msgData.ReplyTo = &MessageData{
|
||||
Name: replyMessage.From.FirstName,
|
||||
Username: replyMessage.From.Username,
|
||||
Text: replyMessage.Text,
|
||||
IsMe: false,
|
||||
ReplyTo: nil,
|
||||
}
|
||||
}
|
||||
|
||||
b.history[chatId].Push(msgData)
|
||||
}
|
||||
|
||||
func tgUserMessageToMessageData(message *telego.Message, isUserRequest bool) MessageData {
|
||||
msgData := MessageData{
|
||||
Name: message.From.FirstName,
|
||||
Username: message.From.Username,
|
||||
Text: message.Text,
|
||||
IsMe: false,
|
||||
IsUserRequest: isUserRequest,
|
||||
}
|
||||
|
||||
if message.ReplyToMessage != nil {
|
||||
replyData := tgUserMessageToMessageData(message.ReplyToMessage, false)
|
||||
msgData.ReplyTo = &replyData
|
||||
}
|
||||
|
||||
return msgData
|
||||
}
|
||||
|
||||
func (b *Bot) getChatHistory(chatId int64) []MessageData {
|
||||
_, ok := b.history[chatId]
|
||||
if !ok {
|
||||
return make([]MessageData, 0)
|
||||
}
|
||||
|
||||
return b.history[chatId].GetAll()
|
||||
}
|
|
@ -3,48 +3,21 @@ 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("stats-middleware: update has no message. skipping.")
|
||||
|
||||
next(bot, update)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
switch message.Chat.Type {
|
||||
case telego.ChatTypeGroup, telego.ChatTypeSupergroup:
|
||||
if b.isMentionOfMe(update) || b.isReplyToMe(update) {
|
||||
slog.Info("stats-middleware: counting message chat type in stats", "type", message.Chat.Type)
|
||||
b.stats.GroupRequest()
|
||||
}
|
||||
b.stats.GroupRequest()
|
||||
case telego.ChatTypePrivate:
|
||||
slog.Info("stats-middleware: counting message chat type in stats", "type", message.Chat.Type)
|
||||
b.stats.PrivateRequest()
|
||||
}
|
||||
|
||||
next(bot, update)
|
||||
}
|
||||
|
||||
func (b *Bot) chatHistory(bot *telego.Bot, update telego.Update, next telegohandler.Handler) {
|
||||
message := update.Message
|
||||
|
||||
if message == nil {
|
||||
slog.Info("chat-history-middleware: update has no message. skipping.")
|
||||
|
||||
next(bot, update)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("chat-history-middleware: saving message to history for", "chat_id", message.Chat.ID)
|
||||
|
||||
b.saveChatMessageToHistory(message)
|
||||
|
||||
next(bot, update)
|
||||
}
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
package bot
|
||||
|
||||
type ModelSelection struct {
|
||||
TextRequestModel string
|
||||
SummarizeModel string
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"github.com/mymmrac/telego"
|
||||
"log/slog"
|
||||
"telegram-ollama-reply-bot/llm"
|
||||
)
|
||||
|
||||
func (b *Bot) createLlmRequestContextFromMessage(message *telego.Message) llm.RequestContext {
|
||||
rc := llm.RequestContext{
|
||||
Empty: true,
|
||||
}
|
||||
|
||||
if message == nil {
|
||||
slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc)
|
||||
|
||||
return rc
|
||||
}
|
||||
|
||||
rc.Empty = false
|
||||
|
||||
user := message.From
|
||||
|
||||
if user != nil {
|
||||
rc.User = llm.UserContext{
|
||||
Username: user.Username,
|
||||
FirstName: user.FirstName,
|
||||
LastName: user.LastName,
|
||||
IsPremium: user.IsPremium,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: implement retrieval of chat description
|
||||
chat := message.Chat
|
||||
|
||||
history := b.getChatHistory(chat.ID)
|
||||
|
||||
rc.Chat = llm.ChatContext{
|
||||
Title: chat.Title,
|
||||
// TODO: fill when ChatFullInfo retrieved
|
||||
//Description: chat.Description,
|
||||
Type: chat.Type,
|
||||
History: historyToLlmMessages(history),
|
||||
}
|
||||
|
||||
slog.Debug("request context created", "request-context", rc)
|
||||
|
||||
return rc
|
||||
}
|
||||
|
||||
func historyToLlmMessages(history []MessageData) []llm.ChatMessage {
|
||||
length := len(history)
|
||||
|
||||
if length > 0 {
|
||||
result := make([]llm.ChatMessage, 0, length)
|
||||
|
||||
for _, msg := range history {
|
||||
result = append(result, messageDataToLlmMessage(msg))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
return make([]llm.ChatMessage, 0)
|
||||
}
|
||||
|
||||
func messageDataToLlmMessage(data MessageData) llm.ChatMessage {
|
||||
llmMessage := llm.ChatMessage{
|
||||
Name: data.Name,
|
||||
Username: data.Username,
|
||||
Text: data.Text,
|
||||
IsMe: data.IsMe,
|
||||
IsUserRequest: data.IsUserRequest,
|
||||
}
|
||||
|
||||
if data.ReplyTo != nil {
|
||||
replyMessage := messageDataToLlmMessage(*data.ReplyTo)
|
||||
llmMessage.ReplyTo = &replyMessage
|
||||
}
|
||||
|
||||
return llmMessage
|
||||
}
|
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 (
|
||||
"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,
|
||||
|
|
29
go.mod
29
go.mod
|
@ -1,45 +1,40 @@
|
|||
module telegram-ollama-reply-bot
|
||||
|
||||
go 1.22.3
|
||||
|
||||
toolchain go1.23.2
|
||||
go 1.22.0
|
||||
|
||||
require (
|
||||
github.com/advancedlogic/GoOse v0.0.0-20231203033844-ae6b36caf275
|
||||
github.com/mymmrac/telego v0.31.4
|
||||
github.com/sashabaranov/go-openai v1.32.5
|
||||
github.com/mymmrac/telego v0.29.1
|
||||
github.com/sashabaranov/go-openai v1.20.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/PuerkitoBio/goquery v1.4.1 // indirect
|
||||
github.com/andybalholm/brotli v1.1.1 // indirect
|
||||
github.com/andybalholm/brotli v1.1.0 // indirect
|
||||
github.com/andybalholm/cascadia v1.0.0 // indirect
|
||||
github.com/araddon/dateparse v0.0.0-20180729174819-cfd92a431d0e // indirect
|
||||
github.com/bytedance/sonic v1.12.3 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.0 // indirect
|
||||
github.com/bytedance/sonic v1.10.2 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
|
||||
github.com/chenzhuoyu/iasm v0.9.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/fasthttp/router v1.5.2 // indirect
|
||||
github.com/fasthttp/router v1.4.22 // indirect
|
||||
github.com/fatih/set v0.2.1 // indirect
|
||||
github.com/gigawattio/window v0.0.0-20180317192513-0f5467e35573 // indirect
|
||||
github.com/go-resty/resty/v2 v2.0.0 // indirect
|
||||
github.com/grbit/go-json v0.11.0 // indirect
|
||||
github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0 // indirect
|
||||
github.com/klauspost/compress v1.17.11 // indirect
|
||||
github.com/klauspost/compress v1.17.6 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.3 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84 // indirect
|
||||
github.com/pkg/errors v0.8.1 // indirect
|
||||
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 // indirect
|
||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
|
||||
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasthttp v1.57.0 // indirect
|
||||
github.com/valyala/fasthttp v1.52.0 // indirect
|
||||
github.com/valyala/fastjson v1.6.4 // indirect
|
||||
golang.org/x/arch v0.6.0 // indirect
|
||||
golang.org/x/net v0.30.0 // indirect
|
||||
golang.org/x/sys v0.26.0 // indirect
|
||||
golang.org/x/text v0.19.0 // indirect
|
||||
golang.org/x/net v0.21.0 // indirect
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
)
|
||||
|
|
32
go.sum
32
go.sum
|
@ -4,8 +4,6 @@ github.com/advancedlogic/GoOse v0.0.0-20231203033844-ae6b36caf275 h1:Kuhf+w+ilOG
|
|||
github.com/advancedlogic/GoOse v0.0.0-20231203033844-ae6b36caf275/go.mod h1:98NztIIMIntZGtQVIs8H85Q5b88fTbwWFbLz/lM9/xU=
|
||||
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
|
||||
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
|
||||
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
|
||||
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
|
||||
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
||||
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||
github.com/araddon/dateparse v0.0.0-20180729174819-cfd92a431d0e h1:s05JG2GwtJMHaPcXDpo4V35TFgyYZzNsmBlSkHPEbeg=
|
||||
|
@ -14,11 +12,6 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1
|
|||
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
|
||||
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
|
||||
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
|
||||
github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU=
|
||||
github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM=
|
||||
github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
|
||||
|
@ -26,17 +19,11 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpV
|
|||
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
|
||||
github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0=
|
||||
github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fasthttp/router v1.4.22 h1:qwWcYBbndVDwts4dKaz+A2ehsnbKilmiP6pUhXBfYKo=
|
||||
github.com/fasthttp/router v1.4.22/go.mod h1:KeMvHLqhlB9vyDWD5TSvTccl9qeWrjSSiTJrJALHKV0=
|
||||
github.com/fasthttp/router v1.5.2 h1:ckJCCdV7hWkkrMeId3WfEhz+4Gyyf6QPwxi/RHIMZ6I=
|
||||
github.com/fasthttp/router v1.5.2/go.mod h1:C8EY53ozOwpONyevc/V7Gr8pqnEjwnkFFqPo1alAGs0=
|
||||
github.com/fatih/set v0.2.1 h1:nn2CaJyknWE/6txyUDGwysr3G5QC6xWB/PtVjPBbeaA=
|
||||
github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI=
|
||||
github.com/gigawattio/window v0.0.0-20180317192513-0f5467e35573 h1:u8AQ9bPa9oC+8/A/jlWouakhIvkFfuxgIIRjiy8av7I=
|
||||
|
@ -49,8 +36,6 @@ github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0 h1:xqgexXAGQgY
|
|||
github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
|
||||
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
|
||||
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
|
||||
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
|
||||
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
|
||||
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
|
@ -59,8 +44,6 @@ github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8Bz
|
|||
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||
github.com/mymmrac/telego v0.29.1 h1:nsNnK0mS18OL+unoDjDI6BVfafJBbT8Wtj7rCzEWoM8=
|
||||
github.com/mymmrac/telego v0.29.1/go.mod h1:ZLD1+L2TQRr97NPOCoN1V2w8y9kmFov33OfZ3qT8cF4=
|
||||
github.com/mymmrac/telego v0.31.4 h1:NpiNl0P/8eydknka/k6XaaaWVj5BKMlM3Ibba63QTBU=
|
||||
github.com/mymmrac/telego v0.31.4/go.mod h1:T12js1PgbYDYznvoN05MSMuPMfWTYo7D9LKl5cPFWiI=
|
||||
github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84 h1:fiKJgB4JDUd43CApkmCeTSQlWjtTtABrU2qsgbuP0BI=
|
||||
github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
|
@ -69,12 +52,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
|||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sashabaranov/go-openai v1.20.2 h1:nilzF2EKzaHyK4Rk2Dbu/aJEZbtIvskDIXvfS4yx+6M=
|
||||
github.com/sashabaranov/go-openai v1.20.2/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||
github.com/sashabaranov/go-openai v1.32.5 h1:/eNVa8KzlE7mJdKPZDj6886MUzZQjoVHyn0sLvIt5qA=
|
||||
github.com/sashabaranov/go-openai v1.32.5/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk=
|
||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
|
||||
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 h1:D0vL7YNisV2yqE55+q0lFuGse6U8lxlg7fYTctlT5Gc=
|
||||
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38/go.mod h1:sM7Mt7uEoCeFSCBM+qBrqvEo+/9vdmj19wzp3yzUhmg=
|
||||
github.com/simplereach/timeutils v1.2.0 h1:btgOAlu9RW6de2r2qQiONhjgxdAG7BL6je0G6J/yPnA=
|
||||
github.com/simplereach/timeutils v1.2.0/go.mod h1:VVbQDfN/FHRZa1LSqcwo4kNZ62OOyqLLGQKYB3pB0Q8=
|
||||
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
|
||||
|
@ -89,22 +68,17 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
|
|||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0=
|
||||
github.com/valyala/fasthttp v1.52.0/go.mod h1:hf5C4QnVMkNXMspnsUlfM3WitlgYflyhHYoKol/szxQ=
|
||||
github.com/valyala/fasthttp v1.57.0 h1:Xw8SjWGEP/+wAAgyy5XTvgrWlOD1+TxbbvNADYCm1Tg=
|
||||
github.com/valyala/fasthttp v1.57.0/go.mod h1:h6ZBaPRlzpZ6O3H5t2gEk1Qi33+TmLvfwgLLp0t9CpE=
|
||||
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
|
||||
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
|
||||
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
|
||||
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
|
||||
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc=
|
||||
golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
|
@ -123,8 +97,6 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
|||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
|
||||
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
|
@ -138,8 +110,6 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
|
@ -153,8 +123,6 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
|||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
|
|
BIN
img/banner.jpeg
BIN
img/banner.jpeg
Binary file not shown.
Before Width: | Height: | Size: 34 KiB |
80
llm/llm.go
80
llm/llm.go
|
@ -5,13 +5,13 @@ import (
|
|||
"errors"
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrLlmBackendRequestFailed = errors.New("llm back-end request failed")
|
||||
ErrNoChoices = errors.New("no choices in LLM response")
|
||||
|
||||
ModelMistralUncensored = "dolphin-mistral"
|
||||
)
|
||||
|
||||
type LlmConnector struct {
|
||||
|
@ -29,46 +29,33 @@ func NewConnector(baseUrl string, token string) *LlmConnector {
|
|||
}
|
||||
}
|
||||
|
||||
func (l *LlmConnector) HandleChatMessage(userMessage ChatMessage, model string, requestContext RequestContext) (string, error) {
|
||||
systemPrompt := "You're a bot in the Telegram chat.\n" +
|
||||
"You're using a free model called \"" + model + "\".\n\n" +
|
||||
requestContext.Prompt()
|
||||
|
||||
historyLength := len(requestContext.Chat.History)
|
||||
|
||||
if historyLength > 0 {
|
||||
systemPrompt += "\nYou have access to last " + strconv.Itoa(historyLength) + " messages in this chat."
|
||||
}
|
||||
|
||||
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.",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if historyLength > 0 {
|
||||
for _, msg := range requestContext.Chat.History {
|
||||
req.Messages = append(req.Messages, chatMessageToOpenAiChatCompletionMessage(msg))
|
||||
}
|
||||
}
|
||||
|
||||
req.Messages = append(req.Messages, chatMessageToOpenAiChatCompletionMessage(userMessage))
|
||||
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: text,
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -82,11 +69,9 @@ 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 each starting with \"-\". " +
|
||||
"Avoid any commentaries and value judgement on the matter. " +
|
||||
"If possible, respond in the same language as the original text." +
|
||||
"Do not use any non-ASCII characters.",
|
||||
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.",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -98,49 +83,18 @@ 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
|
||||
}
|
||||
|
||||
return resp.Choices[0].Message.Content, nil
|
||||
}
|
||||
|
||||
func (l *LlmConnector) HasAllModels(modelIds []string) (bool, map[string]bool) {
|
||||
modelList, err := l.client.ListModels(context.Background())
|
||||
if err != nil {
|
||||
slog.Error("llm: Model list request failed", "error", err)
|
||||
}
|
||||
|
||||
slog.Info("llm: Returned model list", "models", modelList)
|
||||
slog.Info("llm: Checking for requested models", "requested", modelIds)
|
||||
|
||||
requestedModelsCount := len(modelIds)
|
||||
searchResult := make(map[string]bool, requestedModelsCount)
|
||||
|
||||
for _, modelId := range modelIds {
|
||||
searchResult[modelId] = false
|
||||
}
|
||||
|
||||
for _, model := range modelList.Models {
|
||||
if slices.Contains(modelIds, model.ID) {
|
||||
searchResult[model.ID] = true
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range searchResult {
|
||||
if !v {
|
||||
return false, searchResult
|
||||
}
|
||||
}
|
||||
|
||||
return true, searchResult
|
||||
}
|
||||
|
|
|
@ -1,123 +0,0 @@
|
|||
package llm
|
||||
|
||||
import (
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RequestContext struct {
|
||||
Empty 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
|
||||
History []ChatMessage
|
||||
}
|
||||
|
||||
type ChatMessage struct {
|
||||
Name string
|
||||
Username string
|
||||
Text string
|
||||
IsMe bool
|
||||
IsUserRequest bool
|
||||
ReplyTo *ChatMessage
|
||||
}
|
||||
|
||||
func (c RequestContext) Prompt() string {
|
||||
if c.Empty {
|
||||
return ""
|
||||
}
|
||||
|
||||
prompt := ""
|
||||
|
||||
prompt += "The type of chat you're in is \"" + c.Chat.Type + "\". "
|
||||
|
||||
if c.Chat.Type == "group" || c.Chat.Type == "supergroup" {
|
||||
prompt += "Please consider that there are several users in this chat type who may discuss several unrelated " +
|
||||
"topics. Try to respond only about the topic you were asked about and only to the user who asked you, " +
|
||||
"but keep in mind another chat history. "
|
||||
}
|
||||
|
||||
if c.Chat.Title != "" {
|
||||
prompt += "\nChat is called \"" + c.Chat.Title + "\". "
|
||||
}
|
||||
if c.Chat.Description != "" {
|
||||
prompt += "Chat description is \"" + c.Chat.Description + "\". "
|
||||
}
|
||||
|
||||
prompt += "\nProfile of the user who mentioned you in the chat:\n" +
|
||||
"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
|
||||
}
|
||||
|
||||
func chatMessageToOpenAiChatCompletionMessage(message ChatMessage) openai.ChatCompletionMessage {
|
||||
var msgRole string
|
||||
var msgText string
|
||||
|
||||
switch {
|
||||
case message.IsMe:
|
||||
msgRole = openai.ChatMessageRoleAssistant
|
||||
case message.IsUserRequest:
|
||||
msgRole = openai.ChatMessageRoleUser
|
||||
default:
|
||||
msgRole = openai.ChatMessageRoleSystem
|
||||
}
|
||||
|
||||
if message.IsMe {
|
||||
msgText = message.Text
|
||||
} else {
|
||||
msgText = chatMessageToText(message)
|
||||
}
|
||||
|
||||
return openai.ChatCompletionMessage{
|
||||
Role: msgRole,
|
||||
Content: msgText,
|
||||
}
|
||||
}
|
||||
|
||||
func chatMessageToText(message ChatMessage) string {
|
||||
var msgText string
|
||||
|
||||
if message.ReplyTo != nil {
|
||||
msgText += "In reply to:"
|
||||
msgText += quoteText(presentUserMessageAsText(*message.ReplyTo)) + "\n\n"
|
||||
}
|
||||
msgText += presentUserMessageAsText(message)
|
||||
|
||||
return msgText
|
||||
}
|
||||
|
||||
func presentUserMessageAsText(message ChatMessage) string {
|
||||
result := message.Name
|
||||
if message.Username != "" {
|
||||
result += " (@" + message.Username + ")"
|
||||
}
|
||||
result += " wrote:\n" + message.Text
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func quoteText(text string) string {
|
||||
return "> " + strings.ReplaceAll(text, "\n", "\n> ")
|
||||
}
|
30
main.go
30
main.go
|
@ -12,43 +12,25 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
apiToken := os.Getenv("OPENAI_API_TOKEN")
|
||||
apiBaseUrl := os.Getenv("OPENAI_API_BASE_URL")
|
||||
|
||||
models := bot.ModelSelection{
|
||||
TextRequestModel: os.Getenv("MODEL_TEXT_REQUEST"),
|
||||
SummarizeModel: os.Getenv("MODEL_SUMMARIZE_REQUEST"),
|
||||
}
|
||||
|
||||
slog.Info("Selected", "models", models)
|
||||
ollamaToken := os.Getenv("OLLAMA_TOKEN")
|
||||
ollamaBaseUrl := os.Getenv("OLLAMA_BASE_URL")
|
||||
|
||||
telegramToken := os.Getenv("TELEGRAM_TOKEN")
|
||||
|
||||
llmc := llm.NewConnector(apiBaseUrl, apiToken)
|
||||
|
||||
slog.Info("Checking models availability")
|
||||
|
||||
hasAll, searchResult := llmc.HasAllModels([]string{models.TextRequestModel, models.SummarizeModel})
|
||||
if !hasAll {
|
||||
slog.Error("Not all models are available", "result", searchResult)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
slog.Info("All needed models are available")
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
botService := bot.NewBot(telegramApi, llmc, ext, models)
|
||||
botService := bot.NewBot(telegramApi, llmc, ext)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,9 +13,8 @@ type Stats struct {
|
|||
|
||||
GroupRequests uint64
|
||||
PrivateRequests uint64
|
||||
InlineQueries uint64
|
||||
|
||||
Mentions uint64
|
||||
HeyRequests uint64
|
||||
SummarizeRequests uint64
|
||||
}
|
||||
|
||||
|
@ -25,9 +24,8 @@ func NewStats() *Stats {
|
|||
|
||||
GroupRequests: 0,
|
||||
PrivateRequests: 0,
|
||||
InlineQueries: 0,
|
||||
|
||||
Mentions: 0,
|
||||
HeyRequests: 0,
|
||||
SummarizeRequests: 0,
|
||||
}
|
||||
}
|
||||
|
@ -38,18 +36,16 @@ func (s *Stats) MarshalJSON() ([]byte, error) {
|
|||
|
||||
GroupRequests uint64 `json:"group_requests"`
|
||||
PrivateRequests uint64 `json:"private_requests"`
|
||||
InlineQueries uint64 `json:"inline_queries"`
|
||||
|
||||
Mentions uint64 `json:"mentions"`
|
||||
HeyRequests uint64 `json:"hey_requests"`
|
||||
SummarizeRequests uint64 `json:"summarize_requests"`
|
||||
}{
|
||||
Uptime: time.Now().Sub(s.RunningSince).String(),
|
||||
|
||||
GroupRequests: s.GroupRequests,
|
||||
PrivateRequests: s.PrivateRequests,
|
||||
InlineQueries: s.InlineQueries,
|
||||
|
||||
Mentions: s.Mentions,
|
||||
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()
|
||||
|
@ -81,10 +71,10 @@ func (s *Stats) PrivateRequest() {
|
|||
s.PrivateRequests++
|
||||
}
|
||||
|
||||
func (s *Stats) Mention() {
|
||||
func (s *Stats) HeyRequest() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.Mentions++
|
||||
s.HeyRequests++
|
||||
}
|
||||
|
||||
func (s *Stats) SummarizeRequest() {
|
||||
|
|
Loading…
Reference in a new issue