0

I am trying to make simple app to send text on ChatGPT API, using the API key but I am getting 403 response.

private class SendRequestTask extends AsyncTask<String, Void, String> {

        private static final String API_KEY = "**************************************************";
        private static final String ENDPOINT_URL = "https://api.openai.com/v1/engines/text-davinci-003/jobs";

        @Override
        protected String doInBackground(String... params) {
            String input = params[0];
            String response = "";

            try {
                URL url = new URL(ENDPOINT_URL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
                connection.setDoOutput(true);

                String requestBody = "{\"prompt\": \"" + input + "\"}";
                OutputStream os = connection.getOutputStream();
                os.write(requestBody.getBytes());
                os.flush();
                os.close();

                int responseCode = connection.getResponseCode();
                Log.d("Response Code", String.valueOf(responseCode));

                if (responseCode == HttpURLConnection.HTTP_OK) {
                    String line;
                    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    while ((line = br.readLine()) != null) {
                        response += line;
                    }

After flushing the DNS, I've tried cleaning, rebuilding, opening Android studio as an administrator and copy all the code to a new project but with the same result. I've tested in on my Android phone as well and I found no info in the documentation.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Welcome to [so]. 1. ChatGPT hasn't an official API yet. 2. The endpoint in this question has being deprecated. Ref. https://platform.openai.com/docs/api-reference/engines. 3. The question doesn't include enough details / clarity. – Rubén Feb 14 '23 at 02:09
  • 1
    Thank you for the clarification Ruben. With your and Kane Hopper's replies, I've located my silly mistake and will continue working on it from now on. – Ivaylo Tsvetkov Feb 14 '23 at 20:45

1 Answers1

0

You need to change the GPT endpoint. If you ask ChatGPT to write the code, given it was trained up to 2011 it provides the old endpoint.

Change the endpoint URL to:

private static final String ENDPOINT_URL = "https://api.openai.com/v1/completions"

Ref: https://platform.openai.com/docs/api-reference/authentication

Kane Hooper
  • 1,531
  • 1
  • 9
  • 21