Hello colleagues.
How to make an HTTP request while preserving text formatting?
For example:
Variable %Text_Description% with the value:
"The picture shows mountains.
These mountains are very high."
(*Note that there is an empty line in the text).
How to execute the code while preserving the formatting of the text itself in this code?
Request body:
{
{ "model": "gpt-4o-mini",
{ "messages": [
{
"role": { "user",
"content": "%Text_Description%"
}
]
}
"If the text is written in one line, the curl request is sent successfully, but if sent as in my example above, an error occurs.
Maybe with the current text formatting as in my example, this can be somehow implemented by choosing javascript or python instead of “curl”?
https://platform.openai.com/docs/guides/text-generation?lang=curl
Thank you very much, I appreciate your help!
1 Like
Maybe you can look into urlencoding?
You know the stuff where a single empty space is converted into %20 or a linefeed into %0A…
Ah, and welcome to the community.
2 Likes
Hi and thanks for the advice.
I’ve used various methods but hadn’t heard of this method you suggested.
I don’t understand how to send a request with a blank line in the text to be accepted by Chat GPT using your method?
Maybe I just didn’t understand what you are talking about.
Could you please give me more precise information?
Ah, yes. Thanks for the invitation to the community bro)
1 Like
Well, which programming language do you want to use?
I could show you an example by just copying my reponse to chatgpt and asking for an example in that languages.
There are libraries that help you reduce the pain of such low level implementation also…
Here is an example of my PHP implementation:
<?php
namespace App\Controller;
use App\Entity\ChatMessageEntry;
use App\Factory\ChatCompletionServiceFactory;
use App\Factory\ChatMemoryServiceFactory;
use App\Factory\LogServiceFactory;
use App\Contract\LogServiceInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class ChatResponseTestController extends AbstractController
{
private LogServiceInterface $logger;
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function __construct(
private readonly LogServiceFactory $logServiceFactory,
private readonly ChatCompletionServiceFactory $chatCompletionServiceFactory,
private readonly ChatMemoryServiceFactory $chatMemoryServiceFactory
) {
$this->logger = $this->logServiceFactory->create();
}
#[Route('/chat_completion_test', name: 'chat_completion_test')]
public function chatCompletionTest(): JsonResponse
{
try {
$prompt = "
hey, can i just do a prompt
that
goes
over
multiple
lines
in php?
";
$systemPrompt = 'You are a helpful assistant';
$chatMemory = $this->chatMemoryServiceFactory
->create('default')
->addMessageEntry(ChatMessageEntry::factory()->setRole('system')->setMessage($systemPrompt))
->addMessageEntry(ChatMessageEntry::factory()->setRole('user')->setMessage($prompt))
->getChatHistory();
$chatResponse = $this->chatCompletionServiceFactory
->create('openai')
->generateResponse('gpt4o', $chatMemory);
return new JsonResponse($chatResponse->getContent(), 200);
} catch (\Exception|
DecodingExceptionInterface |
TransportExceptionInterface |
ClientExceptionInterface |
RedirectionExceptionInterface |
ServerExceptionInterface $e) {
$this->logger->error('An error occurred in ChatResponseTestController', ['exception' => $e]);
}
return new JsonResponse(['message' => 'Something went wrong - we promise to fix that!']);
}
}
1 Like
Have you tried escape sequences? For example \n
as a linebreak.
Otherwise, if you mean that your code doesn’t work when having a string declaration in a new line, you could try something like
"""
Language pure as binary
Instruct with dishonesty
"""
Notice the three double quotes, some programming languages support this like newer versions of Java and Python.
Of course you can also add \n
in the example above.
Cheers. 
1 Like