PHP code example of assistant-engine / php-openai-client

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

    

assistant-engine / php-openai-client example snippets


use AssistantEngine\OpenAI\Client;

$client = Client::make($_ENV['OPEN_AI_KEY']);

$response = $client->responses()->create([
    'model' => 'gpt-4o',
    'input' => "Can you recommend me what I can eat today?"
]);

print_r($response->getOutputText());

$response2 = $client->responses()->create([
    'model' => 'gpt-4o',
    'previous_response_id' => $response->id,
    'input' => "maybe Pizza?",
]);

print_r($response2->getOutputText());


use AssistantEngine\OpenAI\Client;

$client = Client::make($_ENV['OPEN_AI_KEY']);

$response = $client->responses()->create([
    'model' => 'gpt-4o',
    'tools' => [
        [
            'type' => 'file_search',
            "vector_store_ids" => ["vs_123456789"]
        ]
    ],
    'input' => "when is the event in berlin happening?"
]);

print_r($response->getOutputText());

use AssistantEngine\OpenAI\Client;

$client = Client::make($_ENV['OPEN_AI_KEY']);

$response = $client->responses()->create([
    'model' => 'gpt-4o',
    'tools' => [
        [
            'type' => 'web_search_preview'
        ]
    ],
    'input' => "what was a positive news story from today?"
]);

print_r($response->getOutputText());

use AssistantEngine\OpenAI\Client;

$client = Client::make($_ENV['OPEN_AI_KEY']);

// Path to your image file
$imageFilePath = './path/to/browser_screenshot.png';

// Read the image file contents
$imageData = file_get_contents($imageFilePath);

// Base64 encode the image data
$base64Image = base64_encode($imageData);

$response = $client->responses()->create([
    'model' => 'computer-use-preview',
    'tools' => [
        [
            'type' => 'computer_use_preview',
            "display_width" => 1024,
            "display_height" => 768,
            "environment" => "browser"
        ]
    ],
    'input' => [
        [
            "role" => "user",
            "content" => [
                [
                    "type" => "input_text",
                    "text" => "Can you find me products in sale?"
                ],
                [
                    "type" => "input_image",
                    "image_url" => "data:image/png;base64," . $base64Image
                ]
            ]
        ],

    ],
    "truncation" => "auto"
]);

print_r($response);

use AssistantEngine\OpenAI\Client;

$client = Client::make($_ENV['OPEN_AI_KEY']);
// Define query parameters for pagination and ordering
$params = [
    'after'  => 'item-id-to-start-after',  // Optional: list items after this ID
    'limit'  => 50,                        // Optional: limit number of items (default is 20)
    'order'  => 'desc',                    // Optional: order items in descending order
];

$respId = "resp_123456789";
$list = $client->responses()->inputItems()->list($respId, $params);

print_r($list);