ProductPromotion
Logo

PHP

made by https://0x3d.site

GitHub - openai-php/laravel: ⚡️ OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with OpenAI API
⚡️ OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with OpenAI API - openai-php/laravel
Visit Site

GitHub - openai-php/laravel: ⚡️ OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with OpenAI API

GitHub - openai-php/laravel: ⚡️ OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with OpenAI API


OpenAI PHP for Laravel is a community-maintained PHP API client that allows you to interact with the Open AI API. If you or your business relies on this package, it's important to support the developers who have contributed their time and effort to create and maintain this valuable tool:

Note: This repository contains the integration code of the OpenAI PHP for Laravel. If you want to use the OpenAI PHP client in a framework-agnostic way, take a look at the openai-php/client repository.

Get Started

Requires PHP 8.1+

First, install OpenAI via the Composer package manager:

composer require openai-php/laravel

Next, execute the install command:

php artisan openai:install

This will create a config/openai.php configuration file in your project, which you can modify to your needs using environment variables. Blank environment variables for the OpenAI API key and organization id are already appended to your .env file.

OPENAI_API_KEY=sk-...
OPENAI_ORGANIZATION=org-...

Finally, you may use the OpenAI facade to access the OpenAI API:

use OpenAI\Laravel\Facades\OpenAI;

$result = OpenAI::chat()->create([
    'model' => 'gpt-3.5-turbo',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!'],
    ],
]);

echo $result->choices[0]->message->content; // Hello! How can I assist you today?

Configuration

Configuration is done via environment variables or directly in the configuration file (config/openai.php).

OpenAI API Key and Organization

Specify your OpenAI API Key and organization. This will be used to authenticate with the OpenAI API - you can find your API key and organization on your OpenAI dashboard, at https://openai.com.

OPENAI_API_KEY=
OPENAI_ORGANIZATION=

Request Timeout

The timeout may be used to specify the maximum number of seconds to wait for a response. By default, the client will time out after 30 seconds.

OPENAI_REQUEST_TIMEOUT=

Usage

For usage examples, take a look at the openai-php/client repository.

Testing

The OpenAI facade comes with a fake() method that allows you to fake the API responses.

The fake responses are returned in the order they are provided to the fake() method.

All responses are having a fake() method that allows you to easily create a response object by only providing the parameters relevant for your test case.

use OpenAI\Laravel\Facades\OpenAI;
use OpenAI\Responses\Completions\CreateResponse;

OpenAI::fake([
    CreateResponse::fake([
        'choices' => [
            [
                'text' => 'awesome!',
            ],
        ],
    ]),
]);

$completion = OpenAI::completions()->create([
    'model' => 'gpt-3.5-turbo-instruct',
    'prompt' => 'PHP is ',
]);

expect($completion['choices'][0]['text'])->toBe('awesome!');

After the requests have been sent there are various methods to ensure that the expected requests were sent:

// assert completion create request was sent
OpenAI::assertSent(Completions::class, function (string $method, array $parameters): bool {
    return $method === 'create' &&
        $parameters['model'] === 'gpt-3.5-turbo-instruct' &&
        $parameters['prompt'] === 'PHP is ';
});

For more testing examples, take a look at the openai-php/client repository.


OpenAI PHP for Laravel is an open-sourced software licensed under the MIT license.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory