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\AzureOpenAI;
// Reads from AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT,
// AZURE_OPENAI_MODEL_DEPLOYMENT, and AZURE_OPENAI_API_VERSION
$client = AzureOpenAI::client();
$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.',
]);