gregpriday / laravel-claude-chat
This is my package laravel-claude-chat
Fund package maintenance!
Greg Priday
Requires
- php: ^8.1
- caseyamcl/guzzle_retry_middleware: ^2.9
- guzzlehttp/guzzle: ^7.8
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- larastan/larastan: ^2.0.1
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- orchestra/testbench: ^8.8
- 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 is a package that provides a simple way to integrate Anthropic's Claude AI into your Laravel application. It allows you to easily send requests to the Claude API and receive responses.
Features
- Easy integration with Laravel
- Supports custom arguments for Claude API requests
- Automatic retry mechanism for failed requests
- Facade for convenient access to the
ClaudeChat
class
Installation
You can install the package via composer:
composer require gregpriday/laravel-claude-chat
Configuration
Publish the configuration file:
php artisan vendor:publish --tag="laravel-claude-chat-config"
The published configuration file is located at config/claude.php
. You need to set your Claude API key and endpoint in this file:
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, ], ];
Make sure to add your Claude API key and endpoint to your .env
file:
CLAUDE_API_KEY=your-claude-api-key
CLAUDE_API_URL=https://api.anthropic.com/v1/complete
Usage
You can use the ClaudeChat
class to send requests to the 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;
You can also use the ClaudeChat
facade for a more convenient way to access the class:
use GregPriday\ClaudeChat\Facades\ClaudeChat; $response = ClaudeChat::create([ 'prompt' => 'Hello, Claude!', 'model' => 'claude-v1', ]); echo $response->completion;
Retrieving JSON Responses
If you want to retrieve the response from Claude as a JSON object, you can use the createJson
method:
$response = ClaudeChat::createJson([ 'prompt' => 'Generate a JSON object with a "message" field.', ]); $jsonObject = $response->content[0]->object;
The createJson
method automatically extracts the JSON object from the response and returns it as a PHP object.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.