FakeTranslateProvider.php 4.2 KB

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