2024-03-12 21:32:52 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mymmrac/telego"
|
|
|
|
"log/slog"
|
|
|
|
"telegram-ollama-reply-bot/llm"
|
|
|
|
)
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
func (b *Bot) createLlmRequestContextFromMessage(message *telego.Message) llm.RequestContext {
|
2024-03-12 21:32:52 +00:00
|
|
|
rc := llm.RequestContext{
|
2024-10-27 21:35:35 +00:00
|
|
|
Empty: true,
|
2024-03-12 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
if message == nil {
|
2024-03-12 21:32:52 +00:00
|
|
|
slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc)
|
|
|
|
|
|
|
|
return rc
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.Empty = false
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
user := message.From
|
2024-03-12 21:32:52 +00:00
|
|
|
|
|
|
|
if user != nil {
|
|
|
|
rc.User = llm.UserContext{
|
|
|
|
Username: user.Username,
|
|
|
|
FirstName: user.FirstName,
|
|
|
|
LastName: user.LastName,
|
|
|
|
IsPremium: user.IsPremium,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
// 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),
|
2024-03-12 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
slog.Debug("request context created", "request-context", rc)
|
|
|
|
|
|
|
|
return rc
|
|
|
|
}
|
2024-10-27 21:35:35 +00:00
|
|
|
|
2024-11-03 22:16:26 +00:00
|
|
|
func historyToLlmMessages(history []MessageData) []llm.ChatMessage {
|
2024-10-27 21:35:35 +00:00
|
|
|
length := len(history)
|
|
|
|
|
|
|
|
if length > 0 {
|
|
|
|
result := make([]llm.ChatMessage, 0, length)
|
|
|
|
|
|
|
|
for _, msg := range history {
|
2024-11-03 22:16:26 +00:00
|
|
|
result = append(result, messageDataToLlmMessage(msg))
|
2024-10-27 21:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return make([]llm.ChatMessage, 0)
|
|
|
|
}
|
2024-11-03 22:16:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|