Skip to content

Commit

Permalink
feat: stop abort on API error
Browse files Browse the repository at this point in the history
  • Loading branch information
noodnik2 committed Feb 16, 2024
1 parent e116c5a commit 6391ede
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions internal/controller/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ func DoChat(ctx context.Context, cfg config.Config, console chatter.Console) err

defer func() { ctrl.scribe.Footer(model.ScribeFooter{Time: time.Now()}) }()

console.Println("gochat started")
console.Printf("Hello %s!\n", userName)
console.Printf("Using model: %s\n", ctrl.chatter.Model())
ctrl.signon(console, userName)

defaultPromptName := cfg.Chatter.DefaultPrompt
if defaultPromptName != "" {
prompt := cfg.Chatter.Prompts[defaultPromptName]
if prompt == "" {
panic(fmt.Errorf("%w: default prompt(%s) not found", model.ErrConfig, defaultPromptName))
return fmt.Errorf("%w: default prompt(%s) not found", model.ErrConfig, defaultPromptName)
}

promptUserName := fmt.Sprintf("%s prompt", defaultPromptName)
Expand All @@ -71,15 +69,16 @@ func DoChat(ctx context.Context, cfg config.Config, console chatter.Console) err
console.Println()
}

ctrl.doQuery(ctx, promptUserName, prompt)
if qErr := ctrl.doQuery(ctx, promptUserName, prompt); qErr != nil {
return fmt.Errorf("for initial prompt: %w", qErr)
}
}

console.Println("Type 'exit' to quit")
console.Println("Ask me anything: ")
signoff(console)

ctrl.doDialog(ctx, userName)

return err
return nil
}

func (cc *chatController) doDialog(ctx context.Context, userName string) {
Expand All @@ -95,13 +94,15 @@ func (cc *chatController) doDialog(ctx context.Context, userName string) {
break
}

cc.doQuery(ctx, userName, prompt)
if qErr := cc.doQuery(ctx, userName, prompt); qErr != nil {
cc.console.Printf("%s ! error: %s\n", cc.chatter.Model(), qErr)
}
}

cc.console.Println("Goodbye!")
}

func (cc *chatController) doQuery(ctx context.Context, userName, prompt string) {
func (cc *chatController) doQuery(ctx context.Context, userName, prompt string) error {
cc.scribe.Entry(model.ScribeEntry{
Time: time.Now(),
Who: userName,
Expand All @@ -110,14 +111,27 @@ func (cc *chatController) doQuery(ctx context.Context, userName, prompt string)

response, tqErr := cc.chatter.MakeSynchronousTextQuery(ctx, cc.console, prompt)
if tqErr != nil {
panic(tqErr)
return tqErr
}

cc.scribe.Entry(model.ScribeEntry{
Time: time.Now(),
Who: cc.chatterName,
What: response,
})

return nil
}

func (cc *chatController) signon(console chatter.Console, userName string) {
console.Println("gochat started")
console.Printf("Hello %s!\n", userName)
console.Printf("Using model: %s\n", cc.chatter.Model())
}

func signoff(console chatter.Console) {
console.Println("Type 'exit' to quit")
console.Println("Ask me anything: ")
}

func getUsername() string {
Expand Down

0 comments on commit 6391ede

Please sign in to comment.