A robust PHP package for interacting with Anthropic's Claude 3 API, supporting both text and vision capabilities.
- Easy-to-use interface for sending messages to Claude 3
- Support for text-based conversations with a simple chat method
- Vision capabilities - send images along with text prompts
- Streaming support for real-time responses
- Tool usage support
- Comprehensive error handling
- Fully tested with PHPUnit
You can install the package via composer:
composer require claude-php/claude-3-api
For simple text-based interactions, you can use the chat
method:
For simple text-based interactions, you can use the chat
method in several ways:
- Send a single string message:
use Claude\Claude3Api\Client;
use Claude\Claude3Api\Config;
// Create a configuration object with your API key
$config = new Config('your-api-key-here');
// Create a client
$client = new Client($config);
// Send a single string message
$response = $client->chat("Hello, Claude");
echo "Claude's response: " . $response->getContent()[0]['text'];
- Send a single message as an array:
$response = $client->chat(['role' => 'user', "content" => "Hello, Claude"]);
echo "Claude's response: " . $response->getContent()[0]['text'];
- Continue a conversation with multiple messages:
$response = $client->chat([
['role' => 'user', "content" => "Hello, Claude"],
['role' => 'assistant', "content" => "Hello! It's nice to meet you. How can I assist you today?"],
['role' => 'user', "content" => "What is the population of Sydney?"],
]);
echo "Claude's response: " . $response->getContent()[0]['text'];
- Specify a model (optional):
$response = $client->chat([
'model' => 'claude-3-opus-20240229',
'messages' => [
['role' => 'user', "content" => "Hello, Claude"],
]
]);
echo "Claude's response: " . $response->getContent()[0]['text'];
The chat
method is flexible and can handle various input formats, making it easy to interact with Claude in different scenarios.
For more complex scenarios, you can still use the sendMessage
method with a MessageRequest
object:
use Claude\Claude3Api\Models\Message;
use Claude\Claude3Api\Models\Content\TextContent;
use Claude\Claude3Api\Requests\MessageRequest;
// Create a message request
$messageRequest = new MessageRequest();
// Add a user message
$userMessage = new Message('user', [
new TextContent('What is the capital of France?')
]);
$messageRequest->addMessage($userMessage);
// Send the message and get the response
$response = $client->sendMessage($messageRequest);
// Process the response
echo "Claude's response: " . $response->getContent()[0]['text'];
use Claude\Claude3Api\Models\Content\ImageContent;
// Send a message with both image and text
$response = $client->sendMessageWithImage('path/to/image.jpg', 'What is in this image?');
echo "Claude's description: " . $response->getContent()[0]['text'];
$client->streamMessage($messageRequest, function ($chunk) {
if ($chunk instanceof MessageResponse) {
// Handle complete message response
} elseif (is_array($chunk) && isset($chunk['delta']['text'])) {
echo $chunk['delta']['text'];
}
});
use Claude\Claude3Api\Models\Tool;
$weatherTool = new Tool(
'get_weather',
'Get the current weather in a given location',
[
'type' => 'object',
'properties' => [
'location' => [
'type' => 'string',
'description' => 'The city and state, e.g. San Francisco, CA'
],
'unit' => [
'type' => 'string',
'enum' => ['celsius', 'fahrenheit'],
'description' => 'The unit of temperature'
]
],
'required' => ['location']
]
);
$messageRequest->addTool($weatherTool);
// Create a message request and add the tool
$messageRequest = new MessageRequest();
$messageRequest->addTool($weatherTool);
// Add a user message that might trigger tool use
$userMessage = new Message('user', [
new TextContent('What\'s the weather like in New York?')
]);
$messageRequest->addMessage($userMessage);
// Send the message
$response = $client->sendMessage($messageRequest);
// The response might include tool use
foreach ($response->getContent() as $content) {
if ($content['type'] === 'text') {
echo "Claude's response: " . $content['text'] . "\n";
} elseif ($content['type'] === 'tool_use') {
echo "Tool used: " . $content['name'] . "\n";
echo "Tool input: " . json_encode($content['input']) . "\n";
// Here you would typically execute the actual tool
// and send the result back to Claude in a new message
}
}
composer test
Contributions are welcome! Please feel free to submit a Pull Request.
This Claude 3 API Package is open-sourced software licensed under the MIT license.
If you encounter any problems or have any questions, please open an issue in the GitHub repository.
This package is not officially associated with Anthropic. Make sure to comply with Anthropic's terms of service when using this package.