aisdk/core
Composer 安装命令:
composer require aisdk/core
包简介
Framework-agnostic PHP AI SDK.
关键字:
README 文档
README
Framework-agnostic PHP AI SDK core: contracts, fluent API, value objects, streaming, tools, structured output, and PSR integration.
Installation
composer require aisdk/core
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 models allow terse call sites:
Generate::model(OpenAI::model('gpt-4o')); $result = Generate::text('Explain closures in PHP.')->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', description: 'The city and country extracted from the prompt.', properties: [ Schema::string(name: 'city')->required(), Schema::string(name: 'country')->required(), ], )) ->run(); echo $result->output['city']; // "Lahore"
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();
Class-based tools:
final class WeatherTool extends Tool { public function __construct() { $this->as('weather') ->for('Get current weather') ->input(Schema::string(name: 'city')->required()); } public function __invoke(string $city): string { return "Sunny in {$city}"; } }
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();
Stream hooks:
$stream->onChunk(fn (string $text) => log($text)) ->onFinish(fn (TextResult $result) => log($result->usage)) ->onError(fn (\Throwable $e) => log($e));
Custom Model Registration
Register new models at runtime 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, ]); $result = Generate::text('Hello') ->model(OpenAI::model('gpt-4.2')) ->run();
Unknown unregistered model IDs are allowed for text generation. The provider API will return a normalized error if the model does not exist.
Supported Capabilities
- Text generation (
Generate::text()) - Streaming text generation (
->stream()) - Tool calling (
->tool(...)) - Structured output (
->output(...)) - Provider options passthrough (
->providerOptions(...)) - Custom model registration (
::registerModel(...)) - Normalized provider errors
Testing
composer test
Core ships with fakes and assertions for deterministic testing:
use AiSdk\Tests\Fakes\FakeTextModel; $fake = new FakeTextModel(response: 'Hello!'); $result = Generate::text('Hi') ->model($fake) ->run(); expect($result->text)->toBe('Hello!');
Links
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-30