PHP code example of erdum / php-open-ai-assistant-sdk

1. Go to this page and download the library: Download erdum/php-open-ai-assistant-sdk 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/ */

    

erdum / php-open-ai-assistant-sdk example snippets




rdum\OpenAIAssistant;

$openai = new OpenAIAssistant(
    $api_key,
    $assistant_id
);



ire(__DIR__ . '/php-open-ai-assistant-sdk/src/OpenAIAssistant.php');

use Erdum\OpenAIAssistant;

$openai = new OpenAIAssistant(
    $api_key,
    $assistant_id
);

$thread_id = isset($_SESSION[$session_id]) ? $_SESSION[$session_id] : null;

if (empty($thread_id)) {
    $thread_id = $openai->create_thread($query);
    $_SESSION[$session_id] = $thread_id;
} else {
    $openai->add_message($thread_id, 'Can you help me?');
}
$openai->run_thread($thread_id);

while ($openai->has_tool_calls) {
    $outputs = $openai->execute_tools(
        $thread_id,
        $openai->tool_call_id
    );
    $openai->submit_tool_outputs(
        $thread_id,
        $openai->tool_call_id,
        $outputs
    );
}

// Get the last recent message
$message = $openai->list_thread_messages($thread_id);
$message = $message[0];
$output = '';

if ($message['role'] == 'assistant') {
    foreach ($message['content'] as $msg) {
        $output .= "{$msg['text']['value']}\n";
    }
    exit($output);
}



// It will call the function directly whatever you have provided to the Assistant
$outputs = $openai->execute_tools(
    $thread_id,
    $openai->tool_call_id
);

// This will call methods on an instance of a class
$myObj = new MyAPI();
$outputs = $openai->execute_tools(
    $thread_id,
    $openai->tool_call_id,
    $myObj
);

// This will call static methods of a class
$outputs = $openai->execute_tools(
    $thread_id,
    $openai->tool_call_id,
    'MyAPI'
);

$openai->submit_tool_outputs(
    $thread_id,
    $openai->tool_call_id,
    $outputs
);



ire(__DIR__ . '/php-open-ai-assistant-sdk/src/OpenAIAssistant.php');

use Erdum\OpenAIAssistant;

$openai = new OpenAIAssistant($api_key);

$openai->create_assistant(
    'Customer Support Assistant',
    'You are a customer support assistant of an Telecom company. which is a wholesale DID numbers marketplace. You have to greet the customers and ask them how you can help them then understand their query and do the > array(
                            'type' => 'string',
                            'description' => 'The order-id of the customer.'
                        )
                    ),
                    '