I’m trying to call the gpt-3.5-turbo model api through a java program, and getting a 400 error. I’m pretty sure that the message is valid json. Anyone see anything off here?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class ChatGPTClient {
public static void chatGPT() throws Exception {
String url = "https://api.openai.com/v1/chat/completions";
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Bearer MY-API-KEY");
String model = "gpt-3.5-turbo";
String prompt = "Hello, can you help me with something?";
int maxTokens = 50;
con.setDoOutput(true);
String body = "{\"model\": \"" + model + "\", \"prompt\": \"" + prompt + "\", \"max_tokens\": " + maxTokens + "}";
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(body);
writer.flush();
System.out.println(body);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
public static void main(String[] args) throws Exception {
chatGPT();
}
}
{"model": "gpt-3.5-turbo", "prompt": "Hello, can you help me with something?", "max_tokens": 50}
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.openai.com/v1/chat/completions
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1997)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1589)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
at ChatGPTClient.chatGPT(ChatGPTClient.java:42)
at ChatGPTClient.main(ChatGPTClient.java:54)
Not sure if this is related but we’re also using java with httpURLConnection and have successfully been calling the dalle 2 API for weeks now (https://api.openai.com/v1/images/generations) - suddenly today we have started getting the 400 errors and nothing has changed on the coding end!
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.openai.com/v1/chat/completions
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1997)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1589)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
at ChatGPTClient.chatGPT(ChatGPTClient.java:29)
at ChatGPTClient.main(ChatGPTClient.java:41)
Also if you did not want to implement everything from scratch, there is two community Java libraries are available. Where you already have this functionality.