AiArticleTranslate.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 {--article=} {--anthology=} {--model=} {--channel=} {--token=} {--endpoint=}')]
  9. #[Description('translate article by ai ')]
  10. class AiArticleTranslate extends Command
  11. {
  12. /**
  13. * Execute the console command.
  14. */
  15. public function handle()
  16. {
  17. if (
  18. !$this->option('model') ||
  19. !$this->option('channel')
  20. ) {
  21. $this->error('model,article,channel is requested');
  22. return;
  23. }
  24. //
  25. // ===== 创建 Service =====
  26. $service = app(ArticleTranslateService::class);
  27. $articleService = app(ArticleService::class);
  28. // ===== 执行 =====
  29. if ($this->option('article')) {
  30. $this->info('article translate start');
  31. $total = $service->setModel($this->option('model'))
  32. ->setChannel($this->option('channel'))
  33. ->translateArticle($this->option('article'))
  34. ->saveRpc($this->option('endpoint'), $this->option('token'));
  35. $this->info("{$total} sentences saved");
  36. }
  37. if ($this->option('anthology')) {
  38. $this->info('anthology translate start');
  39. $articleIds = $articleService->articlesInAnthology($this->option('anthology'));
  40. foreach ($articleIds as $article) {
  41. $this->info('article translate start');
  42. $total = $service->setModel($this->option('model'))
  43. ->setChannel($this->option('channel'))
  44. ->translateArticle($article)
  45. ->saveRpc($this->option('endpoint'), $this->option('token'));
  46. $this->info("{$total} sentences saved");
  47. }
  48. $this->info(count($articleIds) . " article saved");
  49. }
  50. }
  51. }