lmcli/pkg/cli/conversation.go

57 lines
1.3 KiB
Go

package cli
import (
"fmt"
"strings"
)
// 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
}
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())
messages := []Message{
{
Role: "user",
OriginalContent: prompt,
},
}
model := "gpt-3.5-turbo" // use cheap model to generate title
response, err := CreateChatCompletion(model, messages, 25)
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()
}