MqAiTranslate.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Api\Mq;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Http;
  7. use Illuminate\Support\Str;
  8. use App\Http\Controllers\AuthController;
  9. use App\Models\Sentence;
  10. use App\Models\ModelLog;
  11. class MqAiTranslate extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. * php artisan mq:ai.translate
  16. * @var string
  17. */
  18. protected $signature = 'mq:ai.translate';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Command description';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. if (\App\Tools\Tools::isStop()) {
  42. return 0;
  43. }
  44. $exchange = 'router';
  45. $queue = 'ai_translate';
  46. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  47. Log::debug("mq:progress start.");
  48. Mq::worker($exchange, $queue, function ($message) {
  49. if (\App\Tools\Tools::isStop()) {
  50. return 0;
  51. }
  52. Log::debug('ai translate start', ['message' => $message]);
  53. //写入 model log
  54. $modelLog = new ModelLog();
  55. $modelLog->uid = Str::uuid();
  56. $param = [
  57. "model" => $message->model->model,
  58. "messages" => [
  59. ["role" => "system", "content" => $message->model->system_prompt],
  60. ["role" => "user", "content" => $message->prompt],
  61. ],
  62. 'prompt' => $message->prompt,
  63. "temperature" => 0.7,
  64. "stream" => false
  65. ];
  66. $this->info('ai request' . $message->model->url);
  67. $this->info('model:' . $param['model']);
  68. Log::debug('ai api request', [
  69. 'url' => $message->model->url,
  70. 'data' => $param
  71. ]);
  72. $modelLog->model_id = $message->model->uid;
  73. $modelLog->request_at = now();
  74. $modelLog->request_data = json_encode($param, JSON_UNESCAPED_UNICODE);
  75. $response = Http::withToken($message->model->key)
  76. ->retry(2, 120000)
  77. ->post($message->model->url, $param);
  78. $modelLog->request_headers = json_encode($response->handlerStats(), JSON_UNESCAPED_UNICODE);
  79. $modelLog->response_headers = json_encode($response->headers(), JSON_UNESCAPED_UNICODE);
  80. $modelLog->status = $response->status();
  81. $modelLog->response_data = json_encode($response->json(), JSON_UNESCAPED_UNICODE);
  82. if ($response->failed()) {
  83. $modelLog->success = false;
  84. $modelLog->save();
  85. $this->error('http response error' . $response->json('message'));
  86. Log::error('http response error', ['data' => $response->json()]);
  87. return 1;
  88. }
  89. $modelLog->save();
  90. $this->info('log saved');
  91. $aiData = $response->json();
  92. Log::debug('http response', ['data' => $response->json()]);
  93. $responseContent = $aiData['choices'][0]['message']['content'];
  94. if (isset($aiData['choices'][0]['message']['reasoning_content'])) {
  95. $reasoningContent = $aiData['choices'][0]['message']['reasoning_content'];
  96. }
  97. $this->info('ai content=' . $responseContent);
  98. if (empty($reasoningContent)) {
  99. $this->info('no reasoningContent');
  100. } else {
  101. $this->info('reasoning=' . $reasoningContent);
  102. }
  103. //获取model token
  104. Log::debug('ai assistant token', ['user' => $message->model->uid]);
  105. $token = AuthController::getUserToken($message->model->uid);
  106. Log::debug('ai assistant token', ['token' => $token]);
  107. if ($message->task->info->category === 'translate') {
  108. //写入句子库
  109. $url = config('app.url') . '/api/v2/sentence';
  110. $sentData = [];
  111. $message->sentence->content = $responseContent;
  112. $sentData[] = $message->sentence;
  113. $this->info("upload to {$url}");
  114. Log::debug('sentence update http request', ['data' => $sentData]);
  115. $response = Http::withToken($token)->post($url, [
  116. 'sentences' => $sentData,
  117. ]);
  118. Log::debug('sentence update http response', ['data' => $response->json()]);
  119. if ($response->failed()) {
  120. $this->error('upload error' . $response->json('message'));
  121. Log::error('upload error', ['data' => $response->json()]);
  122. } else {
  123. $this->info('upload successful');
  124. }
  125. }
  126. //写入discussion
  127. #获取句子id
  128. $sUid = Sentence::where('book_id', $message->sentence->book_id)
  129. ->where('paragraph', $message->sentence->paragraph)
  130. ->where('word_start', $message->sentence->word_start)
  131. ->where('word_end', $message->sentence->word_end)
  132. ->where('channel_uid', $message->sentence->channel_uid)
  133. ->value('uid');
  134. $url = config('app.url') . '/api/v2/discussion';
  135. $data = [
  136. 'res_id' => $sUid,
  137. 'res_type' => 'sentence',
  138. 'title' => $message->task->info->title,
  139. 'content' => $message->task->info->category,
  140. 'content_type' => 'markdown',
  141. 'type' => 'discussion',
  142. ];
  143. $response = Http::withToken($token)->post($url, $data);
  144. if ($response->failed()) {
  145. $this->error('discussion error' . $response->json('message'));
  146. Log::error('discussion error', ['data' => $response->json()]);
  147. } else {
  148. $this->info('discussion topic successful');
  149. }
  150. $data['parent'] = $response->json()['data']['id'];
  151. unset($data['title']);
  152. $topicChildren = [];
  153. //提示词
  154. $topicChildren[] = $message->prompt;
  155. //任务结果
  156. $topicChildren[] = $responseContent;
  157. //推理过程写入discussion
  158. if (isset($reasoningContent) && !empty($reasoningContent)) {
  159. $topicChildren[] = $reasoningContent;
  160. }
  161. foreach ($topicChildren as $content) {
  162. $data['content'] = $content;
  163. Log::debug('discussion child request', ['url' => $url, 'data' => $data]);
  164. $response = Http::withToken($token)->post($url, $data);
  165. if ($response->failed()) {
  166. $this->error('discussion error' . $response->json('message'));
  167. Log::error('discussion error', ['data' => $response->json()]);
  168. } else {
  169. $this->info('discussion child successful');
  170. }
  171. }
  172. //修改task 完成度
  173. $taskProgress = $message->task->progress;
  174. $progress = (int)($taskProgress->current * 100 / $taskProgress->total);
  175. $url = config('app.url') . '/api/v2/task/' . $message->task->info->id;
  176. $data = [
  177. 'progress' => $progress,
  178. ];
  179. Log::debug('task progress request', ['url' => $url, 'data' => $data]);
  180. $response = Http::withToken($token)->patch($url, $data);
  181. if ($response->failed()) {
  182. $this->error('task progress error' . $response->json('message'));
  183. Log::error('task progress error', ['data' => $response->json()]);
  184. } else {
  185. $this->info('task progress successful progress=' . $response->json()['data']['progress']);
  186. }
  187. //任务完成 修改任务状态为 done
  188. if ($taskProgress->current === $taskProgress->total) {
  189. $url = config('app.url') . '/api/v2/task-status/' . $message->task->info->id;
  190. $data = [
  191. 'status' => 'done',
  192. ];
  193. Log::debug('task status request', ['url' => $url, 'data' => $data]);
  194. $response = Http::withToken($token)->patch($url, $data);
  195. //判断状态码
  196. if ($response->failed()) {
  197. $this->error('task status error' . $response->json('message'));
  198. Log::error('task status error', ['data' => $response->json()]);
  199. } else {
  200. $this->info('task status successful ');
  201. }
  202. }
  203. return 0;
  204. });
  205. return 0;
  206. }
  207. }