68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type MessageRole string
|
|
|
|
const (
|
|
MessageRoleUser MessageRole = "user"
|
|
MessageRoleAssistant MessageRole = "assistant"
|
|
MessageRoleSystem MessageRole = "system"
|
|
)
|
|
|
|
// FriendlyRole returns a human friendly signifier for the message's role.
|
|
func (m *Message) FriendlyRole() string {
|
|
var friendlyRole string
|
|
switch m.Role {
|
|
case MessageRoleUser:
|
|
friendlyRole = "You"
|
|
case MessageRoleSystem:
|
|
friendlyRole = "System"
|
|
case MessageRoleAssistant:
|
|
friendlyRole = "Assistant"
|
|
default:
|
|
friendlyRole = string(m.Role)
|
|
}
|
|
return friendlyRole
|
|
}
|
|
|
|
func (c *Conversation) GenerateTitle() error {
|
|
const header = "Generate a consise 4-5 word title for the conversation below."
|
|
prompt := fmt.Sprintf("%s\n\n---\n\n%s", header, c.FormatForExternalPrompting(false))
|
|
|
|
messages := []Message{
|
|
{
|
|
Role: MessageRoleUser,
|
|
OriginalContent: prompt,
|
|
},
|
|
}
|
|
|
|
model := "gpt-3.5-turbo" // use cheap model to generate title
|
|
response, err := CreateChatCompletion(model, messages, 25, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c.Title = response
|
|
return nil
|
|
}
|
|
|
|
func (c *Conversation) FormatForExternalPrompting(system bool) string {
|
|
sb := strings.Builder{}
|
|
messages, err := store.Messages(c)
|
|
if err != nil {
|
|
Fatal("Could not retrieve messages for conversation %v", c)
|
|
}
|
|
for _, message := range messages {
|
|
if message.Role == MessageRoleSystem && !system {
|
|
continue
|
|
}
|
|
sb.WriteString(fmt.Sprintf("<%s>\n", message.FriendlyRole()))
|
|
sb.WriteString(fmt.Sprintf("\"\"\"\n%s\n\"\"\"\n\n", message.OriginalContent))
|
|
}
|
|
return sb.String()
|
|
}
|