PHP code example of sage-grids / php-ai-sdk

1. Go to this page and download the library: Download sage-grids/php-ai-sdk 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/ */

    

sage-grids / php-ai-sdk example snippets


use SageGrids\PhpAiSdk\AI;
use SageGrids\PhpAiSdk\Provider\OpenAI\OpenAIProvider;
use SageGrids\PhpAiSdk\Provider\OpenAI\OpenAIConfig;

// Create a provider
$provider = new OpenAIProvider(
    new OpenAIConfig(apiKey: 'your-api-key'),
);

// Generate text
$result = AI::generateText([
    'model' => $provider,
    'prompt' => 'What is the meaning of life?',
]);

echo $result->text;

use SageGrids\PhpAiSdk\AI;

$result = AI::generateText([
    'model' => 'openai/gpt-4o',
    'prompt' => 'Explain quantum computing in simple terms.',
    'maxTokens' => 500,
    'temperature' => 0.7,
]);

echo $result->text;
echo "Tokens used: " . $result->usage->totalTokens;

use SageGrids\PhpAiSdk\AI;
use SageGrids\PhpAiSdk\Core\Message\UserMessage;
use SageGrids\PhpAiSdk\Core\Message\AssistantMessage;

$result = AI::generateText([
    'model' => 'openai/gpt-4o',
    'system' => 'You are a helpful assistant.',
    'messages' => [
        new UserMessage('What is 2+2?'),
        new AssistantMessage('2+2 equals 4.'),
        new UserMessage('And what is that multiplied by 3?'),
    ],
]);

use SageGrids\PhpAiSdk\AI;

foreach (AI::streamText([
    'model' => 'openai/gpt-4o',
    'prompt' => 'Write a short story about a robot.',
]) as $chunk) {
    echo $chunk->delta; // Print each chunk as it arrives
    flush();
}

$generator = AI::streamText([
    'model' => 'openai/gpt-4o',
    'prompt' => 'Tell me a joke.',
    'onChunk' => function ($chunk) {
        // Called for each chunk
        echo $chunk->delta;
    },
    'onFinish' => function ($finalChunk) {
        // Called when streaming completes
        echo "\n\nTotal tokens: " . $finalChunk->usage?->totalTokens;
    },
]);

// Consume the generator
iterator_to_array($generator);

use SageGrids\PhpAiSdk\AI;
use SageGrids\PhpAiSdk\Core\Schema\Schema;

$result = AI::generateObject([
    'model' => 'openai/gpt-4o',
    'prompt' => 'Generate a user profile for John Doe',
    'schema' => Schema::object([
        'name' => Schema::string()->description('Full name'),
        'age' => Schema::integer()->description('Age in years'),
        'email' => Schema::string()->description('Email address'),
        'interests' => Schema::array(Schema::string())->description('List of hobbies'),
    ]),
]);

// $result->object is validated against the schema
echo $result->object['name'];     // "John Doe"
echo $result->object['age'];      // 30
print_r($result->object['interests']); // ["reading", "coding"]

class UserProfile
{
    public string $name;
    public int $age;
    public string $email;
    /** @var string[] */
    public array $interests;
}

$result = AI::generateObject([
    'model' => 'openai/gpt-4o',
    'prompt' => 'Generate a user profile',
    'schema' => UserProfile::class,
]);

use SageGrids\PhpAiSdk\AI;
use SageGrids\PhpAiSdk\Core\Schema\Schema;
use SageGrids\PhpAiSdk\Core\Tool\Tool;

$weatherTool = Tool::create(
    name: 'get_weather',
    description: 'Get the current weather for a location',
    parameters: Schema::object([
        'city' => Schema::string()->description('City name'),
        'unit' => Schema::string()
            ->enum(['celsius', 'fahrenheit'])
            ->default('celsius'),
    ]),
    execute: function (array $args) {
        // Your weather API logic here
        return json_encode([
            'city' => $args['city'],
            'temperature' => 22,
            'unit' => $args['unit'],
            'condition' => 'sunny',
        ]);
    },
);

$result = AI::generateText([
    'model' => 'openai/gpt-4o',
    'prompt' => 'What is the weather in Paris?',
    'tools' => [$weatherTool],
]);

echo $result->text; // "The weather in Paris is 22°C and sunny."

use SageGrids\PhpAiSdk\Provider\OpenAI\OpenAIProvider;

$provider = new OpenAIProvider($config);

// Single text
$result = $provider->embed('Hello, world!');
$vector = $result->first()->embedding; // float[]

// Multiple texts
$result = $provider->embed([
    'The quick brown fox',
    'jumps over the lazy dog',
]);

// Calculate similarity
$similarity = $result->get(0)->cosineSimilarity($result->get(1));

use SageGrids\PhpAiSdk\Provider\OpenAI\OpenAIProvider;

$provider = new OpenAIProvider($config);

$result = $provider->generateImage(
    prompt: 'A serene mountain landscape at sunset',
    size: '1024x1024',
    quality: 'hd',
);

$imageUrl = $result->first()->url;

$result = $provider->generateSpeech(
    text: 'Hello, welcome to our application!',
    voice: 'alloy',
    responseFormat: 'mp3',
);

$result->saveTo('welcome.mp3');

