PHP code example of iteks / laravel-openai

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

    

iteks / laravel-openai example snippets


use Iteks\Support\Facades\OpenAi;

// Example of a nAi::chat('Your message here');

// Example with optional parameters
$response = OpenAi::chat('Your message here', ['temperature' => 0.7, 'max_tokens' => 150]);

OpenAi::audio()->createSpeech('tts-1', 'The quick brown fox jumped over the lazy dog.', 'alloy');

OpenAi::audio()->createTranscription(fopen('@/path/to/file/audio.mp3', 'r'), 'whisper-1');

OpenAi::audio()->createTranslation('@/path/to/file/german.m4a', 'whisper-1');

OpenAi::chat()->create(
    [
        ['role' => 'system', 'content' => 'You are a helpful assistant.',],
        [ 'role' => 'user', 'content' => 'Hello!', ],
    ],
    'gpt-4o'
);

OpenAi::embeddings()->create(
    'The food was delicious and the waiter...',
    'text-embedding-ada-002',
    ['encoding_format' => 'float']
);

OpenAi::fineTuning()->create('gpt-3.5-turbo', 'file-BK7bzQj3FfZFXr7DbL6xJwfo');

OpenAi::fineTuning()->list();

OpenAi::fineTuning()->listEvents('ftjob-abc123');

OpenAi::fineTuning()->listCheckpoints('ftjob-abc123');

OpenAi::fineTuning()->retrieve('ft-AF1WoRqd3aJAHsqc9NY7iL8F');

OpenAi::fineTuning()->cancel('ftjob-abc123');

OpenAi::batch()->create('file-abc123', '/v1/chat/completions', '24h');

OpenAi::batch()->retrieve('batch_abc123');

OpenAi::batch()->cancel('batch_abc123');

OpenAi::batch()->list();

OpenAi::files()->create(fopen('@mydata.jsonl', 'r'), 'fine-tune');

OpenAi::files()->list();

OpenAi::files()->retrieve('file-abc123');

OpenAi::files()->delete('file-abc123');

OpenAi::files()->retrieveContents('file-abc123');

OpenAi::images()->create(
    'A cute baby sea otter',
    [
        'model' => 'dall-e-3',
        'n' => 1,
        'size' => '1024x1024',
    ]
);

OpenAi::images()->createEdit(
    'fopen('@otter.png', 'r')',
    'A cute baby sea otter wearing a beret',
    [
        'mask' => '@mask.png',
        'n' => 2,
        'size' => '1024x1024',
    ]
);

OpenAi::images()->createVariation(
    'fopen('@otter.png', 'r')',
    [
        'n' => 2,
        'size' => '1024x1024',
    ]
);

OpenAi::models()->list();

OpenAi::models()->retrieve('gpt-3.5-turbo-instruct');

OpenAi::models()->delete('ft:gpt-3.5-turbo:acemeco:suffix:abc123');

OpenAi::moderations()->create('I want to kill them.');

OpenAi::assistants()->create(
    'gpt-4-turbo',
    [
        'instructions' => 'You are a personal math tutor. When asked a question, write and run Python code to answer the question.',
        'name' => 'Math Tutor',
        'tools' => [['type' => 'code_interpreter']],
    ]
);

OpenAi::assistants()->list();

OpenAi::assistants()->retrieve('asst_abc123');

OpenAi::assistants()->modify(
    'asst_idcmCPkquyQbqOpdJOEb6wCO',
    [
        'instructions' => 'You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.',
        'tools' => [['type' => 'file_search']],
        'model' => 'gpt-4-turbo',
    ]
);

OpenAi::assistants()->delete('asst_abc123');

OpenAi::threads()->create();

OpenAi::threads()->retrieve('thread_abc123');

OpenAi::threads()->modify(
    'thread_abc123',
    [
        'metadata' => [
            'modified' => 'true',
            'user' => 'abc123',
        ],
    ]
);

OpenAi::threads()->delete('thread_abc123');

OpenAi::messages()->create(
    'thread_abc123',
    'user',
    'How does AI work? Explain it in simple terms.'
);

OpenAi::messages()->list('thread_abc123');

OpenAi::messages()->retrieve('thread_abc123', 'msg_abc123');

OpenAi::messages()->modify(
    'thread_abc123',
    'msg_abc123',
    [
        'metadata' => [
            'modified' => 'true',
            'user' => 'abc123',
        ],
    ]
);

OpenAi::messages()->delete('thread_abc123', 'msg_abc123');

OpenAi::runs()->create('thread_abc123', 'asst_abc123');

OpenAi::runs()->createThreadAndRun(
    'asst_abc123',
    [
        'messages' => [
            [
                'role' => 'user',
                'content' => 'Explain deep learning to a 5 year old.',
            ],
        ],
    ]
);

OpenAi::runs()->list('thread_abc123');

OpenAi::runs()->retrieve('thread_abc123', 'run_abc123');

OpenAi::runs()->modify(
    'thread_abc123',
    'run_abc123',
    [
        'metadata' => [
            'user' => 'user_abc123',
        ],
    ]
);

OpenAi::runs()->submitToolOutputs(
    'thread_abc123',
    'run_abc123',
    [
        0 => [
            'tool_call_id' => 'call_001',
            'output' => '70 degrees and sunny.',
        ],
    ]
);

OpenAi::runs()->cancel('thread_abc123', 'run_abc123');

OpenAi::runSteps()->list('thread_abc123', 'run_abc123');

OpenAi::runSteps()->retrieve('thread_abc123', 'run_abc123', 'step_abc123');

OpenAi::vectorStores()->create(['name' => 'Support FAQ']);

OpenAi::vectorStores()->list();

OpenAi::vectorStores()->retrieve('vs_abc123');

OpenAi::vectorStores()->modify('vs_abc123', ['name' => 'Support FAQ']);

OpenAi::vectorStores()->delete('vs_abc123');

OpenAi::vectorStoreFiles()->create('vs_abc123', 'file-abc123');

OpenAi::vectorStoreFiles()->list('vs_abc123');

OpenAi::vectorStoreFiles()->retrieve('vs_abc123', 'file-abc123');

OpenAi::vectorStoreFiles()->delete('vs_abc123', 'file-abc123');

OpenAi::vectorStoreFileBatches()->create(
    'vs_abc123',
    [
        'file_ids' => [
            'file-abc123',
            'file-abc456',
        ],
    ]
);

OpenAi::vectorStoreFileBatches()->retrieve('vs_abc123', 'vsfb_abc123');

OpenAi::vectorStoreFileBatches()->cancel('vs_abc123', 'vsfb_abc123');

OpenAi::vectorStoreFileBatches()->list('vs_abc123', 'vsfb_abc123');

OpenAi::completions()->create(
    'gpt-3.5-turbo-instruct',
    'Say this is a test',
    [
        'max_tokens' => 7,
        'temperature' => 0,
    ]
);