2024-03-12 03:12:25 +00:00
|
|
|
package llm
|
|
|
|
|
2024-11-03 22:16:26 +00:00
|
|
|
import (
|
|
|
|
"github.com/sashabaranov/go-openai"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-03-12 03:12:25 +00:00
|
|
|
type RequestContext struct {
|
2024-10-27 21:35:35 +00:00
|
|
|
Empty bool
|
|
|
|
User UserContext
|
|
|
|
Chat ChatContext
|
2024-03-12 03:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserContext struct {
|
|
|
|
Username string
|
|
|
|
FirstName string
|
|
|
|
LastName string
|
|
|
|
IsPremium bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChatContext struct {
|
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Type string
|
2024-10-27 21:35:35 +00:00
|
|
|
History []ChatMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChatMessage struct {
|
2024-11-03 22:16:26 +00:00
|
|
|
Name string
|
|
|
|
Username string
|
|
|
|
Text string
|
|
|
|
IsMe bool
|
|
|
|
IsUserRequest bool
|
|
|
|
ReplyTo *ChatMessage
|
2024-03-12 03:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c RequestContext) Prompt() string {
|
2024-03-12 22:18:01 +00:00
|
|
|
if c.Empty {
|
|
|
|
return ""
|
2024-03-12 03:31:07 +00:00
|
|
|
}
|
2024-03-12 22:18:01 +00:00
|
|
|
|
|
|
|
prompt := ""
|
2024-10-27 21:35:35 +00:00
|
|
|
|
|
|
|
prompt += "The type of chat you're in is \"" + c.Chat.Type + "\". "
|
|
|
|
|
2024-10-27 23:04:35 +00:00
|
|
|
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. "
|
|
|
|
}
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
if c.Chat.Title != "" {
|
2024-10-27 23:04:35 +00:00
|
|
|
prompt += "\nChat is called \"" + c.Chat.Title + "\". "
|
2024-10-27 21:35:35 +00:00
|
|
|
}
|
|
|
|
if c.Chat.Description != "" {
|
|
|
|
prompt += "Chat description is \"" + c.Chat.Description + "\". "
|
2024-03-12 03:31:07 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 23:10:01 +00:00
|
|
|
prompt += "\nProfile of the user who mentioned you in the chat:\n" +
|
2024-08-16 00:47:07 +00:00
|
|
|
"First name: \"" + c.User.FirstName + "\"\n"
|
2024-03-12 03:12:25 +00:00
|
|
|
if c.User.Username != "" {
|
2024-08-16 00:47:07 +00:00
|
|
|
prompt += "Username: @" + c.User.Username + ".\n"
|
2024-03-12 03:12:25 +00:00
|
|
|
}
|
|
|
|
if c.User.LastName != "" {
|
2024-08-16 00:47:07 +00:00
|
|
|
prompt += "Last name: \"" + c.User.LastName + "\"\n"
|
2024-03-12 03:12:25 +00:00
|
|
|
}
|
2024-10-27 23:04:35 +00:00
|
|
|
//if c.User.IsPremium {
|
|
|
|
// prompt += "Telegram Premium subscription: active."
|
|
|
|
//}
|
2024-03-12 03:12:25 +00:00
|
|
|
|
|
|
|
return prompt
|
|
|
|
}
|
2024-11-03 22:16:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-11-03 22:41:08 +00:00
|
|
|
if message.ReplyTo != nil {
|
2024-11-03 22:16:26 +00:00
|
|
|
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> ")
|
|
|
|
}
|