I have a problem using openia in the Laravel Framework 10.41.0 environment.
Whenever I send openIA requests to access the API the following errors occur. Failed to load resource: the server responded with a status of 500 (Internal Server Error) and when clicking on this error in the browser console it takes me to the /chat route that I created for the POST method. And there the following error is sent to me: The GET method is not supported for route chat. Supported methods: POST.
I have already applied the following commands to the solutions, but without success:
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
composer dump-autoload
Scenario:
I use ajax as a script at the end of my layout.blade.php screen which has @yield('content') in the middle of it and my index.blade.php screen has the chat where I send requests.
AJAX:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
$('#btn-submit').on('click', function(){
$message = $('#message').val();
$('#historic').append(`<div class="mb-2">
<div class="box-my-message">
<p class="my-message"> `+$message+` </p>
</div>
</div>`);
$.ajax({
type: 'post',
url: '{{url('chat')}}',
date: {
'input': $message
},
success: function(data){
$('#historic').append(`<div class="d-flex mb-2">
<div class="box-response-message">
<p class="my-message"> `+data+` </p>
</div>
</div>
`)
$message = $('#message').val('');
}
});
});
My routes:
Route::post('/chat', [ChatController::class, 'chat']);
My Controller:
<?php
class ChatController extends Controller
{
public function chat(Request $request)
{
if ($request->input == null) {
return "Empty.";
}
$message = $request->input;
$result = OpenAI::chat()->create([
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
]);
echo $result->choices[0]->message->content; // Hello! How can I watch you today?
$result = $client->completions()->create([
"model" => "gpt-3.5-turbo",
"temperature" => 0.5,
'max_tokens' => 4096,
'prompt' => sprintf('Write article about: %s', $message),
]);
$content = trim($result['choices'][0]['text']);
return $content;
}
}
I have also added the variables with the key in the .env. I simply cannot understand where this problem comes from and what the possible solutions are.