dragonzap/openai-ChatGPT-assistant

为ChatGPT助手API提供清晰的抽象。使用简洁易用的设计构建和运行助手

v2.1.1 2024-07-02 07:13 UTC

This package is auto-updated.

Last update: 2024-10-02 08:38:37 UTC


README

为ChatGPT助手API提供清晰的抽象层。使用简洁易用的设计构建和运行助手,兼容原生PHP和Laravel框架。

Laravel框架安装文档

非Laravel框架安装文档

安装

要安装ChatGPT助手包装器,请使用Composer。在项目目录中运行以下命令

composer require dragonzap/openai-chatgpt-assistant

简单用法

// Replace the API Key with your own chatgpt API key
$assistant = new JessicaAssistant(new APIConfiguration('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'));
$conversation = $assistant->newConversation();

while(1)
{
    $input_message = fgets(STDIN);
    echo 'User:' . $input_message . "\n";
    $conversation->sendMessage($input_message);
    $conversation->blockUntilResponded();
    
    echo 'Assistant: ' . $conversation->getResponseData()->getResponse() . "\n";
    
}

JessicaAssistant将覆盖handleFunction()方法,该方法将在ChatGPT需要执行函数时被调用

 public function handleFunction(string $function, array $arguments): string|array
    {
        $response = [];

        switch($function)
        {
            case 'get_weather':
                $response = ['success' => true, 'message' => 'We will pretend its a sunny day where ever you live'];
                break;

            default:
                $response = [
                    'success' => false,
                    'message' => 'Unknown function'
                ];
        }
        return $response;
    }

handleFunction方法将以字符串和数组的形式返回消息发送回ChatGPT。有关Jessica Assistant的完整实现,请参阅:https://github.com/dragonzapeducation/ChatGPT-assistant-examples/blob/main/JustSimplePhp/src/console-chat-example.php