使用gateway-worker 作為websocket。 當(dāng)worker進(jìn)程里面往GPT(使用的是webman/openai擴(kuò)展),讓GPT按SSE返回?cái)?shù)據(jù),在接收GPT返回?cái)?shù)據(jù)過程當(dāng)中,用戶通過websocket發(fā)送數(shù)據(jù)過來,網(wǎng)關(guān)端可以檢測到數(shù)據(jù)已接收,但是無法給到worker進(jìn)程。直到worker進(jìn)程接收完GPT返回的數(shù)據(jù)之后,此時(shí),worker進(jìn)程才接收到用戶發(fā)送過來的數(shù)據(jù)
$body = [
'stream' => true,
'model' => $model,
'temperature' => $temperature,
'messages' => $messages,
'response_format' => $this->testResponseJson(),
];
$chat = new Chat(['apikey' => $this->openApiKey, 'api' => static::BASE_URI]);
$audioBusinessService = AudioBusinessMap::getInstance();
$audioBusinessService->setUid($uid);
Logger::info(__METHOD__, $messages);
$lastConversationId = $audioBusinessService->getConversationId();
$chat->completions($body, [
'stream' => function ($data) use ($audioBusinessService, $uid, $lastConversationId) {
static $test = new GptOutStreamDealService('');
// 當(dāng)openai接口返回?cái)?shù)據(jù)時(shí)轉(zhuǎn)發(fā)給瀏覽器
$tmpStr = $data['choices'][0]['delta']['content'] ?? '';
$conversationId = $audioBusinessService->getConversationId();
if ($conversationId == $lastConversationId) {
StoryGamePlayV3Service::streamChat($test, $audioBusinessService, $uid, $tmpStr);
}
},
'complete' => function ($result) use ($historyMap, $audioBusinessService, $uid, $lastConversationId) {
// 響應(yīng)結(jié)束時(shí)檢查是否有錯(cuò)誤
$conversationId = $audioBusinessService->getConversationId();
if ($conversationId == $lastConversationId) {
StoryGamePlayV3Service::endChat($result, $historyMap, $audioBusinessService, $uid);
}
},
]);
public static function streamChat(GptOutStreamDealService $test, AudioBusinessMap $audioBusinessService, int $uid, string $tmpStr): void
{
$c = 'Card';
$m = 'coin';
if (empty($test->getText())) {
Log::info(microtime(true) . '大模型第一次吐出詞');
}
}