使用gateway-worker 作为websocket。 当worker进程里面往GPT(使用的是webman/openai扩展),让GPT按SSE返回数据,在接收GPT返回数据过程当中,用户通过websocket发送数据过来,网关端可以检测到数据已接收,但是无法给到worker进程。直到worker进程接收完GPT返回的数据之后,此时,worker进程才接收到用户发送过来的数据
$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('');
// 当openai接口返回数据时转发给浏览器
$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) {
// 响应结束时检查是否有错误
$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) . '大模型第一次吐出词');
}
}
通过描述看不出什么,一般是代码写的有问题
已上传代码