#API error 429 - Android Studio

16 messages · Page 1 of 1 (latest)

midnight roost
#

I've been trying to make chatGPT, perhaps also Dall E into application where you would login with API key, but I have been stopped by this error.

E/API Error:  429
I/okhttp.OkHttpClient: Callback failure for call to https://api.openai.com/...
I/okhttp.OkHttpClient: java.io.IOException: Unexpected code Response{protocol=h2, code=429, message=, url=https://api.openai.com/v1/engines/davinci/completions/}
I/okhttp.OkHttpClient:     at com.example.pocketgptd.LoginActivity$1.onResponse(LoginActivity.java:74)

Error only shows up when application is built, ran and you enter API key and press the designated button. which triggers function naOdziv(onResponse)

                public void naOdziv(@NonNull Call call, @NonNull Response response) throws IOException {
                    try (ResponseBody ignored = response.body()) {
                        if (response.code() == 200 || response.code() == 201) {
                            if (znanAPIKey.isChecked()) {
                                SharedPreferences.Editor editor = preferences.edit();
                                editor.putString(API_KEY, daniAPIKey);
                                editor.apply();
                            }
                            Intent intent = new Intent(LoginStran.this, GlavnaStran.class);
                            intent.putExtra("api_key",apiKey);
                            startActivity(intent);
                        }
                        else {
                            Log.e("API Error",response.message() + " "+ response.code());
                            throw new IOException("Unexpected code " + response);  //Line 74
                        }
                    }
                }
tight ermine
#

okhttp.OkHttpClient: java.io.IOException: Unexpected code Response{protocol=h2, code=429, message=, It looks like you got HTTP 429, which I think is a RateLimitError or a Quota Exceeded Error, can you also print the full response returned by the API so you can see what the error message is?

midnight roost
#

Here you go

I/okhttp.OkHttpClient: Callback failure for call to https://api.openai.com/...
I/okhttp.OkHttpClient: java.io.IOException: Unexpected code Response{protocol=h2, code=429, message=, url=https://api.openai.com/v1/engines/davinci/completions/}
I/okhttp.OkHttpClient:     at com.example.pocketgptd.LoginActivity$1.onResponse(LoginActivity.java:74)
I/okhttp.OkHttpClient:     at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
I/okhttp.OkHttpClient:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
I/okhttp.OkHttpClient:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
I/okhttp.OkHttpClient:     at java.lang.Thread.run(Thread.java:818)
tight ermine
#

I see, yeah, I think that's a rate limit error. Can you make sure of the following?

  • Regenerate an API key and then use the new key
  • Make sure that you are not out of quota (check your Hard Limit) in the API page in user settings
  • Make sure that you have not exceeded the maximum token count for the model that you're trying to use in the max_tokens parameters when you send the request
midnight roost
#

I don't know how to check Hard Limit, but for exceeding maximum token count I'm pretty sure that I have not since there are 2

tight ermine
#

What model are you using and what is your max_tokens set to for the request?

midnight roost
#

I'm using davinci mode and I haven't set max tokens

tight ermine
#

You need to set max_tokens in the requst

midnight roost
#

Yeah it's still not working

tight ermine
#

Can I see the code for how you send the request?

midnight roost
#
mLogin.setOnClickListener(view -> {
            final String enteredAPIKey = mAPIKey.getText().toString();
            // Perform login operation with the entered API key
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("https://api.openai.com/v1/engines/davinci/completions?max_tokens=100")
                    .addHeader("Authorization","Bearer"+enteredAPIKey)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    try (ResponseBody ignored = response.body()) {
                        if (response.code() == 200 || response.code() == 201) {
                            if (mRememberAPIKey.isChecked()) {
                                SharedPreferences.Editor editor = preferences.edit();
                                editor.putString(API_KEY, enteredAPIKey);
                                editor.apply();
                            }
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtra("api_key",apiKey);
                            startActivity(intent);
                        }
                        else {
                            Log.e("API Error", String.valueOf(response));
                            throw new IOException("Unexpected code " + response);
                        }
                    }
                }
            });
        });
tight ermine
#

You can't send max_tokens in a GET parameter url-encoded

#

it has to be a POST request with those parameters in the body of the request

#

you need prompt model max_temperature and a few more, the full list is available on the api documentation 🙂

midnight roost
#

well i'm trying to first verify that API is usable, but just now got idea thanks to you. Thanks for helping