add basic chat loop
This commit is contained in:
29
cmd/chat/chat.go
Normal file
29
cmd/chat/chat.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
5
main.go
5
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)
|
||||
|
Reference in New Issue
Block a user