PHP code example of sysborg / chatgpt

1. Go to this page and download the library: Download sysborg/chatgpt 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/ */

    

sysborg / chatgpt example snippets


return [
    'api_key' => env('OPENAI_API_KEY'),
    'base_url' => env('OPENAI_BASE_URL', 'https://api.openai.com/v1'),
    'default_model' => env('OPENAI_DEFAULT_MODEL', 'gpt-3.5-turbo'),
    'timeout' => env('OPENAI_TIMEOUT', 60),
    'max_tokens' => env('OPENAI_MAX_TOKENS', 1000),
    'temperature' => env('OPENAI_TEMPERATURE', 0.7),
    'retry_attempts' => env('OPENAI_RETRY_ATTEMPTS', 2),
    'retry_delay' => env('OPENAI_RETRY_DELAY', 1),
    'rate_limit' => env('OPENAI_RATE_LIMIT', 60), // 0 = unlimited
];

use Sysborg\ChatGPT\Facades\ChatGPT;

$response = ChatGPT::chat('Hello, how are you?');
echo $response->getContent();

$messages = [
    ['role' => 'user', 'content' => 'What is the capital of Brazil?'],
    ['role' => 'assistant', 'content' => 'The capital of Brazil is Brasília.'],
    ['role' => 'user', 'content' => 'What is the population?']
];

$response = ChatGPT::chatWithHistory($messages);
echo $response->getContent();

$response = ChatGPT::completion('Once upon a time in a distant kingdom');
echo $response->getContent();

$response = ChatGPT::vision(
    'https://example.com/image.jpg',
    'Describe this image in detail'
);

// With base64 image
$base64 = base64_encode(file_get_contents('/path/to/image.jpg'));
$response = ChatGPT::visionFromBase64(
    $base64,
    'What do you see in this image?'
);

echo $response->getAnalysis();

$response = ChatGPT::setModel('gpt-4')
    ->setTemperature(0.9)
    ->setMaxTokens(2000)
    ->chat('Tell a creative story');

$response = ChatGPT::chat('Hi!');

echo $response->getContent();
echo $response->getModel();
echo $response->getTotalTokens();
echo $response->getFinishReason();

if ($response->isComplete()) {
    echo "Completed successfully";
}

if ($response->isTruncated()) {
    echo "Response was truncated";
}

$response = ChatGPT::vision($imageUrl, 'Analyze this image');

echo $response->getAnalysis();
print_r($response->getDetectedEntities());
echo $response->getSummary();

if ($response->hasSafetyConcerns()) {
    echo "This image may contain sensitive content";
}

use Sysborg\ChatGPT\Exceptions\ChatGPTException;
use Sysborg\ChatGPT\Exceptions\RateLimitException;

try {
    $response = ChatGPT::chat('Hi!');
} catch (RateLimitException $e) {
    echo "Rate limit exceeded. Retry in: " . $e->getRetryAfter() . " seconds";
} catch (ChatGPTException $e) {
    echo "API error: " . $e->getMessage();
    print_r($e->getContext());
}

$status = ChatGPT::getRateLimitStatus();
echo "Requests remaining: " . $status['remaining'];

ChatGPT::resetRateLimit(); // Useful for testing

$models = ChatGPT::getAvailableModels();
print_r($models);

[
    'gpt-3.5-turbo',
    'gpt-3.5-turbo-16k',
    'gpt-4',
    'gpt-4-turbo',
    'gpt-4-vision-preview',
    'gpt-4-32k'
]

$response = ChatGPT::chatWithHistory([
    ['role' => 'system', 'content' => 'You are a programming assistant.'],
    ['role' => 'user', 'content' => 'How do I create a REST API in Laravel?']
], [
    'model' => 'gpt-4',
    'temperature' => 0.3,
    'max_tokens' => 1500,
    'top_p' => 0.9
]);

$response = ChatGPT::vision($imageUrl, 'Analyze this image', [
    'model' => 'gpt-4-vision-preview',
    'detail' => 'high', // Options: low, high, auto
    'max_tokens' => 1000
]);
bash
php artisan vendor:publish --tag=chatgpt-config