AiArticleTranslate.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Attributes\Description;
  4. use Illuminate\Console\Attributes\Signature;
  5. use Illuminate\Console\Command;
  6. use App\Services\AIAssistant\ArticleTranslateService;
  7. use App\Services\ArticleService;
  8. #[Signature('app:ai-article-translate
  9. {--article=}
  10. {--anthology=}
  11. {--model=}
  12. {--channel=}
  13. {--thinking=} : deepseek deep thinking true or false
  14. {--token=}
  15. {--endpoint=}')]
  16. #[Description('translate article by ai ')]
  17. class AiArticleTranslate extends Command
  18. {
  19. /**
  20. * Execute the console command.
  21. */
  22. public function handle()
  23. {
  24. if (
  25. !$this->option('model') ||
  26. !$this->option('channel')
  27. ) {
  28. $this->error('model,article,channel is requested');
  29. return;
  30. }
  31. //
  32. // ===== 创建 Service =====
  33. $service = app(ArticleTranslateService::class);
  34. $articleService = app(ArticleService::class);
  35. // ===== 执行 =====
  36. if ($this->option('article')) {
  37. $this->info('article translate start');
  38. $llm = $service->setModel($this->option('model'))
  39. ->setChannel($this->option('channel'));
  40. if ($this->option('thinking')) {
  41. $llm = $llm->setThinking($this->option('thinking') === 'true');
  42. }
  43. $total = $llm->translateArticle($this->option('article'))
  44. ->save();
  45. $this->info("{$total} sentences saved");
  46. }
  47. if ($this->option('anthology')) {
  48. $this->info('anthology translate start');
  49. $articleIds = $articleService->articlesInAnthology($this->option('anthology'));
  50. foreach ($articleIds as $article) {
  51. $this->info('article translate start');
  52. $llm = $service->setModel($this->option('model'))
  53. ->setChannel($this->option('channel'));
  54. if ($this->option('thinking')) {
  55. $llm = $llm->setThinking($this->option('thinking') === 'true');
  56. }
  57. $total = $llm->translateArticle($article)
  58. ->save();
  59. $this->info("{$total} sentences saved");
  60. }
  61. $this->info(count($articleIds) . " article saved");
  62. }
  63. }
  64. }