Skip to content

Cannot access service #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
globexdigitalentertainment opened this issue Nov 21, 2024 · 2 comments · Fixed by #22
Closed

Cannot access service #21

globexdigitalentertainment opened this issue Nov 21, 2024 · 2 comments · Fixed by #22

Comments

@globexdigitalentertainment

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']);
    }
}

@globexdigitalentertainment
Copy link
Author

Anyone care to provide a small example?

@GromNaN
Copy link
Collaborator

GromNaN commented Nov 22, 2024

The service for the interface OpenAI\Contracts\ClientContract is not defined. This will be fixed by #22. You can use the OpenAPI\Client class for autowiring.

<?php

  namespace App\Controller;

- use OpenAI\Contracts\ClientContract;
+ use OpenAI\Client;
  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)
+    public function __construct(Client $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']);
      }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants