PHP code example of ikarolaborda / azure-openai-sdk-php

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

    

ikarolaborda / azure-openai-sdk-php example snippets


use IkaroLaborda\AzureOpenAI\Facades\AzureOpenAI;

$response = AzureOpenAI::chat()->create([
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!'],
    ],
]);

echo $response->choices[0]->message->content;

use OpenAI\Client;

$client = app(Client::class);

$response = $client->chat()->create([
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!'],
    ],
]);

use IkaroLaborda\AzureOpenAI\AzureOpenAI;

// Reads from AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT,
// AZURE_OPENAI_MODEL_DEPLOYMENT, and AZURE_OPENAI_API_VERSION
$client = AzureOpenAI::client();

$client = AzureOpenAI::client(
    apiKey: 'your-azure-api-key',
    endpoint: 'https://your-resource.openai.azure.com',
    deployment: 'gpt-4o',
    apiVersion: '2024-10-21',
);

$client = AzureOpenAI::factory()
    ->withApiKey('your-azure-api-key')
    ->withEndpoint('https://your-resource.openai.azure.com')
    ->withDeployment('gpt-4o')
    ->withApiVersion('2024-10-21')
    ->withHttpClient(new \GuzzleHttp\Client(['timeout' => 120]))
    ->make();

$client = AzureOpenAI::factory()
    ->withApiKey('your-azure-api-key')
    ->withResource('your-resource-name')     // builds https://your-resource-name.openai.azure.com
    ->withDeployment('gpt-4o')
    ->withApiVersion('2024-10-21')
    ->make();

$response = $client->chat()->create([
    'messages' => [
        ['role' => 'system', 'content' => 'You are a helpful assistant.'],
        ['role' => 'user', 'content' => 'What is the capital of France?'],
    ],
]);

echo $response->choices[0]->message->content;

$response = $client->responses()->create([
    'input' => 'What is the meaning of life?',
]);

echo $response->outputText;

$response = $client->responses()->create([
    'input' => 'What was the latest news today?',
    'tools' => [
        ['type' => 'web_search_preview'],
    ],
]);

echo $response->outputText;

$stream = $client->responses()->createStreamed([
    'input' => 'Write a short poem about PHP.',
]);

foreach ($stream as $response) {
    if ($response->outputText) {
        echo $response->outputText;
    }
}

$stream = $client->chat()->createStreamed([
    'messages' => [
        ['role' => 'user', 'content' => 'Write a short poem about PHP.'],
    ],
]);

foreach ($stream as $response) {
    echo $response->choices[0]->delta->content;
}

$response = $client->embeddings()->create([
    'input' => 'The quick brown fox jumps over the lazy dog.',
]);

$vector = $response->embeddings[0]->embedding; // array of floats

use IkaroLaborda\AzureOpenAI\AzureOpenAI;

// Both clients share the API key and endpoint from env;
// only the deployment differs.
$chatClient = AzureOpenAI::client(deployment: 'gpt-4o');
$embeddingClient = AzureOpenAI::client(deployment: 'text-embedding-3-small');

$chat = $chatClient->chat()->create([
    'messages' => [['role' => 'user', 'content' => 'Summarize this document.']],
]);

$embedding = $embeddingClient->embeddings()->create([
    'input' => 'The quick brown fox jumps over the lazy dog.',
]);

$client = AzureOpenAI::factory()
    ->withApiKey('your-azure-api-key')
    ->withEndpoint('https://your-resource.openai.azure.com')
    ->withApiVersion('2024-10-21')
    ->make();

$models = $client->models()->list();

$client = AzureOpenAI::factory()
    ->withApiKey('your-azure-api-key')
    ->withEndpoint('https://your-resource.openai.azure.com')
    ->withDeployment('gpt-4o')
    ->withApiVersion('2024-10-21')
    ->make();

$client = AzureOpenAI::factory()
    ->withAzureAdToken($token)
    ->withEndpoint('https://your-resource.openai.azure.com')
    ->withDeployment('gpt-4o')
    ->withApiVersion('2024-10-21')
    ->make();

$client = AzureOpenAI::factory()
    ->withAzureAdTokenProvider(function () use ($credential) {
        $token = $credential->getToken('https://cognitiveservices.azure.com/.default');

        return $token->token;
    })
    ->withEndpoint('https://your-resource.openai.azure.com')
    ->withDeployment('gpt-4o')
    ->withApiVersion('2024-10-21')
    ->make();

use IkaroLaborda\AzureOpenAI\Enums\AzureApiVersion;

$client = AzureOpenAI::factory()
    ->withApiKey('your-azure-api-key')
    ->withEndpoint('https://your-resource.openai.azure.com')
    ->withDeployment('gpt-4o')
    ->withApiVersion(AzureApiVersion::V2024_10_21->value)
    ->make();
bash
php artisan vendor:publish --tag=azure-openai