Hello everybody! I’m trying to add a line break to my prompt, but I’m having some issues.
The following request:
{
“messages”: [
{“role”: “user”, “content”: “My question with \n line breaker”}
]
} doesn’t work. I receive a Server returned HTTP response code: 400 for URL: https://api.openai.com/v1/chat/completions error.
However, this request:
{
“messages”: [
{“role”: “user”, “content”: “My question with no line breaker”}
]
} works fine.
It probably depends on what environment you’re using. Different programming languages and shells and parsers and things treat strings differently, and it’s possible that using \n breaks the json call something somewhere for you.
you can try \\n, but it’s hard to say without more information; how you’re running this.
Unfortunately \\n doesn’t work either.
I’m in a Java environment and i’m using gpt-4-1106-preview model.
I’m new to the subjet, can you tell me what information should I give ?
Ah. Have you considered using a string builder or concatenation or something for your request? then you can construct your content with a multiline string or something so you don’t have to worry about that stuff.
I would also encourage you to dump or print your final constructed request before you send it out, to ensure that you didn’t accidentally mangle it somehow.
there is nothing really prohibiting newlines; the resulting request just needs to be a properly formatted json.
and the error you’re getting says exactly that: 400: bad request - you mangled your json.
Given that, you could consider using jackson or something to build the json for you
I stopped using the openai-java library because the main contributor desn’t seem to be around anymore and we had a few problems earlier this year. So I’m doing everything from scratch now
So here’s my code :
URL obj = new URL("https://api.openai.com/v1/chat/completions");
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Bearer " + this.apiKey);
connection.setRequestProperty("Content-Type", "application/json");
StringBuilder bodyBuilder = new StringBuilder();
bodyBuilder.append("{\"model\": \"").append(model).append("\", \"messages\": [{\"role\": \"user\", \"content\": \"").append("Question with \n line breaker").append("\"}]}");
String body = bodyBuilder.toString();
System.out.println("BODY :: " +body);
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(body);
writer.flush();
writer.close();
// Response from ChatGPT
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
it’s tough. you likely need to replace/escape your newlines or add a line continuing character.
for escaping I don’t remember if it’s \\n or \\\\n. I think jackson would do it for you, but I would try one of these and check what your "BODY :: " output would say.
I haven’t touched java in quite a while and hope to never have to again