Request contexts and some logging changes. #10

Merged
skobkin merged 3 commits from fix_logging into main 2024-03-12 03:15:55 +00:00
3 changed files with 82 additions and 4 deletions
Showing only changes of commit dc5ad2c580 - Show all commits

View File

@ -87,11 +87,19 @@ func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
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)
llmReply, err := b.llm.HandleSingleRequest(update.Message.Text, llm.ModelMistralUncensored)
requestContext := b.createLlmRequestContext(update)
llmReply, err := b.llm.HandleSingleRequest(userMessage, llm.ModelMistralUncensored, requestContext)
if err != nil {
slog.Error("Cannot get reply from LLM connector")
@ -247,6 +255,35 @@ func (b *Bot) statsHandler(bot *telego.Bot, update telego.Update) {
}
}
func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext {
message := update.Message
rc := llm.RequestContext{}
if message == nil {
return rc
}
user := message.From
if user != nil {
rc.User = llm.UserContext{
Username: user.Username,
FirstName: user.FirstName,
LastName: user.LastName,
IsPremium: user.IsPremium,
}
}
chat := message.Chat
rc.Chat = llm.ChatContext{
Title: chat.Title,
Description: chat.Description,
Type: chat.Type,
}
return rc
}
func (b *Bot) reply(originalMessage *telego.Message, newMessage *telego.SendMessageParams) *telego.SendMessageParams {
return newMessage.WithReplyParameters(&telego.ReplyParameters{
MessageID: originalMessage.MessageID,

View File

@ -29,13 +29,16 @@ func NewConnector(baseUrl string, token string) *LlmConnector {
}
}
func (l *LlmConnector) HandleSingleRequest(text string, model string) (string, error) {
func (l *LlmConnector) HandleSingleRequest(text string, model string, requestContext RequestContext) (string, error) {
req := openai.ChatCompletionRequest{
Model: model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You're a bot in the Telegram chat. You are replying to questions directed to you.",
Role: openai.ChatMessageRoleSystem,
Content: "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. " +
requestContext.Prompt(),
},
},
}

38
llm/request_context.go Normal file
View File

@ -0,0 +1,38 @@
package llm
type RequestContext struct {
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 {
prompt := "The chat you're in is called \"" + c.Chat.Title + "\". " +
"The type of chat is \"" + c.Chat.Type + "\". " +
"The chat description is \"" + c.Chat.Description + "\". "
if c.User.Username != "" {
prompt += "The user who wrote you has username \"@" + c.Chat.Description + "\". "
}
prompt += "Their first name is \"" + c.User.FirstName + "\". "
if c.User.LastName != "" {
prompt += "Their last name is \"" + c.User.LastName + "\". "
}
if c.User.IsPremium {
prompt += "They have Telegram Premium subscription. "
}
return prompt
}