DeepSeekController.php 35 KB

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