PHP code example of outl1ne / nova-openai

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

    

outl1ne / nova-openai example snippets


// in app/Providers/NovaServiceProvider.php

public function tools()
{
    return [
        \Outl1ne\NovaOpenAI\NovaOpenAI::make(),
    ];
}

$assistant = OpenAI::assistants()->create(
  'gpt-3.5-turbo',
  'Allan\'s assistant',
  'For testing purposes of nova-openai package.',
  'You are a kindergarten teacher. When asked a questions, anwser shortly and as a young child could understand.'
);
$assistantModified = OpenAI::assistants()->modify($assistant->id, null, 'Allan\'s assistant!');
$deletedAssistant = OpenAI::assistants()->delete($assistant->id);
// dd($assistant->response->json(), $assistantModified->response->json(), $deletedAssistant->response->json());

$assistant = OpenAI::assistants()->create(
    'gpt-3.5-turbo',
    'Allan\'s assistant',
    'For testing purposes of nova-openai package.',
    'You are a kindergarten teacher. When asked a questions, anwser shortly and as a young child could understand.',
    [
        [
            'type' => 'retrieval',
        ],
    ],
);
$file = OpenAI::files()->upload(
    file_get_contents('files/file.txt'),
    'file.txt',
    'assistants',
);
$assistantFile = OpenAI::assistants()->files()->create($assistant->id, $file->id);
$assistantFiles = OpenAI::assistants()->files()->list($assistant->id);
$deletedAssistantFile = OpenAI::assistants()->files()->delete($assistant->id, $file->id);

// Cleanup
$deletedAssistant = OpenAI::assistants()->delete($assistant->id);
$deletedFile = OpenAI::files()->delete($file->id);
// dd(
//     $assistantFile->response->json(),
//     $assistantFiles->response->json(),
//     $deletedAssistantFile->response->json(),
// );

$response = OpenAI::chat()->create(
    model: 'gpt-3.5-turbo',
    messages: Messages::make()->system('You are a helpful assistant.')->user('Hello!'),
)->json();

$response = OpenAI::chat()->create(
    model: 'gpt-3.5-turbo',
    messages: Messages::make()->system('You are a helpful assistant.')->user('Suggest me tasty fruits as JSON array of fruits.'),
    responseFormat: ResponseFormat::make()->json(),
)->json();

$response = OpenAI::chat()->create(
    model: 'gpt-4o-mini',
    messages: Messages::make()->system('You are a helpful assistant.')->user('Suggest me 10 tasty fruits.'),
    responseFormat: ResponseFormat::make()->jsonSchema(
        JsonObject::make()
            ->property('fruits', JsonArray::make()->items(JsonString::make()))
            ->property('number_of_fruits_in_response', JsonInteger::make())
            ->property('number_of_fruits_in_response_divided_by_three', JsonNumber::make())
            ->property('is_number_of_fruits_in_response_even', JsonBoolean::make())
            ->property('fruit_most_occurring_color', JsonEnum::make()->enums(['red', 'green', 'blue']))
            ->property(
                'random_integer_or_string_max_one_character',
                JsonAnyOf::make()
                    ->schema(JsonInteger::make())
                    ->schema(JsonString::make())
            ),
    ),
)->json();

$response = OpenAI::chat()->create(
    model: 'gpt-4o-mini',
    messages: Messages::make()->system('You are a helpful assistant.')->user('Suggest me tasty fruits.'),
    responseFormat: ResponseFormat::make()->jsonSchema([
        'name' => 'response',
        'strict' => true,
        'schema' => [
            'type' => 'object',
            'properties' => [
                'fruits' => [
                    'type' => 'array',
                    'items' => [
                        'type' => 'string',
                    ],
                ],
            ],
            'additionalProperties' => false,
            '

$response = OpenAI::chat()->stream(function (string $newChunk, string $message) {
    echo $newChunk;
})->create(
    model: 'gpt-3.5-turbo',
    messages: Messages::make()->system('You are a helpful assistant.')->user('Hello!'),
);

$response = OpenAI::embeddings()->create(
    'text-embedding-3-small',
    'The food was delicious and the waiter...'
);
// dd($response->embedding);

$response = OpenAI::embeddings()->storing(function ($model) {
    $model->output = null;
    return $model;
})->create(
    'text-embedding-3-small',
    'The food was delicious and the waiter...'
);

$file = OpenAI::files()->upload(
    file_get_contents('files/file.txt'),
    'file.txt',
    'assistants',
);
$files = OpenAI::files()->list();
$file2 = OpenAI::files()->retrieve($file->id);
$deletedFile = OpenAI::files()->delete($file->id);
// dd($file->response->json(), $file2->response->json(), $deletedFile->response->json());

$fileContent = OpenAI::files()->retrieveContent($file->id);

$filePath = __DIR__ . '/../test.txt';
$file = OpenAI::files()->upload(
    file_get_contents($filePath),
    basename($filePath),
    'assistants',
);

$vectorStore = OpenAI::vectorStores()->create([$file->id]);
$vectorStores = OpenAI::vectorStores()->list();
$vectorStoreRetrieved = OpenAI::vectorStores()->retrieve($vectorStore->id);
$vectorStoreModified = OpenAI::vectorStores()->modify($vectorStore->id, 'Modified vector store');
$vectorStoreDeleted = OpenAI::vectorStores()->delete($vectorStore->id);

$assistant = OpenAI::assistants()->create(
    'gpt-3.5-turbo',
    'Allan',
    'nova-openai testimiseks',
    'You are a kindergarten teacher. When asked a questions, anwser shortly and as a young child could understand.'
);
$thread = OpenAI::threads()
    ->create(Messages::make()->user('What is your purpose in one short sentence?'));
$message = OpenAI::threads()->messages()
    ->create($thread->id, ThreadMessage::user('How does AI work? Explain it in simple terms in one sentence.'));
$response = OpenAI::threads()->run()->execute($thread->id, $assistant->id)->wait()->json();

// cleanup
$deletedThread = OpenAI::threads()->delete($thread->id);
$deletedAssistant = OpenAI::assistants()->delete($assistant->id);
// dd(
//     $assistant->response->json(),
//     $thread->response->json(),
//     $message->response->json(),
//     $run->response->json(),
//     $messages->response->json(),
//     $deletedThread->response->json(),
//     $deletedAssistant->response->json(),
// );

$mockOpenAIChat = Mockery::mock(Chat::class);
$mockOpenAIChat->shouldReceive('create')->andReturn((object) [
    'choices' => [
        [
            'message' => [
                'content' => 'Mocked response'
            ]
        ]
    ],
    'request' => OpenAIRequest::factory()->create()
]);
OpenAI::shouldReceive('chat')->andReturn($mockOpenAIChat);