mathsgod/openai-chat
最新稳定版本:2.1.1
Composer 安装命令:
composer require mathsgod/openai-chat
包简介
OpenAI Chat completion wrapper. Support function calls and variable assignment.
README 文档
README
Installation
composer require mathsgod/openai-chat
Usage
To use the OpenAI chat, you need to create a new instance of the System class and pass the OpenAI API key as the first argument.
use OpenAI\Chat\System; $system = new System($_ENV['OPENAI_API_KEY']); echo $system->ask("Hello");
Add a tool
use OpenAI\Chat\Attributes\Tool; use OpenAI\Chat\Attributes\Parameter; #[Tool(description: 'Get the release date of iphone')] function getIPhoneReleaseDate(#[Parameter("model of the phone")] string $model) { return ["date" => "2022-09-14", "model" => $model]; } $system->addTool(Closure::fromCallable("getIPhoneReleaseDate")); echo $system->ask("When will iPhone 14 be released?");
Add a tool from a class method
class Controller { public $price = "$799"; #[Tool(description: 'Get the price of iphone')] public function getIPhonePrice(#[Parameter("model of the phone")] string $model) { return ["price" => $this->price, "model" => $model]; } } $system->addTool(Closure::fromCallable([new Controller(), "getIPhonePrice"])); echo $system->ask("What is the price and release date of iphone14?");
Get usage records
After run the code above, you can get the usage records
print_r($system->getUsages());
Streaming
$stream = $system->askAsStream("What is the price and release date of iphone14?"); $stream->on('data', function ($data) { echo $data; });
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-21