Skip to content

Cannot access service #21

Closed
Closed
@globexdigitalentertainment

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions