Giter Club home page Giter Club logo

claudephp's Introduction

About

This guide is designed to teach people how to use Claude AI by Anthropic using a basic "I want one response" in PHP. This code is used in my travel itinerary generation website ReisPlan after I migrated it away from ChatGPT. I love how PHP handles JSON and XML data, so this tutorial is a bit easier. I'll be using curl for sending the data.

Setup

Declare your API key (insert your Claude AI API key in the quotes)

// API endpoint URL
$url = 'https://api.anthropic.com/v1/messages';

// Your OpenAI API key
$apiKey = '[API KEY]';

Setting up the request

The following code creates the JSON request to Claude AI. You can review all the options you can include here: https://docs.anthropic.com/claude/reference/messages_post.

It's worth noting that only the "model", "messages", and "max_tokens" are required in the body. There are many models, but I'm using claude-instant-1.2 in this example. Likewise, there are a few roles you can use such as system, user, assistance, or function. Typically, the system role defines what you want to accomplish. In our example, we'll tell Claude AI sytem role that is acting as a travel agent (used on my ReisPlan site).

First, we prepare the prompt statement:

  // Prompt for the conversation
    $messages = [
        [
            'role' => 'user',
            'content' => 'You are a travel advisor that will deliver a detailed itinerary based on the information provided by the user.'
        ]
    ];

Next, we build a JSON array. In the below scenario, I'm only sending the prompt and the model using an array and then encoding it into JSON data using the json_encode function.

// Convert messages to JSON
  $data = [
      'model' => 'claude-instant-1.2',
      'max_tokens' => 2048,
      'messages' => $messages
  ];

$jsonData = json_encode($postData);

Next, we set up the headers using an array to send the request using the Authorization header with our API Key and JSON content type. The curl array will set up the headers using post and the content using the $jsonData we built above.

// Setup the curl data
  $curl = curl_init();

  curl_setopt_array($curl, [
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => $jsonData,
      CURLOPT_HTTPHEADER => [
          'x-api-key: ' . $apiKey,
          'anthropic-version: 2023-06-01',
          'content-type: application/json'
      ],
  ]);

The curl_exec function will execute the above curl command

$response = curl_exec($curl);

Finally, we'll close the curl execution

// Execute curl
curl_close($curl);

Receiving the response

We'll use the json_decode function to read the $response variable from Claude AI.

// Decode the API response
$responseData = json_decode($response, true);

You will receive a JSON response like this in the $data variable (don't copy this):

{
  "id": "string",
  "content": [
    {
      "text": "string"
    },
    {
      "id": "string",
      "name": "string",
      "input": {}
    }
  ],
  "model": "string",
  "stop_reason": "end_turn",
  "stop_sequence": "string",
  "usage": {
    "input_tokens": 0,
    "output_tokens": 0
  }
}
  • content[0] is the first response
  • text is the response you're looking for

Next, we'll extract the $responseData using the above and then explode individual lines in the response. The $content variable contains the whole response.

But, I used $lines to break the reply response so I could get multiple lines of data and have them displayed on the website.

// Extract the assistant's reply
$content = $responseData['content'][0]['text'];

// Output the reply
// Assuming $reply contains the response from the API
$lines = explode("\n", $content);

The following loop examines each $line received and displays it on the website using the echo function.

// Loop through the lines
foreach ($lines as $line) {
    // Process each line as needed
    if (preg_match('/^\*\*Day \d+:\*\*$/', $line)) {
        echo '<strong>' . $line . "</strong><br>";
    } else {
        echo $line . '<br>';
    }
}

Everything together

Now, put it together in all its glory:

$url = 'https://api.anthropic.com/v1/messages';

// Your OpenAI API key
$apiKey = '[API KEY]'; 

// Claude
$model = 'claude-instant-1.2';
$maxTokens = 2048;
$temperature = 0.6;    
$messages = [
    [
        'role' => 'system',
        'content' => 'You are a travel advisor that will deliver a detailed itinerary based on the information provided by the user during the '.$season.' season. I am traveling to Denver, CO.'
    ]
];

$data = [
    'model' => $model,
    'max_tokens' => $maxTokens,
    'temperature' => $temperature,
    'messages' => $messages
];

$jsonData = json_encode($data);

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $jsonData,
    CURLOPT_HTTPHEADER => [
        'x-api-key: ' . $apiKey,
        'anthropic-version: 2023-06-01',
        'content-type: application/json'
    ],
]);

$response = curl_exec($curl);

curl_close($curl);

$responseData = json_decode($response, true);

$content = $responseData['content'][0]['text'];

Sure, I can recommend you check out Livin the Dream brewery in Littleton and Denver Beer Co in Englewood.

claudephp's People

Contributors

rkrehn avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.