TestAIArticleTranslate.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\AIAssistant\ArticleTranslateService;
  4. use Illuminate\Console\Command;
  5. class TestAIArticleTranslate extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. * php artisan test:ai.article.translate
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'test:ai.article.translate {--article=} {--anthology=} {--model=} {--channel=}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Execute the console command.
  22. */
  23. public function handle()
  24. {
  25. if (
  26. ! $this->option('model') ||
  27. ! $this->option('channel')
  28. ) {
  29. $this->error('model,article,channel is requested');
  30. return;
  31. }
  32. //
  33. // ===== 创建 Service =====
  34. $service = app(ArticleTranslateService::class);
  35. // ===== 执行 =====
  36. if ($this->option('article')) {
  37. $this->info('article translate start');
  38. $total = $service->setModel($this->option('model'))
  39. ->setChannel($this->option('channel'))
  40. ->translateArticle($this->option('article'))
  41. ->save();
  42. $this->info("{$total} sentences saved");
  43. }
  44. if ($this->option('anthology')) {
  45. $this->info('anthology translate start');
  46. $total = $service->setModel($this->option('model'))
  47. ->setChannel($this->option('channel'))
  48. ->translateAnthology($this->option('anthology'), function ($article, $sentences) {
  49. $this->info("translate article {$article} sentences {$sentences}");
  50. });
  51. $this->info("{$total} article saved");
  52. }
  53. }
  54. }