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

View File

@ -55,3 +55,22 @@ func HandleDelayedResponse(response chan string) 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")
}
}