lmcli/pkg/cli/conversation.go

68 lines
1.5 KiB
Go
Raw Normal View History

package cli
2023-11-12 23:39:06 -07:00
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
}
2023-11-12 23:39:06 -07:00
func (c *Conversation) GenerateTitle() (string, error) {
messages, err := store.Messages(c)
if err != nil {
return "", err
}
2023-11-12 23:39:06 -07:00
const header = "Generate a concise 4-5 word title for the conversation below."
prompt := fmt.Sprintf("%s\n\n---\n\n%s", header, formatForExternalPrompting(messages, false))
generateRequest := []Message{
2023-11-12 23:39:06 -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
response, err := CreateChatCompletion(model, generateRequest, 25, nil)
2023-11-12 23:39:06 -07:00
if err != nil {
return "", err
2023-11-12 23:39:06 -07:00
}
return response, nil
2023-11-12 23:39:06 -07:00
}
func formatForExternalPrompting(messages []Message, system bool) string {
2023-11-12 23:39:06 -07:00
sb := strings.Builder{}
for _, message := range messages {
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()
}