erdum/php-open-ai-assistant-sdk
Composer 安装命令:
composer require erdum/php-open-ai-assistant-sdk
包简介
A PHP class for seamless interaction with the OpenAI Assistant API, enabling developers build powerful AI assistants capable of performing a variety of tasks.
README 文档
README
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.
Table of Contents
Installation
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 );
Usage
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 the Reference 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; } 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); }
How PHP functions will be called
Whatever function names you provide in the tools array will be called in your PHP environment accordingly by the Assistant API, for example in the below code function "get_account_balance" should be present in your PHP environment in order to be executed. If you want to run methods of an object or static methods of a class then you can provide an additional argument to the "execute_tools" method
<?php // 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 );
Create a new Assistant
<?php require(__DIR__ . '/vendor/autoload.php'); # require(__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 required operation. The functions and tools may require order-id in the arguments but do not ask the customers to provide their order-id because order-id will be included automatically to function calls.', array( array( 'type' => 'function', 'function' => array( 'name' => 'get_account_balance', 'description' => 'This function retrieves the account balance of the customer with the provided order-id.', 'parameters' => array( 'type' => 'object', 'properties' => array( 'order_id' => array( 'type' => 'string', 'description' => 'The order-id of the customer.' ) ), 'required' => array('order_id') ), ) ) ) );
Reference
Constructor
| 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'). |
| Returns | void |
No return value. |
create_assistant
| Parameter | Type | Description |
|---|---|---|
$name |
string |
Name of the assistant. |
$instructions |
string |
Instructions for the assistant. |
$tools |
array |
Array of tools. |
| Returns | string |
ID of the created assistant. |
modify_assistant
| Parameter | Type | Description |
|---|---|---|
$name |
string |
Name of the assistant. |
$instructions |
string |
Instructions for the assistant. |
$tools |
array |
Array of tools. |
| Returns | string |
ID of the modified assistant. |
list_assistants
No parameters.
| Returns | array |
List of available assistants. |
|---|
create_thread
| Parameter | Type | Description |
|---|---|---|
$content |
string |
Content of the thread. |
$role |
string |
Role (default = 'user'). |
| Returns | string |
ID of the created thread. |
get_thread
| Parameter | Type | Description |
|---|---|---|
$thread_id |
string |
Thread ID. |
| Returns | array |
Details of the thread. |
add_message
| Parameter | Type | Description |
|---|---|---|
$thread_id |
string |
Thread ID. |
$content |
string |
Content of the message. |
$role |
string |
Role (default = 'user'). |
| Returns | string |
ID of the added message. |
get_message
| Parameter | Type | Description |
|---|---|---|
$thread_id |
string |
Thread ID. |
$message_id |
string |
Message ID. |
| Returns | array |
Details of the message. |
list_thread_messages
| Parameter | Type | Description |
|---|---|---|
$thread_id |
string |
Thread ID. |
| Returns | array |
List of messages in the thread. |
run_thread
| Parameter | Type | Description |
|---|---|---|
$thread_id |
string |
Thread ID. |
| Returns | string |
ID of the thread run. |
execute_tools
| Parameter | Type | Description |
|---|---|---|
$thread_id |
string |
Thread ID. |
$execution_id |
string |
Execution ID. |
$optional_object |
object |
Optional object. |
| Returns | array |
Outputs of executed tools. |
submit_tool_outputs
| Parameter | Type | Description |
|---|---|---|
$thread_id |
string |
Thread ID. |
$execution_id |
string |
Execution ID. |
$outputs |
array |
Tool outputs array. |
| Returns | string |
ID of the submitted tool outputs. |
Feedback
If you have any feedback, please reach out to us at erdumadnan@gmail.com
License
erdum/php-open-ai-assistant-sdk 适用场景与选型建议
erdum/php-open-ai-assistant-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.32k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2023 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「sdk」 「messaging」 「assistant」 「ai」 「conversation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 erdum/php-open-ai-assistant-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 erdum/php-open-ai-assistant-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 erdum/php-open-ai-assistant-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
PHP SDK for Mobile Message SMS API
Azure service bus Transport
Official PHP client for NATS messaging system
Laravel / Lumen package for Firebase Cloud Messaging
Ozioma for PHP library helps to make API calls to Ozioma Cloud Messaging and Communications Platform for birthday contact and sms newsletter subscription.
统计信息
- 总下载量: 3.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-17
