What is wrong with this request?

I’m getting error message 400 Bad Request and cannot diagnoze what could cause it (followed the documentation).

My message is the follows:
{“max_tokens”:4096,“temperature”:0.2,“messages”:[{“role”:“system”,“content”:“”},{“role”:“user”,“content”:“Describe company De’ Longhi SPA”}],“model”:“gpt-4-1106-preview”}

Where could be the problem?

To add further details, I have Java code that generates this

      URL url = new URL("https://api.openai.com/v1/chat/completions");
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      //connection.setConnectTimeout(8000 * 60); //wait 8 minute the most
      //connection.setReadTimeout(8000 * 60);
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
      connection.setRequestProperty("Content-Type", "application/json");
      connection.setDoOutput(true);
      OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());  
    
      JSONObject data = new JSONObject();
      data.put("model", "gpt-4-1106-preview");
      data.put("temperature", 0.2);
      data.put("max_tokens", 4096);
      JSONArray messages = new JSONArray();
      JSONObject systemObject = new JSONObject();
      systemObject.put("role", "system");
      systemObject.put("content", system);
      messages.put(0, systemObject);
      JSONObject userObject = new JSONObject();
      userObject.put("role", "user");
      userObject.put("content", message);
      messages.put(1, userObject);
      data.put("messages",messages);
      String dataStr = data.toString();
      System.out.println(dataStr);
      osw.append(dataStr);
      String response = connection.getResponseMessage();
      System.out.println(connection.getResponseCode());
      System.out.println(response);
      osw.flush();
      osw.close();

why is your system prompt blank? what detail is accompanying the 400 status? can you share the complete response?

1 Like

I have discovered the problem, I didn’t send the complete Request before requesting the Response.

This would be a code that is working:

    URL url = new URL("https://api.openai.com/v1/chat/completions");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    //connection.setConnectTimeout(8000 * 60); //wait 8 minute the most
    //connection.setReadTimeout(8000 * 60);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setDoOutput(true);
    OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());

    JSONObject data = new JSONObject();
    data.put("model", "gpt-4-1106-preview");
    data.put("temperature", 0.2);
    data.put("max_tokens", 4096);
    JSONArray messages = new JSONArray();
    JSONObject systemObject = new JSONObject();
    systemObject.put("role", "system");
    systemObject.put("content", system);
    messages.put(0, systemObject);
    JSONObject userObject = new JSONObject();
    userObject.put("role", "user");
    userObject.put("content", message);
    messages.put(1, userObject);
    data.put("messages", messages);

    String dataStr = data.toString();
    System.out.println(dataStr);
    osw.write(dataStr);
    osw.flush();
    osw.close();

    //String response = connection.getResponseMessage();
    if (connection.getResponseCode() != 200) {
      System.out.println(connection.getResponseCode());
      System.out.println(connection.getResponseMessage());
      InputStream error = connection.getErrorStream();
      InputStreamReader isrerror = new InputStreamReader(error);
      BufferedReader bre = new BufferedReader(isrerror);
      String linee;
      while ((linee = bre.readLine()) != null) {
        System.out.println(linee);
      }
      return "";
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;

    StringBuffer response = new StringBuffer();

    while ((line = br.readLine()) != null) {
      response.append(line);
    }
    br.close();
    return response.toString();

2 Likes