TestAIArticleTranslate.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Services\AIAssistant\ArticleTranslateService;
  5. class TestAIArticleTranslate extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. * php artisan test:ai.article.translate
  10. * @var string
  11. */
  12. protected $signature = 'test:ai.article.translate {--article=} {--anthology=} {--model=} {--channel=}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Command description';
  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. // ===== 执行 =====
  35. if ($this->option('article')) {
  36. $this->info('article translate start');
  37. $total = $service->setModel($this->option('model'))
  38. ->setChannel($this->option('channel'))
  39. ->translateArticle($this->option('article'))
  40. ->save();
  41. $this->info("{$total} sentences saved");
  42. }
  43. if ($this->option('anthology')) {
  44. $this->info('anthology translate start');
  45. $total = $service->setModel($this->option('model'))
  46. ->setChannel($this->option('channel'))
  47. ->translateAnthology($this->option('anthology'), function ($article, $sentences) {
  48. $this->info("translate article {$article} sentences {$sentences}");
  49. });
  50. $this->info("{$total} article saved");
  51. }
  52. }
  53. }