dragonzap / openai-ChatGPT-assistant
为ChatGPT助手API提供清晰的抽象。使用简洁易用的设计构建和运行助手
v2.1.1
2024-07-02 07:13 UTC
Requires
- laravel/framework: ^9.0|^10.0
- nyholm/psr7: ^1.8
- openai-php/client: ^0.10.0
- symfony/http-client: ^7.0
README
为ChatGPT助手API提供清晰的抽象层。使用简洁易用的设计构建和运行助手,兼容原生PHP和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