2023-11-12 16:32:12 -07:00
|
|
|
package cli
|
|
|
|
|
2023-11-12 23:39:06 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-11-28 21:43:53 -07:00
|
|
|
type MessageRole string
|
|
|
|
|
|
|
|
const (
|
|
|
|
MessageRoleUser MessageRole = "user"
|
|
|
|
MessageRoleAssistant MessageRole = "assistant"
|
|
|
|
MessageRoleSystem MessageRole = "system"
|
|
|
|
)
|
|
|
|
|
2023-11-12 16:32:12 -07:00
|
|
|
// FriendlyRole returns a human friendly signifier for the message's role.
|
|
|
|
func (m *Message) FriendlyRole() string {
|
|
|
|
var friendlyRole string
|
|
|
|
switch m.Role {
|
2023-11-28 21:43:53 -07:00
|
|
|
case MessageRoleUser:
|
2023-11-12 16:32:12 -07:00
|
|
|
friendlyRole = "You"
|
2023-11-28 21:43:53 -07:00
|
|
|
case MessageRoleSystem:
|
2023-11-12 16:32:12 -07:00
|
|
|
friendlyRole = "System"
|
2023-11-28 21:43:53 -07:00
|
|
|
case MessageRoleAssistant:
|
2023-11-12 16:32:12 -07:00
|
|
|
friendlyRole = "Assistant"
|
|
|
|
default:
|
2023-11-28 21:43:53 -07:00
|
|
|
friendlyRole = string(m.Role)
|
2023-11-12 16:32:12 -07:00
|
|
|
}
|
|
|
|
return friendlyRole
|
|
|
|
}
|
2023-11-12 23:39:06 -07:00
|
|
|
|
2023-11-29 08:30:11 -07:00
|
|
|
func (c *Conversation) GenerateTitle() (string, error) {
|
2024-01-01 21:31:21 -07:00
|
|
|
messages, err := store.Messages(c)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-11-12 23:39:06 -07:00
|
|
|
|
2024-01-01 21:31:21 -07:00
|
|
|
const header = "Generate a concise 4-5 word title for the conversation below."
|
|
|
|
prompt := fmt.Sprintf("%s\n\n---\n\n%s", header, formatForExternalPrompting(messages, false))
|
|
|
|
|
|
|
|
generateRequest := []Message{
|
2023-11-12 23:39:06 -07:00
|
|
|
{
|
2023-11-28 21:43:53 -07:00
|
|
|
Role: MessageRoleUser,
|
2023-11-12 23:39:06 -07:00
|
|
|
OriginalContent: prompt,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
model := "gpt-3.5-turbo" // use cheap model to generate title
|
2024-01-01 21:31:21 -07:00
|
|
|
response, err := CreateChatCompletion(model, generateRequest, 25, nil)
|
2023-11-12 23:39:06 -07:00
|
|
|
if err != nil {
|
2023-11-29 08:30:11 -07:00
|
|
|
return "", err
|
2023-11-12 23:39:06 -07:00
|
|
|
}
|
|
|
|
|
2023-11-29 08:30:11 -07:00
|
|
|
return response, nil
|
2023-11-12 23:39:06 -07:00
|
|
|
}
|
|
|
|
|
2024-01-01 21:31:21 -07:00
|
|
|
func formatForExternalPrompting(messages []Message, system bool) string {
|
2023-11-12 23:39:06 -07:00
|
|
|
sb := strings.Builder{}
|
|
|
|
for _, message := range messages {
|
2023-11-28 21:51:38 -07:00
|
|
|
if message.Role == MessageRoleSystem && !system {
|
|
|
|
continue
|
|
|
|
}
|
2023-11-12 23:39:06 -07:00
|
|
|
sb.WriteString(fmt.Sprintf("<%s>\n", message.FriendlyRole()))
|
|
|
|
sb.WriteString(fmt.Sprintf("\"\"\"\n%s\n\"\"\"\n\n", message.OriginalContent))
|
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|