Pull message rendering into its own method

This commit is contained in:
Matt Low 2023-11-05 08:50:07 +00:00
parent 2c64ab501b
commit 6eca84dab8
2 changed files with 33 additions and 11 deletions

View File

@ -97,33 +97,36 @@ var newCmd = &cobra.Command{
OriginalContent: messageContents, OriginalContent: messageContents,
}, },
} }
for _, message := range(messages) { for _, message := range messages {
err = store.SaveMessage(&message) err = store.SaveMessage(&message)
if err != nil { if err != nil {
Warn("Could not save %s message: %v\n", message.Role, err) Warn("Could not save %s message: %v\n", message.Role, err)
} }
} }
fmt.Printf("<System>\n\n%s\n\n", system) for _, message := range messages {
fmt.Printf("<You>\n\n%s\n\n", messageContents) message.RenderTTY(true)
fmt.Print("<Assistant>\n\n") }
reply := Message{
ConversationID: conversation.ID,
Role: "assistant",
}
reply.RenderTTY(false)
receiver := make(chan string) receiver := make(chan string)
response := make(chan string) response := make(chan string)
go func() { go func() {
response <- HandleDelayedResponse(receiver) response <- HandleDelayedResponse(receiver)
}() }()
err = CreateChatCompletionStream(messages, receiver) err = CreateChatCompletionStream(messages, receiver)
if err != nil { if err != nil {
Fatal("%v\n", err) Fatal("%v\n", err)
} }
reply := Message{ reply.OriginalContent = <-response
ConversationID: conversation.ID,
Role: "assistant",
OriginalContent: <-response,
}
err = store.SaveMessage(&reply) err = store.SaveMessage(&reply)
if err != nil { if err != nil {
Fatal("Could not save reply: %v\n", err) Fatal("Could not save reply: %v\n", err)

View File

@ -55,3 +55,22 @@ func HandleDelayedResponse(response chan string) string {
return sb.String() return sb.String()
} }
func (m *Message) RenderTTY(paddingDown bool) {
var friendlyRole string
switch m.Role {
case "user":
friendlyRole = "You"
case "system":
friendlyRole = "System"
case "assistant":
friendlyRole = "Assistant"
}
fmt.Printf("<%s>\n\n", friendlyRole)
if m.OriginalContent != "" {
fmt.Print(m.OriginalContent)
}
if paddingDown {
fmt.Print("\n\n")
}
}