Skip to content

Commit

Permalink
feat: ensure chat completion ends with a new line
Browse files Browse the repository at this point in the history
  • Loading branch information
yiblet committed Apr 2, 2023
1 parent b7c9f19 commit 2ee9439
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
local/
hlp
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (args *askCmd) Execute(ctx context.Context) error {
return err
}

lastMessage := ""
err = client.ChatCompletionStream(ctx, gpt3.ChatCompletionRequest{
Messages: []gpt3.ChatCompletionRequestMessage{
{Role: "system", Content: systemMessage},
Expand All @@ -60,10 +61,19 @@ func (args *askCmd) Execute(ctx context.Context) error {
Stream: true,
Model: model,
}, func(cr *gpt3.ChatCompletionStreamResponse) {
message := cr.Choices[0].Delta.Content
if message != "" {
lastMessage = message
}
fmt.Printf("%s", cr.Choices[0].Delta.Content)
})

return err
if err != nil {
return err
}
if len(lastMessage) == 0 || lastMessage[len(lastMessage)-1] != '\n' {
fmt.Printf("\n")
}
return nil
}

type authCmd struct {
Expand Down

0 comments on commit 2ee9439

Please sign in to comment.