Skip to content

Commit

Permalink
Gracefully handle missing session
Browse files Browse the repository at this point in the history
  • Loading branch information
edofic committed Apr 14, 2023
1 parent c361fcc commit 04a2d4f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,26 @@ func getClient() *openai.Client {

func getCompletionRequest(p params, model string) openai.ChatCompletionRequest {
if p.continueSession {
return loadLastCompletion()
} else {
return newCompletionRequest(p, model)
req := loadLastCompletion()
if req != nil {
return *req
}
fmt.Println("WARN: failed to load previous session, starting a new one")
}
return newCompletionRequest(p, model)
}

func loadLastCompletion() openai.ChatCompletionRequest {
func loadLastCompletion() *openai.ChatCompletionRequest {
var req openai.ChatCompletionRequest
session, err := os.ReadFile(sessionFile)
if err != nil {
panic(err)
return nil
}
err = json.Unmarshal(session, &req)
if err != nil {
panic(err)
return nil
}
return req
return &req
}

func saveCompletion(req openai.ChatCompletionRequest) error {
Expand Down

0 comments on commit 04a2d4f

Please sign in to comment.