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
|
|
|
|
|
|
|
func (c *Conversation) GenerateTitle() error {
|
2023-11-26 21:56:39 -07:00
|
|
|
const header = "Generate a consise 4-5 word title for the conversation below."
|
2023-11-28 21:51:38 -07:00
|
|
|
prompt := fmt.Sprintf("%s\n\n---\n\n%s", header, c.FormatForExternalPrompting(false))
|
2023-11-12 23:39:06 -07:00
|
|
|
|
|
|
|
messages := []Message{
|
|
|
|
{
|
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
|
2023-11-28 21:43:53 -07:00
|
|
|
response, err := CreateChatCompletion(model, messages, 25, nil)
|
2023-11-12 23:39:06 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Title = response
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-28 21:51:38 -07:00
|
|
|
func (c *Conversation) FormatForExternalPrompting(system bool) string {
|
2023-11-12 23:39:06 -07:00
|
|
|
sb := strings.Builder{}
|
|
|
|
messages, err := store.Messages(c)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("Could not retrieve messages for conversation %v", c)
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|