Hello everyone, regular user of chatGPT and young web developer, I started to do a small personal project
around this in PHP/Symfony, with the purpose of calling the api in my project.
Unfortunately, I get a 404 Not Found returned for "https://api.openai.com/v1/completions error when calling the api.
Here is my controller:
class HomeController extends AbstractController
{
#[Route('/', name: 'home')]
public function home(Request $request, \App\Service\OpenAiService $openAi): Response
{
$form = $this->createForm(RegexType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$json = $openAi->getResponse($data['regex']);
dd($json);
}
return $this->render('home/index.html.twig', [
'form' => $form,
]);
}
}
Here is my OpenAiService.php:
class OpenAiService
{
public function __construct (
private HttpClientInterface $client, private ParameterBagInterface $para
){
}
public function getResponse(string $regex): string
{
$open_ai_key = $this->para->get('OPENAI_API_KEY');
$response = $this->client->request(
'POST',
'https://api.openai.com/v1/completions',
[
'headers' => [
'Content-Type' => 'application/json'
],
'json' => [
'model' =>'test-davinci-002',
'prompt' => 'Salut chat GPT',
'temperature' => 0,
'max_tokens' => 1024,
'frequency_penality' => 0.5,
'presence_penality' => 0,
],
'auth_bearer' => $open_ai_key,
]
);
$respense_array = $response->toArray() ?? [];
switch ($response->getStatusCode()){
case 200:
return n12br($respense_array['choices'][0]['text']);
break;
default:
return 'Bad request';
break;
}
}
}
The api key being stored in a .env.local, this one is good because when I change it, I get a 403 error.
Anyone got a lead for me?
Thanks in advance for reading me.
PS. sorry for my bad English