Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

⚡️ OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with OpenAI API

License

NotificationsYou must be signed in to change notification settings

markbeam/openai-php-laravel

 
 

Repository files navigation

GitHub Workflow Status (master)Total DownloadsLatest VersionLicense


OpenAI PHP for Laravel is a community-maintained PHP API client that allows you to interact with theOpen 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 theOpenAI PHP for Laravel. If you want to use theOpenAI PHP client in a framework-agnostic way, take a look at theopenai-php/client repository.

Get Started

RequiresPHP 8.1+

First, install OpenAI via theComposer package manager:

composer require openai-php/laravel

Next, execute the install command:

php artisan openai:install

This will create aconfig/openai.php configuration file in your project, which you can modify to your needsusing 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 theOpenAI facade to access the OpenAI API:

useOpenAI\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 beused to authenticate with the OpenAI API - you can find your API keyand organization on your OpenAI dashboard, athttps://openai.com.

OPENAI_API_KEY=OPENAI_ORGANIZATION=

Request Timeout

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

OPENAI_REQUEST_TIMEOUT=

Usage

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

Testing

TheOpenAI facade comes with afake() method that allows you to fake the API responses.

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

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

useOpenAI\Laravel\Facades\OpenAI;useOpenAI\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 sentOpenAI::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 theopenai-php/client repository.


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

About

⚡️ OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with OpenAI API

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP99.9%
  • Hack0.1%

[8]ページ先頭

©2009-2025 Movatter.jp