OpenAI PHP for Laravel

repository·main·Indexed 25 days ago

https://github.com/openai-php/laravel

A Laravel-specific integration of the OpenAI PHP client. It provides a Facade-based interface to interact with the OpenAI API, including support for environment variable configuration, API response faking for tests, and request assertions.

Tokens
1K
Snippets
5
Records
6
Agent score
37%

What's inside openai-php/laravel

  1. Install OpenAI PHP for Laravel

    main

    To integrate OpenAI into your Laravel application, install the package via Composer and then run the artisan install command to publish the configuration file.

    composer require openai-php/laravel
    
    php artisan openai:install
  2. Configure OpenAI via environment variables

    main

    You can configure the OpenAI client using environment variables in your .env file. The following keys are available:

    • OPENAI_API_KEY: Your OpenAI API Key.
    • OPENAI_ORGANIZATION: Your OpenAI organization ID.
    • OPENAI_PROJECT: Your OpenAI project ID.
    • OPENAI_BASE_URL: The base URL for the OpenAI API (defaults to api.openai.com/v1).
    • OPENAI_REQUEST_TIMEOUT: Maximum seconds to wait for a response (defaults to 30).
    OPENAI_API_KEY=sk-...
    OPENAI_ORGANIZATION=org-...
    OPENAI_PROJECT=proj_...
    OPENAI_BASE_URL=
    OPENAI_REQUEST_TIMEOUT=
  3. Install OpenAI for Laravel

    main

    Use the openai:install Artisan command to prepare the OpenAI client for use in your Laravel application. This command automates the following setup steps:

    1. Publishes the configuration file: Creates config/openai.php using the package's service provider.
    2. Updates environment variables: Appends the required OPENAI_API_KEY and OPENAI_ORGANIZATION keys to your .env and .env.example files.

    After running the command, you must manually add your actual OpenAI API key and organization ID to your .env file.

  4. Assert OpenAI requests were sent

    main

    Use OpenAI::assertSent() to verify that specific API requests were dispatched with the expected method and parameters. This is useful for ensuring your application logic correctly constructs API calls.

    use OpenAI\Resources\Responses;
    
    // assert completion create request was sent
    OpenAI::assertSent(Responses::class, function (string $method, array $parameters): bool {
        return $method === 'create' &&
            $parameters['model'] === 'gpt-5' &&
            $parameters['prompt'] === 'PHP is ';
    });
  5. Use the OpenAI Facade to interact with the API

    main

    Use the OpenAI\Laravel\Facades\OpenAI facade to access OpenAI resources and methods. This provides a convenient way to make API calls within your Laravel application.

    use OpenAI\
    Laravel\
    Facades\
    OpenAI;
    
    $response = OpenAI::responses()->create([
        'model' => 'gpt-5',
        'input' => 'Hello!',
    ]);
    
    echo $response->outputText; // Hello! How can I assist you today?
  6. Fake OpenAI API responses in tests

    main

    The OpenAI facade provides a fake() method to intercept API calls and return predefined responses. You can use specific response fake methods (e.g., CreateResponse::fake()) to define the structure of the returned data. Responses are returned in the order they are provided to the fake() method.

    use OpenAI\Laravel\Facades\OpenAI;
    use OpenAI\Responses\Responses\CreateResponse;
    
    OpenAI::fake([
        CreateResponse::fake([
            'choices' => [
                [
                    'text' => 'awesome!',
                ],
            ],
        ]),
    ]);
    
    $response = OpenAI::responses()->create([
        'model' => 'gpt-5',
        'input' => 'PHP is ',
    ]);
    
    expect($response->outputText)->toBe('awesome!');