add basic chat loop

This commit is contained in:
2024-02-25 20:17:48 -07:00
parent d102e52dfc
commit 1ed6023a14
2 changed files with 32 additions and 2 deletions

29
cmd/chat/chat.go Normal file
View File

@ -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
}
}
}

View File

@ -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)