aisdk/anthropic
Composer 安装命令:
composer require aisdk/anthropic
包简介
Official Anthropic provider for the PHP AI SDK.
关键字:
README 文档
README
Official Anthropic provider for the PHP AI SDK.
Installation
composer require aisdk/anthropic
Basic Usage
use AiSdk\Anthropic; use AiSdk\Generate; $result = Generate::text() ->model(Anthropic::model('claude-sonnet-4')) ->instructions('Write short, clear answers.') ->prompt('Explain closures in PHP.') ->run(); echo $result->text;
Default model shorthand:
Generate::model(Anthropic::model('claude-sonnet-4')); $result = Generate::text('Explain closures in PHP.')->run();
Configuration
Environment Variables
| Variable | Description | Default |
|---|---|---|
ANTHROPIC_API_KEY |
API key for authentication | Required |
ANTHROPIC_BASE_URL |
Base URL for API requests | https://api.anthropic.com/v1 |
ANTHROPIC_VERSION |
API version header | 2023-06-01 |
Programmatic Configuration
$provider = Anthropic::create([ 'apiKey' => 'sk-ant-...', 'baseUrl' => 'https://api.anthropic.com/v1', 'version' => '2023-06-01', 'headers' => ['anthropic-beta' => 'extended-thinking-2025-05-14'], ]);
Supported Capabilities
| Capability | Support |
|---|---|
| Text generation | Native |
| Streaming | Native |
| Tool calling | Native |
| Structured output | Adapted (forced tool use) |
| Reasoning | Native (thinking blocks) |
| Text input | Native |
| Image input | Native |
| File input | Native (documents) |
Streaming
use AiSdk\Anthropic; use AiSdk\Generate; $stream = Generate::text('Tell me a story.') ->model(Anthropic::model('claude-sonnet-4')) ->stream(); foreach ($stream->chunks() as $chunk) { echo $chunk; } $result = $stream->run();
Structured Output
Anthropic does not natively support json_schema response format. Structured output is adapted through forced tool use:
use AiSdk\Anthropic; use AiSdk\Generate; use AiSdk\Schema; $result = Generate::text() ->model(Anthropic::model('claude-sonnet-4')) ->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();
Reasoning
use AiSdk\Anthropic; use AiSdk\Generate; use AiSdk\Reasoning; $result = Generate::text('Solve: what is 2+2?') ->model(Anthropic::model('claude-sonnet-4')) ->reasoning(Reasoning::effort('high')) ->run();
Tools
use AiSdk\Anthropic; use AiSdk\Generate; 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(Anthropic::model('claude-sonnet-4')) ->prompt('What is the weather in Lahore?') ->tool($weather) ->run();
Custom Model Registration
Register new Anthropic models without waiting for a package release:
use AiSdk\Anthropic; use AiSdk\Capability; Anthropic::registerModel('claude-5-sonnet', capabilities: [ Capability::TextGeneration, Capability::Streaming, Capability::ToolCalling, Capability::StructuredOutput, Capability::Reasoning, Capability::TextInput, Capability::ImageInput, ]); $result = Generate::text('Hello') ->model(Anthropic::model('claude-5-sonnet')) ->run();
Use ModelDefinition only when you need metadata or adapted-capability details.
Provider-Specific Options
Raw provider options can be passed as an escape hatch:
$result = Generate::text('Hello') ->model(Anthropic::model('claude-sonnet-4')) ->providerOptions('anthropic', [ 'raw' => ['top_k' => 40], ]) ->run();
Testing
composer test
Links
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-30