ahmadrosid/laravel-anthropic

Unofficial anthropic client library for laravel.

1.0.1 2024-10-15 03:02 UTC

This package is auto-updated.

Last update: 2024-12-15 03:20:33 UTC


README

Unofficial Anthropic AI client for laravel.

Installation

composer require ahmarosid/laravel-anthropic

Env

Add an environment variable with the key ANTHROPIC_API_KEY.

ANTHROPIC_API_KEY=sk-...

Usage

Chat without streaming.

use Ahmadrosid\Laravel\Anthropic\AnthropicAI;

$response = AnthropicAI::chat()->create([
    'model' => 'claude-3-opus-20240229',
    'temperature' => 0,
    'max_tokens' => 1024,
    'system' => 'You are a helpfull assistant',
    'messages' => [
        [
            'role' => 'user',
            'content' => 'Hello, how are you?'
        ]
    ],
]);

Chat with streaming.

use Ahmadrosid\Laravel\Anthropic\AnthropicAI;

$response = AnthropicAI::chat()->createStreamed([
    'model' => 'claude-3-opus-20240229',
    'temperature' => 0,
    'max_tokens' => 1024,
    'system' => 'You are a helpfull assistant',
    'messages' => [
        [
            'role' => 'user',
            'content' => 'Hello, how are you?'
        ]
    ],
]);

foreach ($response as $block) {
    foreach ($block->choices as $choice) {
        if ($choice->delta) {
            echo($choice->delta->content);
        }
    }
}

Testing

You can mock the response from anthropic.

AnthropicAI::fake([
    \Ahmarosid\Laravel\Anthropic\Responses\CreateResponse::fake([
        'role' => 'assistant',
        'content' => [
            [
                'type' => 'text',
                'text' => 'Example mock response',
            ]
        ],
        'usage' => [
            'input_tokens' => 10,
            'output_tokens' => 10
        ]
    ])
]);