PHP code example of cable8mm / nano-ai

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

    

cable8mm / nano-ai example snippets


use Cable8mm\NanoAI\Client;

// If API Key is omitted, it will be automatically read from the OPENAI_API_KEY / OPENROUTER_API_KEY environment variables.
$client = new Client(
  provider: 'openai',
  apiKey: <openai api key>,
  model: <model name>
);

echo $client->generate('Hello?');

// Images: supports URL, data URI, and local file path (auto base64 conversion)
echo $client->generate('Describe this photo', imageUrl: '/path/to/photo.jpg');

$client = new Client(
    provider: 'openai',
    apiKey: 'sk-...',
    model: 'gpt-4o',
    options: [
        'timeout' => 60,           // Request timeout in seconds (default: 120)
        'connectTimeout' => 10,    // Connection timeout in seconds (default: 30)
        'baseUrl' => 'https://custom-api.example.com',  // Override provider base URL
        'referer' => 'https://myapp.com',               // Custom referer header
        'title' => 'My App',                            // Custom title header
    ]
);

$client = new Client(
    provider: 'openrouter',
    apiKey: <openrouter api key>,
    model: 'deepseek/deepseek-chat:free', // Check the latest list at https://openrouter.ai/models?max_price=0
);

use Cable8mm\NanoAI\Client;
use Cable8mm\NanoAI\Http\GuzzleHttpClient;

// Use Guzzle with default timeout settings (30s / 10s connect)
$client = new Client(
    provider: 'openai',
    apiKey: 'sk-...',
    httpClient: new GuzzleHttpClient(),
);

// Or inject a pre-configured Guzzle client for full control
$guzzle = new \GuzzleHttp\Client([
    'timeout' => 60,
    'proxy' => 'http://localhost:8080',
]);
$client = new Client(
    provider: 'openai',
    apiKey: 'sk-...',
    httpClient: new GuzzleHttpClient($guzzle),
);