Skip to content

Commit

Permalink
add simple spinner to chat completion requests
Browse files Browse the repository at this point in the history
  • Loading branch information
austin-denoble committed Jun 16, 2024
1 parent 895319d commit dd6296f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/pkg/assistants/assistant_chat_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"net/http"
"os"
"strings"
"time"

"github.com/briandowns/spinner"
"github.com/pinecone-io/cli/internal/pkg/utils/configuration/state"
"github.com/pinecone-io/cli/internal/pkg/utils/log"
"github.com/pinecone-io/cli/internal/pkg/utils/models"
Expand Down Expand Up @@ -44,13 +46,17 @@ func GetAssistantChatCompletions(asstName string, msg string, stream bool) (*mod
return nil, err
}

s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Start()

var resp *models.ChatCompletionModel
if !stream {
resp, err = network.PostAndDecode[models.ChatCompletionRequest, models.ChatCompletionModel](
assistantDataUrl,
fmt.Sprintf(URL_ASSISTANT_CHAT_COMPLETIONS, asstName),
body,
)
s.Stop()
if err != nil {
return nil, err
}
Expand All @@ -59,6 +65,7 @@ func GetAssistantChatCompletions(asstName string, msg string, stream bool) (*mod
assistantDataUrl,
fmt.Sprintf(URL_ASSISTANT_CHAT_COMPLETIONS, asstName),
body,
s,
)
if err != nil {
return nil, err
Expand All @@ -73,12 +80,13 @@ func GetAssistantChatCompletions(asstName string, msg string, stream bool) (*mod
return resp, nil
}

func PostAndStreamChatResponse[B any](baseUrl string, path string, body B) (*models.ChatCompletionModel, error) {
func PostAndStreamChatResponse[B any](baseUrl string, path string, body B, spinner *spinner.Spinner) (*models.ChatCompletionModel, error) {
resp, err := network.RequestWithBody[B](baseUrl, path, http.MethodPost, body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
spinner.Stop()

var completeResponse string
var id string
Expand Down

0 comments on commit dd6296f

Please sign in to comment.