From 1ed6023a142b70cbd068b27e8de50a91612fc541 Mon Sep 17 00:00:00 2001 From: Mason Payne Date: Sun, 25 Feb 2024 20:17:48 -0700 Subject: [PATCH] add basic chat loop --- cmd/chat/chat.go | 29 +++++++++++++++++++++++++++++ main.go | 5 +++-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 cmd/chat/chat.go diff --git a/cmd/chat/chat.go b/cmd/chat/chat.go new file mode 100644 index 0000000..5b9e0e4 --- /dev/null +++ b/cmd/chat/chat.go @@ -0,0 +1,29 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +func main() { + reader := bufio.NewReader(os.Stdin) + fmt.Println("Go CLI Chat App") + fmt.Println("---------------------") + + for { + fmt.Print("You: ") + text, _ := reader.ReadString('\n') + text = strings.TrimSpace(text) // Remove leading and trailing whitespace + + // Process the input. For now, we'll just echo it back. + fmt.Printf("App: You said, %s\n", text) + + // Check if the user wants to exit. + if text == "exit" { + fmt.Println("Exiting chat...") + break + } + } +} diff --git a/main.go b/main.go index ef3ebf5..85359ea 100644 --- a/main.go +++ b/main.go @@ -106,8 +106,9 @@ func singlePromptInteraction(systemPrompt, prompt string, retries int) (openai.C if err != nil { // if 429, wait and try again if strings.Contains(err.Error(), "429") && retries > 0 { - fmt.Println("429 error, waiting 5 seconds...") - time.Sleep(5 * time.Second) + seconds := (1 / retries) * 60 // back off for each retry e.g. 12, 15, 20, 30, 60 + fmt.Printf("429 error, waiting %v seconds...\n", seconds) + time.Sleep(time.Duration(seconds) * time.Second) return singlePromptInteraction(systemPrompt, prompt, retries-1) // TODO: establish base case to prevent forever retrying } return openai.ChatCompletionResponse{}, fmt.Errorf("ChatCompletion request error: %w", err)