$result = $provider->transcribe(
    audio: '/path/to/audio.mp3',
    language: 'en',
);

echo $result->text;

use SageGrids\PhpAiSdk\Provider\OpenAI\OpenAIProvider;
use SageGrids\PhpAiSdk\Provider\OpenAI\OpenAIConfig;

$provider = new OpenAIProvider(
    new OpenAIConfig(
        apiKey: getenv('OPENAI_API_KEY'),
        organization: 'org-xxx', // Optional
        defaultModel: 'gpt-4o',
    ),
);

use SageGrids\PhpAiSdk\Provider\ProviderRegistry;
use SageGrids\PhpAiSdk\AI;

// Register the provider
ProviderRegistry::getInstance()->register('openai', $provider);

// Use with model string
$result = AI::generateText([
    'model' => 'openai/gpt-4o',
    'prompt' => 'Hello!',
]);

use SageGrids\PhpAiSdk\AIConfig;

// Set default provider
AIConfig::setProvider('openai/gpt-4o');

// Set default parameters
AIConfig::setDefaults([
    'temperature' => 0.7,
    'maxTokens' => 1000,
]);

// Now you can omit the model parameter
$result = AI::generateText([
    'prompt' => 'Hello!',
]);

use PHPUnit\Framework\TestCase;
use SageGrids\PhpAiSdk\Testing\AITestCase;
use SageGrids\PhpAiSdk\Testing\FakeProvider;
use SageGrids\PhpAiSdk\Testing\FakeResponse;
use SageGrids\PhpAiSdk\AI;

class MyFeatureTest extends TestCase
{
    use AITestCase;

    protected function setUp(): void
    {
        $this->setUpAI();
    }

    protected function tearDown(): void
    {
        $this->tearDownAI();
    }

    public function testGeneratesGreeting(): void
    {
        // Create fake provider with queued response
        $fake = $this->fake();
        $fake->addTextResponse('Hello, John!');

        // Call your code
        $result = AI::generateText([
            'model' => $fake,
            'prompt' => 'Greet John',
        ]);

        // Assert the response
        $this->assertAIGenerated($result, 'Hello, John!');

        // Assert the request was made correctly
        $this->assertAIRequestMade($fake, 'generateText');
        $this->assertAIRequestContains($fake, 'John');
    }

    public function testToolCalling(): void
    {
        $fake = $this->fake();

        // Queue a response with tool calls
        $fake->addResponse('generateText', FakeResponse::toolCalls([
            FakeResponse::toolCall('get_weather', ['city' => 'Paris']),
        ]));

        // Then queue the final response
        $fake->addTextResponse('The weather in Paris is sunny.');

        $result = AI::generateText([
            'model' => $fake,
            'prompt' => 'What is the weather in Paris?',
            'tools' => [$this->createWeatherTool()],
        ]);

        $this->assertAIGenerated($result, 'The weather in Paris is sunny.');
    }

    public function testStreamingResponse(): void
    {
        $fake = $this->fake();
        $fake->addTextStreamResponse(['Hello', ', ', 'world', '!']);

        $chunks = [];
        foreach (AI::streamText([
            'model' => $fake,
            'prompt' => 'Say hello',
        ]) as $chunk) {
            $chunks[] = $chunk->delta;
        }

        $this->assertEquals(['Hello', ', ', 'world', '!'], $chunks);
    }
}

use SageGrids\PhpAiSdk\Testing\FakeResponse;

// Text responses
FakeResponse::text('Hello!');
FakeResponse::text('Hello!', FakeResponse::usage(10, 5));

// Tool calls
FakeResponse::toolCalls([
    FakeResponse::toolCall('get_weather', ['city' => 'Paris']),
]);

// Streaming chunks
FakeResponse::streamedText(['Hello', ' ', 'world']);

// Structured objects
FakeResponse::object(['name' => 'John', 'age' => 30]);
FakeResponse::streamedObject(
    finalObject: ['name' => 'John'],
    partialObjects: [['name' => 'Jo']],
);

// Embeddings
FakeResponse::embedding([0.1, 0.2, 0.3]);
FakeResponse::randomEmbedding(dimensions: 1536);

// Images
FakeResponse::image(url: 'https://example.com/image.png');

// Speech and transcription
FakeResponse::speech('audio-content');
FakeResponse::transcription('Hello, world!');

use SageGrids\PhpAiSdk\Exception\AuthenticationException;
use SageGrids\PhpAiSdk\Exception\RateLimitException;
use SageGrids\PhpAiSdk\Exception\InputValidationException;
use SageGrids\PhpAiSdk\Exception\ProviderException;

try {
    $result = AI::generateText([
        'model' => 'openai/gpt-4o',
        'prompt' => 'Hello',
    ]);
} catch (AuthenticationException $e) {
    // Invalid API key
} catch (RateLimitException $e) {
    // Rate limit exceeded - check $e->getRetryAfter()
} catch (InputValidationException $e) {
    // Invalid input parameters
} catch (ProviderException $e) {
    // Other provider errors
}
bash
git clone https://github.com/sage-grids/php-ai-sdk.git
cd php-ai-sdk
composer install
bash
composer cs-fix
# or
./vendor/bin/php-cs-fixer fix