erdum/php-open-ai-assistant-sdk

一个用于与 OpenAI 助理 API 无缝交互的 PHP 类,使开发者能够构建能够执行各种任务的功能强大的 AI 助理。

v1.0.0 2023-11-20 17:13 UTC

This package is auto-updated.

Last update: 2024-09-29 14:05:09 UTC


README

一个用于与 OpenAI 助理 API 无缝交互的 PHP 类,使开发者能够构建能够执行各种任务的功能强大的 AI 助理。

目录

安装

使用 composer 安装

composer require erdum/php-open-ai-assistant-sdk

或者在旧版 PHP 版本上不使用 composer 安装,只需在您的工作目录中克隆项目并包含 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
);

使用方法

目前,此使用示例不包含有关 OpenAI 助理 API 工作原理及其生命周期的任何信息。为了更好地理解 OpenAI 助理 API 的工作原理和生命周期,请参阅OpenAI 文档并查看参考以查看所有可用方法。

<?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);
}

PHP 函数的调用方式

你提供的工具数组中的任何函数名都将由助理 API 在你的 PHP 环境中相应地调用。例如,在下面的代码中,函数 "get_account_balance" 应该存在于你的 PHP 环境中,以便执行。如果你要运行对象的或类的静态方法,你可以向 "execute_tools" 方法提供一个额外的参数。

<?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
);

创建一个新的助理

<?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')
                ),
            )
        )
    )
);

参考

构造函数

create_assistant

modify_assistant

list_assistants

无参数。

create_thread

get_thread

add_message

get_message

list_thread_messages

run_thread

execute_tools

submit_tool_outputs

反馈

如果您有任何反馈,请通过erdumadnan@gmail.com与我们联系。

许可证

MIT License