PHP code example of sanjarani / openai

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

    

sanjarani / openai example snippets


// In config/openai.php
    // ...
    /*
    |--------------------------------------------------------------------------
    | OpenAI Beta Headers
    |--------------------------------------------------------------------------
    |
    | Specify any OpenAI-Beta headers riable:
        env("OPENAI_BETA_ASSISTANTS_VERSION") ? "OpenAI-Beta" : null => 
            env("OPENAI_BETA_ASSISTANTS_VERSION") ? "assistants=" . env("OPENAI_BETA_ASSISTANTS_VERSION") : null,
        // Add other static or env-driven beta headers here:
        // "Another-Beta-Header" => "some-value",
    ]),
    // ...

use Sanjarani\OpenAI\Facades\OpenAI;

// Example: Using a custom base URL for a specific chat completion request
$customBaseUrl = "https://my-custom-openai-proxy.com/v1/";

$response = OpenAI::chat()->create([
    "model" => "gpt-4o",
    "messages" => [
        ["role" => "user", "content" => "Hello from a custom endpoint!"],
    ],
], $customBaseUrl); // Pass the custom base URL as the last argument

echo $response["choices"][0]["message"]["content"];

use Sanjarani\OpenAI\Facades\OpenAI;

$response = OpenAI::chat()->create([
    "model" => "gpt-4o",
    "messages" => [
        ["role" => "user", "content" => "Hello! What is the capital of France?"],
    ],
]);

echo $response["choices"][0]["message"]["content"]; // Paris

use Sanjarani\OpenAI\Facades\OpenAI;

OpenAI::chat()->stream([
    "model" => "gpt-4o",
    "messages" => [
        ["role" => "user", "content" => "Tell me a short story about a brave robot."],
    ],
], function ($chunk) {
    // $chunk is an array representing a part of the streamed response
    if (isset($chunk["choices"][0]["delta"]["content"])) {
        echo $chunk["choices"][0]["delta"]["content"];
    }
});

use Sanjarani\OpenAI\Facades\OpenAI;

// Single input
$response = OpenAI::embeddings()->create([
    "model" => "text-embedding-3-small",
    "input" => "The food was delicious and the waiter...",
]);

$embedding = $response["data"][0]["embedding"];

// Batch input with a custom base URL
$customUrl = "https://another-api.com/openai-compat/";
$response = OpenAI::embeddings()->createBatch([
    "The food was delicious and the waiter...",
    "The movie was amazing and the plot was thrilling!"
], "text-embedding-3-small", [], $customUrl);

foreach ($response["data"] as $embeddingData) {
    // $embeddingData["embedding"]
}

use Sanjarani\OpenAI\Facades\OpenAI;

$response = OpenAI::images()->create([
    "prompt" => "A futuristic cityscape with flying cars, digital art",
    "n" => 1,
    "size" => "1024x1024",
    "response_format" => "url", // or b64_json
]);

$imageUrl = $response["data"][0]["url"];

use Sanjarani\OpenAI\Facades\OpenAI;

$response = OpenAI::audio()->createTranscription([
    "file" => "/path/to/your/audio.mp3",
    "model" => "whisper-1",
]);

$transcribedText = $response["text"];

use Sanjarani\OpenAI\Facades\OpenAI;
use Illuminate\Support\Facades\Storage;

$audioContent = OpenAI::audio()->createSpeech([
    "model" => "tts-1",
    "input" => "Hello world! This is a test of the text-to-speech API.",
    "voice" => "alloy",
]);

Storage::disk("local")->put("speech.mp3", $audioContent);

use Sanjarani\OpenAI\Facades\OpenAI;

$assistant = OpenAI::assistants()->create([
    "name" => "Math Tutor",
    "instructions" => "You are a personal math tutor. Write and run code to answer math questions.",
    "tools" => [["type" => "code_interpreter"]],
    "model" => "gpt-4o"
]);
// ... (rest of the Assistants API flow as previously documented)

use Sanjarani\OpenAI\Facades\OpenAI;

// ... (Vector Store creation and Assistant setup as previously documented)
bash
php artisan vendor:publish --provider="Sanjarani\OpenAI\OpenAIServiceProvider" --tag="openai-config"