gregpriday / laravel-claude-chat
这是我开发的laravel-claude-chat包
0.3
2024-06-24 13:10 UTC
Requires
- php: ^8.1
- caseyamcl/guzzle_retry_middleware: ^2.9
- guzzlehttp/guzzle: ^7.8
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- larastan/larastan: ^2.0.1
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8|^8.0
- orchestra/testbench: ^8.8|^9.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.26
README
Laravel Claude Chat是一个提供将Anthropic的Claude AI简单集成到Laravel应用的包。它允许您轻松地向Claude API发送请求并接收响应。
特性
- 轻松集成Laravel
- 支持Claude API请求的自定义参数
- 自动重试失败请求的机制
- 方便访问
ClaudeChat类的门面
安装
您可以通过composer安装此包
composer require gregpriday/laravel-claude-chat
配置
发布配置文件
php artisan vendor:publish --provider="GregPriday\ClaudeChat\ClaudeChatServiceProvider"
发布的配置文件位于config/claude.php。您需要在文件中设置您的Claude API密钥和端点
return [ 'api_key' => env('CLAUDE_API_KEY'), 'endpoint' => env('CLAUDE_API_URL'), 'request_timeout' => 30, 'retry' => [ 'retries' => 5, 'retry_on_status' => [429, 500, 502, 503, 504], 'retry_on_timeout' => true, 'delay' => 1000, 'multiplier' => 2, 'max_delay' => 10000, ], ];
请确保将您的Claude API密钥和端点添加到您的.env文件中
CLAUDE_API_KEY=your-claude-api-key
CLAUDE_API_URL=https://api.anthropic.com/v1/complete
使用
您可以使用ClaudeChat类向Claude API发送请求
use GregPriday\ClaudeChat\ClaudeChat; $claudeChat = new ClaudeChat(config('claude.api_key'), config('claude.endpoint')); $response = $claudeChat->create([ 'prompt' => 'Hello, Claude!', 'model' => 'claude-v1', ]); echo $response->completion;
您还可以使用ClaudeChat门面以更方便的方式访问类
use GregPriday\ClaudeChat\Facades\ClaudeChat; $response = ClaudeChat::create([ 'prompt' => 'Hello, Claude!', 'model' => 'claude-v1', ]); echo $response->completion;
检索JSON响应
如果您想将Claude的响应作为JSON对象检索,可以使用createJson方法
$response = ClaudeChat::createJson([ 'prompt' => 'Generate a JSON object with a "message" field.', ]); $jsonObject = $response->content[0]->object;
createJson方法会自动从响应中提取JSON对象并将其作为PHP对象返回。
测试
composer test
变更日志
请参阅CHANGELOG了解最近更改的详细信息。
贡献
请参阅CONTRIBUTING了解详细信息。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。