Closed
Description
Hi,
I cannot seem to inject the service or access it in any way, unless I explicitly alias it as public - which I do not want.
Unless I am doing it wrong... please advise.
Example:
<?php
namespace App\Controller;
use OpenAI\Contracts\ClientContract;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class OpenAIController extends AbstractController
{
private $openai;
public function __construct(ClientContract $openai)
{
$this->openai = $openai;
}
#[Route('/openai/test', name: 'openai_test')]
public function test(): JsonResponse
{
$response = $this->openai->chat()->create([
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'user', 'content' => 'Hello, OpenAI!'],
],
]);
return $this->json(['response' => $response['choices'][0]['message']['content'] ?? 'No response']);
}
}
Metadata
Metadata
Assignees
Labels
No labels