DeepSeekController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. <?php
  2. namespace App\Http\Controllers\DeepSeek;
  3. use App\Transformer\DeepSeek\DeepSeekTransformer;
  4. use App\Facade\Site;
  5. use App\Consts\ErrorConst;
  6. use App\Exceptions\ApiException;
  7. use App\Libs\ApiResponse;
  8. use App\Libs\Utils;
  9. use App\Services\DeepSeek\DeepSeekService;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Routing\Controller as BaseController;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Redis;
  14. use Illuminate\Support\Facades\Validator;
  15. class DeepSeekController extends BaseController
  16. {
  17. use ApiResponse;
  18. protected $deepseekService;
  19. public function __construct(
  20. DeepSeekService $deepseekService
  21. ) {
  22. $this->deepseekService = $deepseekService;
  23. }
  24. /**
  25. * 可选供应商
  26. *
  27. * @param Request $request
  28. * @return mixed
  29. */
  30. public function chatWithReasoner(Request $request) {
  31. // 忽略所有超时限制
  32. set_time_limit(0);
  33. ini_set('max_execution_time', '0');
  34. $data = $request->all();
  35. $result = $this->deepseekService->chatWithReasoner($data);
  36. return $this->success($result);
  37. }
  38. /**
  39. * 音色列表
  40. *
  41. * @param Request $request
  42. * @return mixed
  43. */
  44. public function timbreList(Request $request) {
  45. $data = $request->all();
  46. $result = $this->deepseekService->timbreList($data);
  47. return $this->success($result);
  48. }
  49. // 重置段落音频状态
  50. public function resetParagraphAudio(Request $request) {
  51. $data = $request->all();
  52. $result = $this->deepseekService->resetParagraphAudio($data);
  53. return $this->success(['success'=>$result ? 1 : 0]);
  54. }
  55. // 保存段落音频
  56. public function saveParagraphAudio(Request $request) {
  57. $data = $request->all();
  58. $result = $this->deepseekService->saveParagraphAudio($data);
  59. return $this->success(['success'=>$result ? 1 : 0]);
  60. }
  61. // 保存段落音频
  62. public function insertAudioEffect(Request $request) {
  63. $data = $request->all();
  64. $result = $this->deepseekService->insertAudioEffect($data);
  65. return $this->success(['success'=>$result ? 1 : 0]);
  66. }
  67. // 保存段落音频
  68. public function insertBgm(Request $request) {
  69. $data = $request->all();
  70. $result = $this->deepseekService->insertBgm($data);
  71. return $this->success(['success'=>$result ? 1 : 0]);
  72. }
  73. /**
  74. * 获取段落视频
  75. */
  76. public function paragraphAudios(Request $request) {
  77. $data = $request->all();
  78. $result = $this->deepseekService->getParagraphAudios($data);
  79. return $this->success($result);
  80. }
  81. /**
  82. * 新增合成任务
  83. *
  84. * @param Request $request
  85. * @return mixed
  86. */
  87. public function addGenerateTask(Request $request) {
  88. $data = $request->all();
  89. $result = $this->deepseekService->addGenerateTask($data);
  90. return $this->success(['success'=>$result ? 1 : 0]);
  91. }
  92. // 获取火山临时token
  93. public function setStsToken(Request $request) {
  94. $data = $request->all();
  95. $result = $this->deepseekService->setStsToken($data);
  96. return $this->success($result);
  97. }
  98. public function sseLink(Request $request) {
  99. $data = $request->all();
  100. $bid = getProp($data, 'bid');
  101. $version_id = getProp($data, 'version_id');
  102. $cid = getProp($data, 'cid');
  103. // 禁用输出缓冲
  104. while (ob_get_level()) ob_end_clean();
  105. // 设置SSE所需的HTTP头
  106. header('Content-Type: text/event-stream');
  107. header('Cache-Control: no-cache');
  108. header('Connection: keep-alive');
  109. header('X-Accel-Buffering: no'); // 防止Nginx缓冲
  110. // 允许跨域请求(根据需求设置)
  111. header('Access-Control-Allow-Origin: *');
  112. header('Access-Control-Allow-Credentials: true');
  113. // 设置脚本执行时间无限
  114. set_time_limit(0);
  115. // 手动刷新输出缓冲区
  116. ob_implicit_flush(true);
  117. if(ob_get_level()>0) ob_flush();
  118. flush();
  119. // 客户端ID(用于多用户区分)
  120. $clientId = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER['REMOTE_PORT'];
  121. $lastEventId = isset($_SERVER['HTTP_LAST_EVENT_ID']) ? intval($_SERVER['HTTP_LAST_EVENT_ID']) : 0;
  122. // dd($clientId, $lastEventId);
  123. // 发送初始欢迎消息
  124. $this->sendEvent([
  125. // 'message' => "欢迎使用SSE服务! 你的客户端ID: {$clientId}",
  126. // 'type' => 'info',
  127. // 'timestamp' => date('H:i:s')
  128. 'code' => 0,
  129. 'msg' => "已连接! 你的客户端ID: {$clientId}",
  130. 'data' => [],
  131. ]);
  132. if (!$bid && !$version_id && !$cid) {
  133. $this->sendEvent([
  134. // 'message' => '参数异常',
  135. // 'type' => 'error',
  136. // 'timestamp' => date('H:i:s')
  137. 'code' => -1,
  138. 'msg' => "参数异常",
  139. 'data' => [],
  140. ]);
  141. }
  142. $redis_key = "select-{$bid}-{$version_id}-{$cid}";
  143. // 主循环 - 定期发送数据
  144. while (true) {
  145. // 检查客户端是否断开连接
  146. if (connection_aborted()) {
  147. exit();
  148. }
  149. // 判断是否有待更新数据,有则查询
  150. $ids = Redis::smembers($redis_key);
  151. if (count($ids) > 0) {
  152. $count = DB::table('mp_chapter_paragraph_audios')->whereIn('id', $ids)->where(function($query) {
  153. return $query->where('generate_status', '!=', '制作中')->orWhere('error_msg', '!=', '');
  154. })->count('id');
  155. // 如果有更新数据,则发送消息
  156. if ($count > 0) {
  157. // 查询更新后的信息
  158. $paragraph_urls = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)
  159. ->where('cid', $cid)->where(function($query) {
  160. return $query->where('generate_status', '!=', '制作中')->orWhere('error_msg', '!=', '');
  161. })->select('id', 'sequence', 'generate_status', 'paragraph_audio_url', 'error_msg')
  162. ->get()->map(function ($value) {
  163. return (array)$value;
  164. })->toArray();
  165. $rem_ids = [];
  166. foreach ($paragraph_urls as $item) {
  167. $rem_ids[] = getProp($item, 'id');
  168. }
  169. // 发送消息
  170. $this->sendEvent([
  171. 'code' => 0,
  172. 'msg' => "",
  173. 'data' => $paragraph_urls,
  174. // 'message' => '数据更新',
  175. // 'type' => 'update',
  176. // 'timestamp' => date('H:i:s')
  177. ]);
  178. // 删除有数据的id
  179. Redis::srem($redis_key, $rem_ids);
  180. }
  181. }
  182. // 等待1秒
  183. sleep(1);
  184. }
  185. }
  186. private function sendEvent($data, $event = null, $id = null) {
  187. // // 事件ID
  188. // if ($id !== null) {
  189. // echo "id: {$id}\n";
  190. // }
  191. // 事件类型
  192. if ($event !== null) {
  193. echo "event: {$event}\n";
  194. }
  195. // 数据部分(JSON格式)
  196. echo "data: " . json_encode($data) . "\n\n";
  197. // 立即发送数据
  198. if(ob_get_level()>0) ob_flush();
  199. flush();
  200. }
  201. public function emotionGroups(Request $request) {
  202. $data = $request->all();
  203. $result = $this->deepseekService->emotionGroups($data);
  204. return $this->success($result);
  205. }
  206. /**
  207. * 剧本列表
  208. *
  209. * @param Request $request
  210. * @return mixed
  211. */
  212. public function scriptList(Request $request) {
  213. $data = $request->all();
  214. $result = $this->deepseekService->scriptList($data);
  215. return $this->success($result, [new DeepSeekTransformer(), 'newBuildScriptList']);
  216. }
  217. public function scripts(Request $request) {
  218. $data = $request->all();
  219. $result = $this->deepseekService->scripts($data);
  220. return $this->success($result);
  221. }
  222. // 剧本详情
  223. public function scriptInfo(Request $request) {
  224. $data = $request->all();
  225. $result = $this->deepseekService->scriptInfo($data);
  226. return $this->success($result);
  227. }
  228. /**
  229. * 创建剧本
  230. *
  231. * @param Request $request
  232. * @return mixed
  233. */
  234. public function createScript(Request $request) {
  235. $data = $request->all();
  236. $result = $this->deepseekService->createScript($data);
  237. return $this->success(['script_id' => $result]);
  238. }
  239. // 编辑剧本
  240. public function editScript(Request $request) {
  241. $data = $request->all();
  242. $result = $this->deepseekService->editScript($data);
  243. return $this->success(['success' => $result ? 1 : 0]);
  244. }
  245. // 删除剧本
  246. public function delScript(Request $request) {
  247. $data = $request->all();
  248. $result = $this->deepseekService->delScript($data);
  249. return $this->success(['success' => $result ? 1 : 0]);
  250. }
  251. /**
  252. * 创建分集组
  253. *
  254. * @param Request $request
  255. * @return mixed
  256. */
  257. public function createEpisode(Request $request) {
  258. $data = $request->all();
  259. $result = $this->deepseekService->createEpisode($data);
  260. return $this->success(['group_id' => $result]);
  261. }
  262. // 编辑剧本
  263. public function editEpisode(Request $request) {
  264. $data = $request->all();
  265. $result = $this->deepseekService->editEpisode($data);
  266. return $this->success(['success' => $result ? 1 : 0]);
  267. }
  268. // 删除剧本
  269. public function delEpisode(Request $request) {
  270. $data = $request->all();
  271. $result = $this->deepseekService->delEpisode($data);
  272. return $this->success(['success' => $result ? 1 : 0]);
  273. }
  274. /**
  275. * 与DeepSeek对话(带文件上传)
  276. * @param Request $request
  277. * @return mixed
  278. * @throws ApiException
  279. */
  280. public function chatWithFile(Request $request) {
  281. // 忽略所有超时限制
  282. set_time_limit(0);
  283. ini_set('max_execution_time', '0');
  284. $data = $request->all();
  285. $result = $this->deepseekService->chatWithFile($data);
  286. return $this->success($result);
  287. }
  288. /**
  289. * 与DeepSeek对话(带文件上传 - 流式输出版本)
  290. * @param Request $request
  291. * @return \Symfony\Component\HttpFoundation\StreamedResponse
  292. * @throws ApiException
  293. */
  294. public function chatWithFileStream(Request $request) {
  295. // 忽略所有超时限制
  296. set_time_limit(0);
  297. ini_set('max_execution_time', '0');
  298. $data = $request->all();
  299. return response()->stream(function () use ($data) {
  300. // 禁用所有输出缓冲
  301. if (ob_get_level()) {
  302. ob_end_clean();
  303. }
  304. // 设置输出缓冲为关闭
  305. ini_set('output_buffering', 'off');
  306. ini_set('zlib.output_compression', 'off');
  307. // 立即刷新
  308. if (function_exists('apache_setenv')) {
  309. apache_setenv('no-gzip', '1');
  310. }
  311. try {
  312. $generator = $this->deepseekService->chatWithFileStream($data);
  313. foreach ($generator as $chunk) {
  314. // 发送 SSE 格式的数据
  315. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  316. echo "data: {$jsonData}\n\n";
  317. // 强制刷新输出缓冲区
  318. if (ob_get_level() > 0) {
  319. ob_flush();
  320. }
  321. flush();
  322. // 检查客户端是否断开连接
  323. if (connection_aborted()) {
  324. break;
  325. }
  326. }
  327. // 发送结束标记
  328. echo "data: [DONE]\n\n";
  329. if (ob_get_level() > 0) {
  330. ob_flush();
  331. }
  332. flush();
  333. } catch (\Exception $e) {
  334. // 发送错误信息
  335. $error = [
  336. 'type' => 'error',
  337. 'msg' => $e->getMessage(),
  338. 'code' => $e->getCode()
  339. ];
  340. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  341. echo "data: {$jsonError}\n\n";
  342. if (ob_get_level() > 0) {
  343. ob_flush();
  344. }
  345. flush();
  346. }
  347. }, 200, [
  348. 'Content-Type' => 'text/event-stream',
  349. 'Cache-Control' => 'no-cache',
  350. 'Connection' => 'keep-alive',
  351. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  352. 'Access-Control-Allow-Origin' => '*',
  353. 'Access-Control-Allow-Credentials' => 'true'
  354. ]);
  355. }
  356. public function generateEpisodes(Request $request) {
  357. // 忽略所有超时限制
  358. set_time_limit(0);
  359. ini_set('max_execution_time', '0');
  360. $data = $request->all();
  361. return response()->stream(function () use ($data) {
  362. // 禁用所有输出缓冲
  363. if (ob_get_level()) {
  364. ob_end_clean();
  365. }
  366. // 设置输出缓冲为关闭
  367. ini_set('output_buffering', 'off');
  368. ini_set('zlib.output_compression', 'off');
  369. // 立即刷新
  370. if (function_exists('apache_setenv')) {
  371. apache_setenv('no-gzip', '1');
  372. }
  373. try {
  374. $generator = $this->deepseekService->generateEpisodes($data);
  375. foreach ($generator as $chunk) {
  376. // 发送 SSE 格式的数据
  377. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  378. echo "data: {$jsonData}\n\n";
  379. // 强制刷新输出缓冲区
  380. if (ob_get_level() > 0) {
  381. ob_flush();
  382. }
  383. flush();
  384. // 检查客户端是否断开连接
  385. if (connection_aborted()) {
  386. break;
  387. }
  388. }
  389. // 发送结束标记
  390. echo "data: [DONE]\n\n";
  391. if (ob_get_level() > 0) {
  392. ob_flush();
  393. }
  394. flush();
  395. } catch (\Exception $e) {
  396. // 发送错误信息
  397. $error = [
  398. 'type' => 'error',
  399. 'msg' => $e->getMessage(),
  400. 'code' => $e->getCode()
  401. ];
  402. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  403. echo "data: {$jsonError}\n\n";
  404. if (ob_get_level() > 0) {
  405. ob_flush();
  406. }
  407. flush();
  408. }
  409. }, 200, [
  410. 'Content-Type' => 'text/event-stream',
  411. 'Cache-Control' => 'no-cache',
  412. 'Connection' => 'keep-alive',
  413. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  414. 'Access-Control-Allow-Origin' => '*',
  415. 'Access-Control-Allow-Credentials' => 'true'
  416. ]);
  417. }
  418. // 保存剧本
  419. public function saveScript(Request $request) {
  420. $data = $request->all();
  421. $result = $this->deepseekService->saveScript($data);
  422. return $this->success(['success'=>$result ? 1 : 0]);
  423. }
  424. // 导出剧本
  425. public function exportScript(Request $request) {
  426. $data = $request->all();
  427. // 验证必要参数
  428. if (empty($data['script_id'])) {
  429. return $this->error('20003:缺少剧本ID参数');
  430. }
  431. // 直接调用导出服务,该方法会直接输出文件并退出
  432. $this->deepseekService->exportScript($data);
  433. }
  434. public function getBookContent(Request $request) {
  435. $data = $request->all();
  436. // 直接调用导出服务,该方法会直接输出文件并退出
  437. $result = $this->deepseekService->getBookContent($data);
  438. return $this->success($result);
  439. }
  440. // 新建对话
  441. public function addChat(Request $request) {
  442. // 忽略所有超时限制
  443. set_time_limit(0);
  444. ini_set('max_execution_time', '0');
  445. $data = $request->all();
  446. return response()->stream(function () use ($data) {
  447. // 禁用所有输出缓冲
  448. if (ob_get_level()) {
  449. ob_end_clean();
  450. }
  451. // 设置输出缓冲为关闭
  452. ini_set('output_buffering', 'off');
  453. ini_set('zlib.output_compression', 'off');
  454. // 立即刷新
  455. if (function_exists('apache_setenv')) {
  456. apache_setenv('no-gzip', '1');
  457. }
  458. try {
  459. $generator = $this->deepseekService->addChat($data);
  460. foreach ($generator as $chunk) {
  461. // 发送 SSE 格式的数据
  462. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  463. echo "data: {$jsonData}\n\n";
  464. // 强制刷新输出缓冲区
  465. if (ob_get_level() > 0) {
  466. ob_flush();
  467. }
  468. flush();
  469. // 检查客户端是否断开连接
  470. if (connection_aborted()) {
  471. break;
  472. }
  473. }
  474. // 发送结束标记
  475. echo "data: [DONE]\n\n";
  476. if (ob_get_level() > 0) {
  477. ob_flush();
  478. }
  479. flush();
  480. } catch (\Exception $e) {
  481. // 发送错误信息
  482. $error = [
  483. 'type' => 'error',
  484. 'msg' => $e->getMessage(),
  485. 'code' => $e->getCode()
  486. ];
  487. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  488. echo "data: {$jsonError}\n\n";
  489. if (ob_get_level() > 0) {
  490. ob_flush();
  491. }
  492. flush();
  493. }
  494. }, 200, [
  495. 'Content-Type' => 'text/event-stream',
  496. 'Cache-Control' => 'no-cache',
  497. 'Connection' => 'keep-alive',
  498. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  499. 'Access-Control-Allow-Origin' => '*',
  500. 'Access-Control-Allow-Credentials' => 'true'
  501. ]);
  502. }
  503. // 调整大纲
  504. public function reGenerateAnime(Request $request) {
  505. // 忽略所有超时限制
  506. set_time_limit(0);
  507. ini_set('max_execution_time', '0');
  508. $data = $request->all();
  509. return response()->stream(function () use ($data) {
  510. // 禁用所有输出缓冲
  511. if (ob_get_level()) {
  512. ob_end_clean();
  513. }
  514. // 设置输出缓冲为关闭
  515. ini_set('output_buffering', 'off');
  516. ini_set('zlib.output_compression', 'off');
  517. // 立即刷新
  518. if (function_exists('apache_setenv')) {
  519. apache_setenv('no-gzip', '1');
  520. }
  521. try {
  522. $generator = $this->deepseekService->reGenerateAnime($data);
  523. foreach ($generator as $chunk) {
  524. // 发送 SSE 格式的数据
  525. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  526. echo "data: {$jsonData}\n\n";
  527. // 强制刷新输出缓冲区
  528. if (ob_get_level() > 0) {
  529. ob_flush();
  530. }
  531. flush();
  532. // 检查客户端是否断开连接
  533. if (connection_aborted()) {
  534. break;
  535. }
  536. }
  537. // 发送结束标记
  538. echo "data: [DONE]\n\n";
  539. if (ob_get_level() > 0) {
  540. ob_flush();
  541. }
  542. flush();
  543. } catch (\Exception $e) {
  544. // 发送错误信息
  545. $error = [
  546. 'type' => 'error',
  547. 'msg' => $e->getMessage(),
  548. 'code' => $e->getCode()
  549. ];
  550. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  551. echo "data: {$jsonError}\n\n";
  552. if (ob_get_level() > 0) {
  553. ob_flush();
  554. }
  555. flush();
  556. }
  557. }, 200, [
  558. 'Content-Type' => 'text/event-stream',
  559. 'Cache-Control' => 'no-cache',
  560. 'Connection' => 'keep-alive',
  561. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  562. 'Access-Control-Allow-Origin' => '*',
  563. 'Access-Control-Allow-Credentials' => 'true'
  564. ]);
  565. }
  566. public function chat(Request $request) {
  567. // 忽略所有超时限制
  568. set_time_limit(0);
  569. ini_set('max_execution_time', '0');
  570. $data = $request->all();
  571. return response()->stream(function () use ($data) {
  572. // 禁用所有输出缓冲
  573. if (ob_get_level()) {
  574. ob_end_clean();
  575. }
  576. // 设置输出缓冲为关闭
  577. ini_set('output_buffering', 'off');
  578. ini_set('zlib.output_compression', 'off');
  579. // 立即刷新
  580. if (function_exists('apache_setenv')) {
  581. apache_setenv('no-gzip', '1');
  582. }
  583. try {
  584. $generator = $this->deepseekService->chat($data);
  585. foreach ($generator as $chunk) {
  586. // 发送 SSE 格式的数据
  587. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  588. echo "data: {$jsonData}\n\n";
  589. // 强制刷新输出缓冲区
  590. if (ob_get_level() > 0) {
  591. ob_flush();
  592. }
  593. flush();
  594. // 检查客户端是否断开连接
  595. if (connection_aborted()) {
  596. break;
  597. }
  598. }
  599. // 发送结束标记
  600. echo "data: [DONE]\n\n";
  601. if (ob_get_level() > 0) {
  602. ob_flush();
  603. }
  604. flush();
  605. } catch (\Exception $e) {
  606. // 发送错误信息
  607. $error = [
  608. 'type' => 'error',
  609. 'msg' => $e->getMessage(),
  610. 'code' => $e->getCode()
  611. ];
  612. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  613. echo "data: {$jsonError}\n\n";
  614. if (ob_get_level() > 0) {
  615. ob_flush();
  616. }
  617. flush();
  618. }
  619. }, 200, [
  620. 'Content-Type' => 'text/event-stream',
  621. 'Cache-Control' => 'no-cache',
  622. 'Connection' => 'keep-alive',
  623. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  624. 'Access-Control-Allow-Origin' => '*',
  625. 'Access-Control-Allow-Credentials' => 'true'
  626. ]);
  627. }
  628. // 对话改图
  629. public function chatChangeImg(Request $request) {
  630. $data = $request->all();
  631. $result = $this->deepseekService->chatChangeImg($data);
  632. return $this->success($result);
  633. }
  634. /**
  635. * 通用文生文接口 - 流式输出
  636. * @param Request $request
  637. * @return \Symfony\Component\HttpFoundation\StreamedResponse
  638. */
  639. public function generateText(Request $request) {
  640. // 忽略所有超时限制
  641. set_time_limit(0);
  642. ini_set('max_execution_time', '0');
  643. $data = $request->all();
  644. // 处理上传的图片文件
  645. if ($request->hasFile('images')) {
  646. $data['images'] = $request->file('images');
  647. } else if ($request->hasFile('image')) {
  648. // 支持单张图片
  649. $data['images'] = [$request->file('image')];
  650. }
  651. return response()->stream(function () use ($data) {
  652. // 禁用所有输出缓冲
  653. if (ob_get_level()) {
  654. ob_end_clean();
  655. }
  656. // 设置输出缓冲为关闭
  657. ini_set('output_buffering', 'off');
  658. ini_set('zlib.output_compression', 'off');
  659. // 立即刷新
  660. if (function_exists('apache_setenv')) {
  661. apache_setenv('no-gzip', '1');
  662. }
  663. try {
  664. $generator = $this->deepseekService->generateText($data);
  665. foreach ($generator as $chunk) {
  666. // 发送 SSE 格式的数据
  667. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  668. echo "data: {$jsonData}\n\n";
  669. // 强制刷新输出缓冲区
  670. if (ob_get_level() > 0) {
  671. ob_flush();
  672. }
  673. flush();
  674. // 检查客户端是否断开连接
  675. if (connection_aborted()) {
  676. break;
  677. }
  678. }
  679. // 发送结束标记
  680. echo "data: [DONE]\n\n";
  681. if (ob_get_level() > 0) {
  682. ob_flush();
  683. }
  684. flush();
  685. } catch (\Exception $e) {
  686. // 发送错误信息
  687. $error = [
  688. 'type' => 'error',
  689. 'msg' => $e->getMessage(),
  690. 'code' => $e->getCode()
  691. ];
  692. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  693. echo "data: {$jsonError}\n\n";
  694. if (ob_get_level() > 0) {
  695. ob_flush();
  696. }
  697. flush();
  698. }
  699. }, 200, [
  700. 'Content-Type' => 'text/event-stream',
  701. 'Cache-Control' => 'no-cache',
  702. 'Connection' => 'keep-alive',
  703. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  704. 'Access-Control-Allow-Origin' => '*',
  705. 'Access-Control-Allow-Credentials' => 'true'
  706. ]);
  707. }
  708. }