aisdk/openai
Composer 安装命令:
composer require aisdk/openai
包简介
Official OpenAI provider for the PHP AI SDK.
README 文档
README
Official OpenAI provider for the PHP AI SDK.
Installation
composer require aisdk/openai
Basic Usage
use AiSdk\Generate; use AiSdk\OpenAI; $result = Generate::text() ->model(OpenAI::model('gpt-4o')) ->instructions('Write short, clear answers.') ->prompt('Explain closures in PHP.') ->run(); echo $result->text;
Default model shorthand:
Generate::model(OpenAI::model('gpt-4o')); $result = Generate::text('Explain closures in PHP.')->run();
Configuration
Environment Variables
| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY |
API key for authentication | Required |
OPENAI_BASE_URL |
Base URL for API requests | https://api.openai.com/v1 |
OPENAI_ORGANIZATION |
Organization ID header | None |
Programmatic Configuration
$provider = OpenAI::create([ 'apiKey' => 'sk-...', 'baseUrl' => 'https://api.openai.com/v1', 'organization' => 'org-...', 'headers' => ['X-Custom-Header' => 'value'], ]);
Supported Capabilities
| Capability | Support |
|---|---|
| Text generation | Native |
| Streaming | Native |
| Tool calling | Native |
| Structured output | Native (json_schema) |
| Reasoning | Native (reasoning_effort) |
| Text input | Native |
| Image input | Native |
| Audio input | Native |
| File input | Native |
Streaming
use AiSdk\Generate; use AiSdk\OpenAI; $stream = Generate::text('Tell me a story.') ->model(OpenAI::model('gpt-4o')) ->stream(); foreach ($stream->chunks() as $chunk) { echo $chunk; } $result = $stream->run();
Structured Output
use AiSdk\Generate; use AiSdk\OpenAI; use AiSdk\Schema; $result = Generate::text() ->model(OpenAI::model('gpt-4o')) ->prompt('Extract the city and country from: Lahore, Pakistan.') ->output(Schema::object( name: 'address', properties: [ Schema::string(name: 'city')->required(), Schema::string(name: 'country')->required(), ], )) ->run();
Tools
use AiSdk\Generate; use AiSdk\OpenAI; use AiSdk\Schema; use AiSdk\Tool; $weather = Tool::make('weather', 'Get current weather') ->input(Schema::string(name: 'city')->required()) ->run(fn (string $city): string => "Sunny in {$city}"); $result = Generate::text() ->model(OpenAI::model('gpt-4o')) ->prompt('What is the weather in Lahore?') ->tool($weather) ->run();
Custom Model Registration
Register new OpenAI models without waiting for a package release:
use AiSdk\Capability; use AiSdk\OpenAI; OpenAI::registerModel('gpt-4.2', capabilities: [ Capability::TextGeneration, Capability::Streaming, Capability::ToolCalling, Capability::StructuredOutput, Capability::TextInput, Capability::ImageInput, ]); $result = Generate::text('Hello') ->model(OpenAI::model('gpt-4.2')) ->run();
Unknown unregistered models are allowed for text generation. For unlisted advanced capabilities, opt in on the model handle:
$model = OpenAI::model('gpt-4.2-preview')->assume([ Capability::ToolCalling, Capability::StructuredOutput, ]);
The OpenAI API will return a normalized SDK exception if the model or requested feature is rejected by the provider.
Provider-Specific Options
Raw provider options can be passed as an escape hatch:
$result = Generate::text('Hello') ->model(OpenAI::model('gpt-4o')) ->providerOptions('openai', [ 'raw' => ['seed' => 42, 'service_tier' => 'default'], ]) ->run();
Testing
composer test
Links
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-30