|
|
@@ -460,4 +460,82 @@ class DeepSeekController extends BaseController
|
|
|
'Access-Control-Allow-Credentials' => 'true'
|
|
|
]);
|
|
|
}
|
|
|
+
|
|
|
+ public function chat(Request $request) {
|
|
|
+ // 忽略所有超时限制
|
|
|
+ set_time_limit(0);
|
|
|
+ ini_set('max_execution_time', '0');
|
|
|
+
|
|
|
+ $data = $request->all();
|
|
|
+ return response()->stream(function () use ($data) {
|
|
|
+ // 禁用所有输出缓冲
|
|
|
+ if (ob_get_level()) {
|
|
|
+ ob_end_clean();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置输出缓冲为关闭
|
|
|
+ ini_set('output_buffering', 'off');
|
|
|
+ ini_set('zlib.output_compression', 'off');
|
|
|
+
|
|
|
+ // 立即刷新
|
|
|
+ if (function_exists('apache_setenv')) {
|
|
|
+ apache_setenv('no-gzip', '1');
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $generator = $this->deepseekService->chat($data);
|
|
|
+
|
|
|
+ foreach ($generator as $chunk) {
|
|
|
+ // 发送 SSE 格式的数据
|
|
|
+ $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
|
|
|
+ echo "data: {$jsonData}\n\n";
|
|
|
+
|
|
|
+ // 强制刷新输出缓冲区
|
|
|
+ if (ob_get_level() > 0) {
|
|
|
+ ob_flush();
|
|
|
+ }
|
|
|
+ flush();
|
|
|
+
|
|
|
+ // 检查客户端是否断开连接
|
|
|
+ if (connection_aborted()) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送结束标记
|
|
|
+ echo "data: [DONE]\n\n";
|
|
|
+ if (ob_get_level() > 0) {
|
|
|
+ ob_flush();
|
|
|
+ }
|
|
|
+ flush();
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ // 发送错误信息
|
|
|
+ $error = [
|
|
|
+ 'type' => 'error',
|
|
|
+ 'message' => $e->getMessage(),
|
|
|
+ 'code' => $e->getCode()
|
|
|
+ ];
|
|
|
+ $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
|
|
|
+ echo "data: {$jsonError}\n\n";
|
|
|
+ if (ob_get_level() > 0) {
|
|
|
+ ob_flush();
|
|
|
+ }
|
|
|
+ flush();
|
|
|
+ }
|
|
|
+ }, 200, [
|
|
|
+ 'Content-Type' => 'text/event-stream',
|
|
|
+ 'Cache-Control' => 'no-cache',
|
|
|
+ 'Connection' => 'keep-alive',
|
|
|
+ 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
|
|
|
+ 'Access-Control-Allow-Origin' => '*',
|
|
|
+ 'Access-Control-Allow-Credentials' => 'true'
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testChat(Request $request) {
|
|
|
+ $data = $request->all();
|
|
|
+ $this->deepseekService->chat($data);
|
|
|
+ return $this->success(true);
|
|
|
+ }
|
|
|
}
|