sgraaf / ChatGPT-php
OpenAI ChatGPT API 的便捷 PHP 封装。
0.1.0
2023-03-07 10:42 UTC
Requires
- sgraaf/openai-php: ^0.1.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.14
- phpstan/phpstan: ^1.9
This package is auto-updated.
Last update: 2024-09-03 23:17:58 UTC
README
OpenAI ChatGPT API 的便捷 PHP 封装。
安装
您可以通过 Composer 安装 ChatGPT PHP。
composer require sgraaf/chatgpt-php
使用
初始化客户端
// initialize the client $client = new ChatGPT\Client('YOUR_OPENAI_API_KEY');
使用自定义 系统消息
// initialize the client $client = new ChatGPT\Client('YOUR_OPENAI_API_KEY', system_message: 'You are a helpful assistant that translates English to French.');
开始对话
// provide an instruction $message = $client->chat('Who won the world series in 2020?'); var_dump($message); // string(53) "The Los Angeles Dodgers won the World Series in 2020." // continue the conversation $message = $client->chat('Where was it played?'); var_dump($message); // string(116) "The 2020 World Series was played in Arlington, Texas at the Globe Life Field, the home stadium of the Texas Rangers."
查看对话历史
var_dump($client->messages); // array(5) { // [0]=> // array(2) { // ["role"]=> // string(6) "system" // ["content"]=> // string(28) "You are a helpful assistant." // } // [1]=> // array(2) { // ["role"]=> // string(4) "user" // ["content"]=> // string(33) "Who won the world series in 2020?" // } // [2]=> // array(2) { // ["role"]=> // string(9) "assistant" // ["content"]=> // string(53) "The Los Angeles Dodgers won the World Series in 2020." // } // [3]=> // array(2) { // ["role"]=> // string(4) "user" // ["content"]=> // string(20) "Where was it played?" // } // [4]=> // array(2) { // ["role"]=> // string(9) "assistant" // ["content"]=> // string(116) "The 2020 World Series was played in Arlington, Texas at the Globe Life Field, the home stadium of the Texas Rangers." // } // }
清除对话历史
// clear the conversation history $client->clear(); var_dump($client->messages); // array(1) { // [0]=> // array(2) { // ["role"]=> // string(6) "system" // ["content"]=> // string(28) "You are a helpful assistant." // } // }