FakeTranslateProvider.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. declare(strict_types=1);
  7. namespace OCA\Testing\TaskProcessing;
  8. use OCA\Testing\AppInfo\Application;
  9. use OCP\L10N\IFactory;
  10. use OCP\TaskProcessing\EShapeType;
  11. use OCP\TaskProcessing\ISynchronousProvider;
  12. use OCP\TaskProcessing\ShapeDescriptor;
  13. use OCP\TaskProcessing\ShapeEnumValue;
  14. use OCP\TaskProcessing\TaskTypes\TextToTextTranslate;
  15. use RuntimeException;
  16. class FakeTranslateProvider implements ISynchronousProvider {
  17. public function __construct(
  18. private IFactory $l10nFactory,
  19. ) {
  20. }
  21. public function getId(): string {
  22. return Application::APP_ID . '-translate';
  23. }
  24. public function getName(): string {
  25. return 'Fake translate task processing provider';
  26. }
  27. public function getTaskTypeId(): string {
  28. return TextToTextTranslate::ID;
  29. }
  30. public function getExpectedRuntime(): int {
  31. return 1;
  32. }
  33. public function getInputShapeEnumValues(): array {
  34. $coreL = $this->l10nFactory->getLanguages();
  35. $languages = array_merge($coreL['commonLanguages'], $coreL['otherLanguages']);
  36. $languageEnumValues = array_map(static function (array $language) {
  37. return new ShapeEnumValue($language['name'], $language['code']);
  38. }, $languages);
  39. $detectLanguageEnumValue = new ShapeEnumValue('Detect language', 'detect_language');
  40. return [
  41. 'origin_language' => array_merge([$detectLanguageEnumValue], $languageEnumValues),
  42. 'target_language' => $languageEnumValues,
  43. ];
  44. }
  45. public function getInputShapeDefaults(): array {
  46. return [
  47. 'origin_language' => 'detect_language',
  48. ];
  49. }
  50. public function getOptionalInputShape(): array {
  51. return [
  52. 'max_tokens' => new ShapeDescriptor(
  53. 'Maximum output words',
  54. 'The maximum number of words/tokens that can be generated in the completion.',
  55. EShapeType::Number
  56. ),
  57. 'model' => new ShapeDescriptor(
  58. 'Model',
  59. 'The model used to generate the completion',
  60. EShapeType::Enum
  61. ),
  62. ];
  63. }
  64. public function getOptionalInputShapeEnumValues(): array {
  65. return [
  66. 'model' => [
  67. new ShapeEnumValue('Model 1', 'model_1'),
  68. new ShapeEnumValue('Model 2', 'model_2'),
  69. new ShapeEnumValue('Model 3', 'model_3'),
  70. ],
  71. ];
  72. }
  73. public function getOptionalInputShapeDefaults(): array {
  74. return [
  75. 'max_tokens' => 200,
  76. 'model' => 'model_3',
  77. ];
  78. }
  79. public function getOptionalOutputShape(): array {
  80. return [];
  81. }
  82. public function getOutputShapeEnumValues(): array {
  83. return [];
  84. }
  85. public function getOptionalOutputShapeEnumValues(): array {
  86. return [];
  87. }
  88. private function getCoreLanguagesByCode(): array {
  89. $coreL = $this->l10nFactory->getLanguages();
  90. $coreLanguages = array_reduce(array_merge($coreL['commonLanguages'], $coreL['otherLanguages']), function ($carry, $val) {
  91. $carry[$val['code']] = $val['name'];
  92. return $carry;
  93. });
  94. return $coreLanguages;
  95. }
  96. public function process(?string $userId, array $input, callable $reportProgress): array {
  97. if (isset($input['model']) && is_string($input['model'])) {
  98. $model = $input['model'];
  99. } else {
  100. $model = 'model_3';
  101. }
  102. if (!isset($input['input']) || !is_string($input['input'])) {
  103. throw new RuntimeException('Invalid input text');
  104. }
  105. $inputText = $input['input'];
  106. $maxTokens = null;
  107. if (isset($input['max_tokens']) && is_int($input['max_tokens'])) {
  108. $maxTokens = $input['max_tokens'];
  109. }
  110. $coreLanguages = $this->getCoreLanguagesByCode();
  111. $toLanguage = $coreLanguages[$input['target_language']] ?? $input['target_language'];
  112. if ($input['origin_language'] !== 'detect_language') {
  113. $fromLanguage = $coreLanguages[$input['origin_language']] ?? $input['origin_language'];
  114. $prompt = 'Fake translation from ' . $fromLanguage . ' to ' . $toLanguage . ': ' . $inputText;
  115. } else {
  116. $prompt = 'Fake Translation to ' . $toLanguage . ': ' . $inputText;
  117. }
  118. $fakeResult = $prompt . "\n\nModel: " . $model . "\nMax tokens: " . $maxTokens;
  119. return ['output' => $fakeResult];
  120. }
  121. }