Formatting/comments

This commit is contained in:
Matt Low 2023-11-05 18:19:30 +00:00
parent 9c9b8fa412
commit 0c2149663f
3 changed files with 7 additions and 5 deletions

View File

@ -112,7 +112,6 @@ var newCmd = &cobra.Command{
ConversationID: conversation.ID, ConversationID: conversation.ID,
Role: "assistant", Role: "assistant",
} }
reply.RenderTTY(false) reply.RenderTTY(false)
receiver := make(chan string) receiver := make(chan string)
@ -120,6 +119,7 @@ var newCmd = &cobra.Command{
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)

View File

@ -25,8 +25,8 @@ func CreateChatCompletionRequest(messages []Message) *openai.ChatCompletionReque
} }
} }
// CreateChatCompletion accepts a slice of Message and returns the response // CreateChatCompletion submits a Chat Completion API request and returns the
// of the Large Language Model. // response.
func CreateChatCompletion(messages []Message) (string, error) { func CreateChatCompletion(messages []Message) (string, error) {
client := openai.NewClient(config.OpenAI.APIKey) client := openai.NewClient(config.OpenAI.APIKey)
resp, err := client.CreateChatCompletion( resp, err := client.CreateChatCompletion(
@ -41,8 +41,8 @@ func CreateChatCompletion(messages []Message) (string, error) {
return resp.Choices[0].Message.Content, nil return resp.Choices[0].Message.Content, nil
} }
// CreateChatCompletionStream submits an streaming Chat Completion API request // CreateChatCompletionStream submits a streaming Chat Completion API request
// and sends the received data to the output channel. // and streams the received response to the provided output channel.
func CreateChatCompletionStream(messages []Message, output chan string) error { func CreateChatCompletionStream(messages []Message, output chan string) error {
client := openai.NewClient(config.OpenAI.APIKey) client := openai.NewClient(config.OpenAI.APIKey)
ctx := context.Background() ctx := context.Background()

View File

@ -45,7 +45,9 @@ func HandleDelayedResponse(response chan string) string {
firstChunk := true firstChunk := true
for chunk := range response { for chunk := range response {
if firstChunk { if firstChunk {
// notify wait animation that we've received data
waitSignal <- "" waitSignal <- ""
// wait for signal that wait animation has completed
<-waitSignal <-waitSignal
firstChunk = false firstChunk = false
} }