Generate titles for new conversations

This commit is contained in:
Matt Low 2023-11-13 06:39:06 +00:00
parent e66016aedd
commit cf0e98f656
2 changed files with 48 additions and 0 deletions

View File

@ -239,6 +239,15 @@ var newCmd = &cobra.Command{
} }
fmt.Println() fmt.Println()
err = conversation.GenerateTitle()
if err != nil {
Warn("Could not generate title for conversation: %v\n", err)
}
err = store.SaveConversation(&conversation)
if err != nil {
Warn("Could not save conversation after generating title: %v\n", err)
}
}, },
} }

View File

@ -1,5 +1,10 @@
package cli package cli
import (
"fmt"
"strings"
)
// FriendlyRole returns a human friendly signifier for the message's role. // FriendlyRole returns a human friendly signifier for the message's role.
func (m *Message) FriendlyRole() string { func (m *Message) FriendlyRole() string {
var friendlyRole string var friendlyRole string
@ -15,3 +20,37 @@ func (m *Message) FriendlyRole() string {
} }
return friendlyRole return friendlyRole
} }
func (c *Conversation) GenerateTitle() error {
const header = "Generate a short title for the conversation below."
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
response, err := CreateChatCompletion(model, messages, 10)
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()
}