2023-11-12 16:32:12 -07:00
|
|
|
package cli
|
|
|
|
|
2023-11-12 23:39:06 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
case "user":
|
|
|
|
friendlyRole = "You"
|
|
|
|
case "system":
|
|
|
|
friendlyRole = "System"
|
|
|
|
case "assistant":
|
|
|
|
friendlyRole = "Assistant"
|
|
|
|
default:
|
|
|
|
friendlyRole = m.Role
|
|
|
|
}
|
|
|
|
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-12 23:39:06 -07:00
|
|
|
prompt := fmt.Sprintf("%s\n\n---\n\n%s", header, c.FormatForExternalPrompting())
|
|
|
|
|
|
|
|
messages := []Message{
|
|
|
|
{
|
|
|
|
Role: "user",
|
|
|
|
OriginalContent: prompt,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
model := "gpt-3.5-turbo" // use cheap model to generate title
|
2023-11-19 20:48:32 -07:00
|
|
|
response, err := CreateChatCompletion(model, messages, 25)
|
2023-11-12 23:39:06 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Title = response
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conversation) FormatForExternalPrompting() 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 {
|
|
|
|
sb.WriteString(fmt.Sprintf("<%s>\n", message.FriendlyRole()))
|
|
|
|
sb.WriteString(fmt.Sprintf("\"\"\"\n%s\n\"\"\"\n\n", message.OriginalContent))
|
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|