PHP code example of alle-ai / anthropic-api-php

1. Go to this page and download the library: Download alle-ai/anthropic-api-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/ */

    

alle-ai / anthropic-api-php example snippets


// Load your project's composer autoloader (If you aren't already doing so).

$api_key = 'your-anthropic-api-key';


$api_key = 'your-anthropic-api-key';
$anthropic_version = "2023-06-01";

$anthropic_api = new Alle_AI\Anthropic\AnthropicAPI($api_key, $anthropic_version);
$prompt = "How many toes do dogs have?";
$data = array(
    'prompt' => "\n\nHuman: ".$prompt."\n\nAssistant:", // Be sure to format prompt appropriately
    'model' => 'claude-2.1',
    'max_tokens_to_sample' => 300,
    'stop_sequences' => array("\n\nHuman:")
  );
$response = $anthropic_api->generateText($data);

echo $response['completion']; // To display only completion

// Claude's Response: 
// Dogs typically have 4 toes on each of their front paws and 5 toes on each of their back paws, for a total of 18 toes.