A PHP class for seamless interaction with the OpenAI Assistant API, enabling developers to build powerful AI assistants capable of performing a variety of tasks.
Install with composer
composer require erdum/php-open-ai-assistant-sdk
Or install without composer on older PHP versions just clone the project in your working directory and require the OpenAIAssistant.php
git clone https://github.com/erdum/php-open-ai-assistant-sdk.git
<?php
require(__DIR__ . 'php-open-ai-assistant-sdk/src/OpenAIAssistant.php');
use Erdum\OpenAIAssistant;
$openai = new OpenAIAssistant(
$api_key,
$assistant_id
);
Currently, this usage example does not contain any information about OpenAI Assistant API working for a better understanding of how OpenAI Assistant API works and its lifecycle please refer to the OpenAI documentation and look inside the OpenAIAssistant.php file to see all the available methods.
<?php
require(__DIR__ . '/vendor/autoload.php');
# require(__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;
}
$message_id = $openai->create_message($thread_id, 'Can you help me?');
if ($message_id) $openai->run_thread($thread_id);
if ($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_messages($thread_id);
$message = $message[0];
$output = '';
if ($message['role'] == 'assistant') {
foreach ($message['content'] as $msg) {
$output .= "{$msg['text']['value']}\n";
}
exit($output);
}
<?php
use Erdum\OpenAIAssistant;
$openai = new OpenAIAssistant($api_key);
Parameter | Type | Description |
---|---|---|
api_key |
string |
Required. Your OpenAI API key |
assistant_id |
string |
Assistant ID (default = null) |
base_url |
string |
OpenAI API base URL (default = 'https://api.openai.com/v1') |
version_header |
string |
OpenAI API version header (default = 'OpenAI-Beta: assistants=v1' ) |
If you have any feedback, please reach out to us at erdumadnan@gmail.com