DeepSeekController.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. public function getEpisodeContent(Request $request) {
  419. $data = $request->all();
  420. $result = $this->deepseekService->getEpisodeContent($data);
  421. return $this->success($result);
  422. }
  423. // 保存剧本
  424. public function saveEpisodeContent(Request $request) {
  425. $data = $request->all();
  426. $result = $this->deepseekService->saveEpisodeContent($data);
  427. return $this->success(['success'=>$result ? 1 : 0]);
  428. }
  429. // 导出剧本
  430. public function exportScript(Request $request) {
  431. $data = $request->all();
  432. // 验证必要参数
  433. if (empty($data['script_id'])) {
  434. return $this->error('20003:缺少剧本ID参数');
  435. }
  436. // 直接调用导出服务,该方法会直接输出文件并退出
  437. $this->deepseekService->exportScript($data);
  438. }
  439. public function getBookContent(Request $request) {
  440. $data = $request->all();
  441. // 直接调用导出服务,该方法会直接输出文件并退出
  442. $result = $this->deepseekService->getBookContent($data);
  443. return $this->success($result);
  444. }
  445. public function createGenerateText(Request $request) {
  446. // 忽略所有超时限制
  447. set_time_limit(0);
  448. ini_set('max_execution_time', '0');
  449. $data = $request->all();
  450. $result = $this->deepseekService->createGenerateText($data);
  451. return $this->success($result);
  452. }
  453. /**
  454. * 通用文生文(非流式版本)
  455. * @param Request $request
  456. * @return mixed
  457. */
  458. public function newGenerateText(Request $request) {
  459. // 忽略所有超时限制
  460. set_time_limit(0);
  461. ini_set('max_execution_time', '0');
  462. $data = $request->all();
  463. $result = $this->deepseekService->newGenerateText($data);
  464. return $this->success($result);
  465. }
  466. // 保存剧本沟通记录
  467. public function saveScriptChatHistory(Request $request) {
  468. $data = $request->all();
  469. $result = $this->deepseekService->saveScriptChatHistory($data);
  470. return $this->success(['success'=>$result ? 1 : 0]);
  471. }
  472. /**
  473. * 获取剧本的历史对话记录
  474. * @param Request $request
  475. * @return mixed
  476. */
  477. public function getScriptChatHistory(Request $request) {
  478. $data = $request->all();
  479. $result = $this->deepseekService->getScriptChatHistory($data);
  480. return $this->success($result);
  481. }
  482. // 新建对话
  483. public function addChat(Request $request) {
  484. // 忽略所有超时限制
  485. set_time_limit(0);
  486. ini_set('max_execution_time', '0');
  487. $data = $request->all();
  488. return response()->stream(function () use ($data) {
  489. // 禁用所有输出缓冲
  490. if (ob_get_level()) {
  491. ob_end_clean();
  492. }
  493. // 设置输出缓冲为关闭
  494. ini_set('output_buffering', 'off');
  495. ini_set('zlib.output_compression', 'off');
  496. // 立即刷新
  497. if (function_exists('apache_setenv')) {
  498. apache_setenv('no-gzip', '1');
  499. }
  500. try {
  501. $generator = $this->deepseekService->addChat($data);
  502. foreach ($generator as $chunk) {
  503. // 发送 SSE 格式的数据
  504. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  505. echo "data: {$jsonData}\n\n";
  506. // 强制刷新输出缓冲区
  507. if (ob_get_level() > 0) {
  508. ob_flush();
  509. }
  510. flush();
  511. // 检查客户端是否断开连接
  512. if (connection_aborted()) {
  513. break;
  514. }
  515. }
  516. // 发送结束标记
  517. echo "data: [DONE]\n\n";
  518. if (ob_get_level() > 0) {
  519. ob_flush();
  520. }
  521. flush();
  522. } catch (\Exception $e) {
  523. // 发送错误信息
  524. $error = [
  525. 'type' => 'error',
  526. 'msg' => $e->getMessage(),
  527. 'code' => $e->getCode()
  528. ];
  529. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  530. echo "data: {$jsonError}\n\n";
  531. if (ob_get_level() > 0) {
  532. ob_flush();
  533. }
  534. flush();
  535. }
  536. }, 200, [
  537. 'Content-Type' => 'text/event-stream',
  538. 'Cache-Control' => 'no-cache',
  539. 'Connection' => 'keep-alive',
  540. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  541. 'Access-Control-Allow-Origin' => '*',
  542. 'Access-Control-Allow-Credentials' => 'true'
  543. ]);
  544. }
  545. // 调整大纲
  546. public function reGenerateAnime(Request $request) {
  547. // 忽略所有超时限制
  548. set_time_limit(0);
  549. ini_set('max_execution_time', '0');
  550. $data = $request->all();
  551. return response()->stream(function () use ($data) {
  552. // 禁用所有输出缓冲
  553. if (ob_get_level()) {
  554. ob_end_clean();
  555. }
  556. // 设置输出缓冲为关闭
  557. ini_set('output_buffering', 'off');
  558. ini_set('zlib.output_compression', 'off');
  559. // 立即刷新
  560. if (function_exists('apache_setenv')) {
  561. apache_setenv('no-gzip', '1');
  562. }
  563. try {
  564. $generator = $this->deepseekService->reGenerateAnime($data);
  565. foreach ($generator as $chunk) {
  566. // 发送 SSE 格式的数据
  567. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  568. echo "data: {$jsonData}\n\n";
  569. // 强制刷新输出缓冲区
  570. if (ob_get_level() > 0) {
  571. ob_flush();
  572. }
  573. flush();
  574. // 检查客户端是否断开连接
  575. if (connection_aborted()) {
  576. break;
  577. }
  578. }
  579. // 发送结束标记
  580. echo "data: [DONE]\n\n";
  581. if (ob_get_level() > 0) {
  582. ob_flush();
  583. }
  584. flush();
  585. } catch (\Exception $e) {
  586. // 发送错误信息
  587. $error = [
  588. 'type' => 'error',
  589. 'msg' => $e->getMessage(),
  590. 'code' => $e->getCode()
  591. ];
  592. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  593. echo "data: {$jsonError}\n\n";
  594. if (ob_get_level() > 0) {
  595. ob_flush();
  596. }
  597. flush();
  598. }
  599. }, 200, [
  600. 'Content-Type' => 'text/event-stream',
  601. 'Cache-Control' => 'no-cache',
  602. 'Connection' => 'keep-alive',
  603. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  604. 'Access-Control-Allow-Origin' => '*',
  605. 'Access-Control-Allow-Credentials' => 'true'
  606. ]);
  607. }
  608. public function chat(Request $request) {
  609. // 忽略所有超时限制
  610. set_time_limit(0);
  611. ini_set('max_execution_time', '0');
  612. $data = $request->all();
  613. return response()->stream(function () use ($data) {
  614. // 禁用所有输出缓冲
  615. if (ob_get_level()) {
  616. ob_end_clean();
  617. }
  618. // 设置输出缓冲为关闭
  619. ini_set('output_buffering', 'off');
  620. ini_set('zlib.output_compression', 'off');
  621. // 立即刷新
  622. if (function_exists('apache_setenv')) {
  623. apache_setenv('no-gzip', '1');
  624. }
  625. try {
  626. $generator = $this->deepseekService->chat($data);
  627. foreach ($generator as $chunk) {
  628. // 发送 SSE 格式的数据
  629. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  630. echo "data: {$jsonData}\n\n";
  631. // 强制刷新输出缓冲区
  632. if (ob_get_level() > 0) {
  633. ob_flush();
  634. }
  635. flush();
  636. // 检查客户端是否断开连接
  637. if (connection_aborted()) {
  638. break;
  639. }
  640. }
  641. // 发送结束标记
  642. echo "data: [DONE]\n\n";
  643. if (ob_get_level() > 0) {
  644. ob_flush();
  645. }
  646. flush();
  647. } catch (\Exception $e) {
  648. // 发送错误信息
  649. $error = [
  650. 'type' => 'error',
  651. 'msg' => $e->getMessage(),
  652. 'code' => $e->getCode()
  653. ];
  654. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  655. echo "data: {$jsonError}\n\n";
  656. if (ob_get_level() > 0) {
  657. ob_flush();
  658. }
  659. flush();
  660. }
  661. }, 200, [
  662. 'Content-Type' => 'text/event-stream',
  663. 'Cache-Control' => 'no-cache',
  664. 'Connection' => 'keep-alive',
  665. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  666. 'Access-Control-Allow-Origin' => '*',
  667. 'Access-Control-Allow-Credentials' => 'true'
  668. ]);
  669. }
  670. // 新建对话
  671. public function addChatForAce(Request $request) {
  672. // 忽略所有超时限制
  673. set_time_limit(0);
  674. ini_set('max_execution_time', '0');
  675. $data = $request->all();
  676. return response()->stream(function () use ($data) {
  677. // 禁用所有输出缓冲
  678. if (ob_get_level()) {
  679. ob_end_clean();
  680. }
  681. // 设置输出缓冲为关闭
  682. ini_set('output_buffering', 'off');
  683. ini_set('zlib.output_compression', 'off');
  684. // 立即刷新
  685. if (function_exists('apache_setenv')) {
  686. apache_setenv('no-gzip', '1');
  687. }
  688. try {
  689. $generator = $this->deepseekService->addChatForAce($data);
  690. foreach ($generator as $chunk) {
  691. // 发送 SSE 格式的数据
  692. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  693. echo "data: {$jsonData}\n\n";
  694. // 强制刷新输出缓冲区
  695. if (ob_get_level() > 0) {
  696. ob_flush();
  697. }
  698. flush();
  699. // 检查客户端是否断开连接
  700. if (connection_aborted()) {
  701. break;
  702. }
  703. }
  704. // 发送结束标记
  705. echo "data: [DONE]\n\n";
  706. if (ob_get_level() > 0) {
  707. ob_flush();
  708. }
  709. flush();
  710. } catch (\Exception $e) {
  711. // 发送错误信息
  712. $error = [
  713. 'type' => 'error',
  714. 'msg' => $e->getMessage(),
  715. 'code' => $e->getCode()
  716. ];
  717. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  718. echo "data: {$jsonError}\n\n";
  719. if (ob_get_level() > 0) {
  720. ob_flush();
  721. }
  722. flush();
  723. }
  724. }, 200, [
  725. 'Content-Type' => 'text/event-stream',
  726. 'Cache-Control' => 'no-cache',
  727. 'Connection' => 'keep-alive',
  728. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  729. 'Access-Control-Allow-Origin' => '*',
  730. 'Access-Control-Allow-Credentials' => 'true'
  731. ]);
  732. }
  733. // 调整大纲
  734. public function reGenerateAnimeForAce(Request $request) {
  735. // 忽略所有超时限制
  736. set_time_limit(0);
  737. ini_set('max_execution_time', '0');
  738. $data = $request->all();
  739. return response()->stream(function () use ($data) {
  740. // 禁用所有输出缓冲
  741. if (ob_get_level()) {
  742. ob_end_clean();
  743. }
  744. // 设置输出缓冲为关闭
  745. ini_set('output_buffering', 'off');
  746. ini_set('zlib.output_compression', 'off');
  747. // 立即刷新
  748. if (function_exists('apache_setenv')) {
  749. apache_setenv('no-gzip', '1');
  750. }
  751. try {
  752. $generator = $this->deepseekService->reGenerateAnimeForAce($data);
  753. foreach ($generator as $chunk) {
  754. // 发送 SSE 格式的数据
  755. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  756. echo "data: {$jsonData}\n\n";
  757. // 强制刷新输出缓冲区
  758. if (ob_get_level() > 0) {
  759. ob_flush();
  760. }
  761. flush();
  762. // 检查客户端是否断开连接
  763. if (connection_aborted()) {
  764. break;
  765. }
  766. }
  767. // 发送结束标记
  768. echo "data: [DONE]\n\n";
  769. if (ob_get_level() > 0) {
  770. ob_flush();
  771. }
  772. flush();
  773. } catch (\Exception $e) {
  774. // 发送错误信息
  775. $error = [
  776. 'type' => 'error',
  777. 'msg' => $e->getMessage(),
  778. 'code' => $e->getCode()
  779. ];
  780. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  781. echo "data: {$jsonError}\n\n";
  782. if (ob_get_level() > 0) {
  783. ob_flush();
  784. }
  785. flush();
  786. }
  787. }, 200, [
  788. 'Content-Type' => 'text/event-stream',
  789. 'Cache-Control' => 'no-cache',
  790. 'Connection' => 'keep-alive',
  791. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  792. 'Access-Control-Allow-Origin' => '*',
  793. 'Access-Control-Allow-Credentials' => 'true'
  794. ]);
  795. }
  796. public function chatForAce(Request $request) {
  797. // 忽略所有超时限制
  798. set_time_limit(0);
  799. ini_set('max_execution_time', '0');
  800. $data = $request->all();
  801. return response()->stream(function () use ($data) {
  802. // 禁用所有输出缓冲
  803. if (ob_get_level()) {
  804. ob_end_clean();
  805. }
  806. // 设置输出缓冲为关闭
  807. ini_set('output_buffering', 'off');
  808. ini_set('zlib.output_compression', 'off');
  809. // 立即刷新
  810. if (function_exists('apache_setenv')) {
  811. apache_setenv('no-gzip', '1');
  812. }
  813. try {
  814. $generator = $this->deepseekService->chatForAce($data);
  815. foreach ($generator as $chunk) {
  816. // 发送 SSE 格式的数据
  817. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  818. echo "data: {$jsonData}\n\n";
  819. // 强制刷新输出缓冲区
  820. if (ob_get_level() > 0) {
  821. ob_flush();
  822. }
  823. flush();
  824. // 检查客户端是否断开连接
  825. if (connection_aborted()) {
  826. break;
  827. }
  828. }
  829. // 发送结束标记
  830. echo "data: [DONE]\n\n";
  831. if (ob_get_level() > 0) {
  832. ob_flush();
  833. }
  834. flush();
  835. } catch (\Exception $e) {
  836. // 发送错误信息
  837. $error = [
  838. 'type' => 'error',
  839. 'msg' => $e->getMessage(),
  840. 'code' => $e->getCode()
  841. ];
  842. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  843. echo "data: {$jsonError}\n\n";
  844. if (ob_get_level() > 0) {
  845. ob_flush();
  846. }
  847. flush();
  848. }
  849. }, 200, [
  850. 'Content-Type' => 'text/event-stream',
  851. 'Cache-Control' => 'no-cache',
  852. 'Connection' => 'keep-alive',
  853. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  854. 'Access-Control-Allow-Origin' => '*',
  855. 'Access-Control-Allow-Credentials' => 'true'
  856. ]);
  857. }
  858. // 对话改图
  859. public function chatChangeImg(Request $request) {
  860. $data = $request->all();
  861. $result = $this->deepseekService->chatChangeImg($data);
  862. return $this->success($result);
  863. }
  864. /**
  865. * 通用文生文接口 - 流式输出
  866. * @param Request $request
  867. * @return \Symfony\Component\HttpFoundation\StreamedResponse
  868. */
  869. public function generateText(Request $request) {
  870. // 忽略所有超时限制
  871. set_time_limit(0);
  872. ini_set('max_execution_time', '0');
  873. $data = $request->all();
  874. // 处理上传的图片文件
  875. if ($request->hasFile('images')) {
  876. $data['images'] = $request->file('images');
  877. } else if ($request->hasFile('image')) {
  878. // 支持单张图片
  879. $data['images'] = [$request->file('image')];
  880. }
  881. return response()->stream(function () use ($data) {
  882. // 禁用所有输出缓冲
  883. if (ob_get_level()) {
  884. ob_end_clean();
  885. }
  886. // 设置输出缓冲为关闭
  887. ini_set('output_buffering', 'off');
  888. ini_set('zlib.output_compression', 'off');
  889. // 立即刷新
  890. if (function_exists('apache_setenv')) {
  891. apache_setenv('no-gzip', '1');
  892. }
  893. try {
  894. $generator = $this->deepseekService->generateText($data);
  895. foreach ($generator as $chunk) {
  896. // 发送 SSE 格式的数据
  897. $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
  898. echo "data: {$jsonData}\n\n";
  899. // 强制刷新输出缓冲区
  900. if (ob_get_level() > 0) {
  901. ob_flush();
  902. }
  903. flush();
  904. // 检查客户端是否断开连接
  905. if (connection_aborted()) {
  906. break;
  907. }
  908. }
  909. // 发送结束标记
  910. echo "data: [DONE]\n\n";
  911. if (ob_get_level() > 0) {
  912. ob_flush();
  913. }
  914. flush();
  915. } catch (\Exception $e) {
  916. // 发送错误信息
  917. $error = [
  918. 'type' => 'error',
  919. 'msg' => $e->getMessage(),
  920. 'code' => $e->getCode()
  921. ];
  922. $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
  923. echo "data: {$jsonError}\n\n";
  924. if (ob_get_level() > 0) {
  925. ob_flush();
  926. }
  927. flush();
  928. }
  929. }, 200, [
  930. 'Content-Type' => 'text/event-stream',
  931. 'Cache-Control' => 'no-cache',
  932. 'Connection' => 'keep-alive',
  933. 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
  934. 'Access-Control-Allow-Origin' => '*',
  935. 'Access-Control-Allow-Credentials' => 'true'
  936. ]);
  937. }
  938. }