PHP code example of wpai-inc / anthropic-sdk-php

1. Go to this page and download the library: Download wpai-inc/anthropic-sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wpai-inc / anthropic-sdk-php example snippets


$anthropic = new \WpAi\Anthropic\AnthropicAPI($apiKey);

$messages = [
    [
        'role' => 'user',
        'content' => 'How can you help me?',
    ],
];
$options = [
    'model' => 'claude-3-opus-20240229',
    'maxTokens' => 1024,
    'messages' => $messages,
];

$anthropic->messages()->create($options);

$anthropic->messages()->maxTokens(2048)->create($messages);

$response = Anthropic::messages()->create($query, [ 'anthropic-beta' => 'max-tokens-3-5-sonnet-2024-07-15' ]);

$anthropic->messages()->stream($options);

while (! $stream->eof()) {
    echo $stream->read(1024);
    ob_flush();
    flush();
}

use WpAi\Anthropic\Facades\Anthropic;

// Create a message
$response = Anthropic::messages()
    ->model('claude-v1')
    ->maxTokens(100)
    ->messages([
        ['role' => 'user', 'content' => 'Hello, Claude!'],
    ])
    ->create();

// Stream a message
$stream = Anthropic::messages()
    ->model('claude-v1')
    ->maxTokens(100)
    ->messages([
        ['role' => 'user', 'content' => 'Tell me a story.'],
    ])
    ->stream();
sh
php artisan vendor:publish --provider="WpAi\Anthropic\Providers\AnthropicServiceProvider"