1. Go to this page and download the library: Download limingxinleo/coze-php-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
limingxinleo / coze-php-sdk example snippets
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Coze\ChatTask;
use Coze\ChatTaskRunner;
use Coze\Client;
use Coze\Message\DetailMessages;
use Coze\Message\RetrieveMessage;
use Coze\Token;
('123123', '1234', [
[
'content' => '请给我一份上海嘉定的房源数据',
'content_type' => 'text',
'role' => 'user',
'type' => 'question',
],
]);
$task2 = ChatTask::fromRetrieveMessage($res, 2);
// 开发者需要重新实现 ChatTaskRunner,这里直接创建两个 Task 来进行测试
$runner = new class($client, [$task1, $task2]) extends ChatTaskRunner {
public function __construct(Client $client, public array $tasks)
{
parent::__construct($client);
}
public function scroll(int $id): array
{
// 这里根据实际情况,从数据库表里读取未完成的Task列表
if ($id === 0) {
return $this->tasks;
}
return [];
}
public function execute(ChatTask $task): void
{
var_dump('执行 ' . $task->chatId);
parent::execute($task);
}
public function save(ChatTask $task, RetrieveMessage $retrieved, DetailMessages $messages): bool
{
// 这里需要开发者根据 $task->id 保存对应数据,避免下次调度任务重复执行已经完成的对话
var_dump('执行完毕 ' . $task->chatId, $task->isCompleted);
var_dump($messages->withRawData());
return true;
}
};
// 这里通过 while(true) 模拟定时任务,开发者可以直接使用定时任务,每分钟跑一次
while (true) {
sleep(1);
$runner->run();
if ($task1->isCompleted && $task2->isCompleted) {
break;
}
}
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.