Skip to content

Commit

Permalink
Improve logging of OpenAI errors (#117)
Browse files Browse the repository at this point in the history
* Log Agent.Generate errors so we have clear error messages in the event
the OpenAI request fails.

Fix #112
  • Loading branch information
jlewi authored May 24, 2024
1 parent 3ab5177 commit 9e149f3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (a *Agent) Generate(ctx context.Context, req *v1alpha1.GenerateRequest) (*v
blocks, err := a.completeWithRetries(ctx, req, examples)
if err != nil {
// TODO(jeremy): Should we set a status code?
log.Error(err, "Agent.Generate failed to generate completions")
return nil, err
}

Expand Down
46 changes: 46 additions & 0 deletions app/pkg/oai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,49 @@ func Test_Ollama(t *testing.T) {

t.Logf("Response: %+v", resp)
}

func Test_OpenAIErrors(t *testing.T) {
// We want to test how OpenAI errors are processed so that we ensure we surface actionable error messages.
// E.g. if the API key is invalid, we want to surface that error message to the user.
if os.Getenv("GITHUB_ACTIONS") != "" {
t.Skipf("Test_OpenAIErrors is a manual test that is skipped in CICD")
}

if err := config.InitViper(nil); err != nil {
t.Fatalf("Failed to initialize viper: %v", err)
}

cfg := config.GetConfig()
cfg.AzureOpenAI = nil

// Use a key file that should be deprecated
cfg.OpenAI.APIKeyFile = "/Users/jlewi/secrets/openapi.api.key.20230207"

client, err := NewClient(*cfg)
if err != nil {
t.Fatalf("Failed to create OpenAI client: %v", err)
}

messages := []openai.ChatCompletionMessage{
{Role: openai.ChatMessageRoleSystem,
Content: "You are a helpful assistant.",
},
{Role: openai.ChatMessageRoleUser,
Content: "hello",
},
}

request := openai.ChatCompletionRequest{
Model: "llama2",
Messages: messages,
MaxTokens: 2000,
Temperature: 0.9,
}

resp, err := client.CreateChatCompletion(context.Background(), request)
if err != nil {
t.Fatalf("Failed to create chat completion: %v", err)
}

t.Logf("Response: %+v", resp)
}

0 comments on commit 9e149f3

Please sign in to comment.