Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/namankumar80510/claude-sdk
A Simple PHP SDK For Using Anthropic Claude API
https://github.com/namankumar80510/claude-sdk
Last synced: 9 days ago
JSON representation
A Simple PHP SDK For Using Anthropic Claude API
- Host: GitHub
- URL: https://github.com/namankumar80510/claude-sdk
- Owner: namankumar80510
- License: mit
- Created: 2024-07-22T14:30:47.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-09-23T09:16:20.000Z (about 2 months ago)
- Last Synced: 2024-11-03T22:48:26.570Z (15 days ago)
- Language: PHP
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Claude API PHP SDK
This is a simple but useful PHP Class that you can use to communicate with Anthropic's Claude via API.
## Installation
```bash
composer require dikki/claude-sdk
```## Usage
### Creating an Instance
First, create an instance of the `Claude` class. Pass the API key as the first parameter. You can pass the model name as the second parameter or keep it empty to use the default model (`claude-2.1`).
```php
$claude = new \Dikki\Claude\Claude($apiKey, 'claude-2.1');
```### Methods
#### getResponse
To get the whole response from Claude, use the `getResponse()` method. It accepts the following parameters:
- **string $prompt**: The prompt to send to the API.
- **array $messages**: Optional. The messages to send to the API.
- **string|null $model**: Optional. The model to use for the API request.
- **int $maxTokens**: Optional. The maximum number of tokens to generate (default is 4000).
- **string $method**: Optional. The HTTP method to use for the request (default is 'POST').Returns an array containing the full response, including text, model used, etc.
```php
$response = $claude->getResponse("Write an essay on AI.");
```#### getTextResponse
To get only the string response, use the `getTextResponse()` method. It accepts the same parameters as `getResponse()`.
Returns a string response.
```php
$response = $claude->getTextResponse("Write an essay on AI.");
```### Example Usage
```php
// Create an instance of Claude
$claude = new \Dikki\Claude\Claude($apiKey, 'claude-2.1');// Get full response
$response = $claude->getResponse("Write an essay on AI.");// Get only text response
$textResponse = $claude->getTextResponse("Write an essay on AI.");
```### Class Overview
- **Class**: `Claude`
- **Namespace**: `Dikki\Claude`
- **Constructor Parameters**:
- `string $apiKey`: The API key for authentication.
- `string $model`: The model to use for the API requests (default is 'claude-2.1').
- `string $modelVersion`: The version of the model to use (default is '2023-06-01').### Additional Information
- **getEndpoint()**: Returns the API endpoint URL.
- **prepareMessages()**: Prepares the messages for the API request.
- **getHeaders()**: Returns the headers for the API request.
- **getRequestBody()**: Returns the request body for the API request.