FakeTextToTextProvider.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\TaskProcessing\EShapeType;
  11. use OCP\TaskProcessing\Exception\ProcessingException;
  12. use OCP\TaskProcessing\ISynchronousProvider;
  13. use OCP\TaskProcessing\ShapeDescriptor;
  14. use OCP\TaskProcessing\ShapeEnumValue;
  15. use OCP\TaskProcessing\TaskTypes\TextToText;
  16. use RuntimeException;
  17. class FakeTextToTextProvider implements ISynchronousProvider {
  18. public function __construct(
  19. protected IAppConfig $appConfig,
  20. ) {
  21. }
  22. public function getId(): string {
  23. return Application::APP_ID . '-text2text';
  24. }
  25. public function getName(): string {
  26. return 'Fake text2text task processing provider';
  27. }
  28. public function getTaskTypeId(): string {
  29. return TextToText::ID;
  30. }
  31. public function getExpectedRuntime(): int {
  32. return 1;
  33. }
  34. public function getInputShapeEnumValues(): array {
  35. return [];
  36. }
  37. public function getInputShapeDefaults(): array {
  38. return [];
  39. }
  40. public function getOptionalInputShape(): array {
  41. return [
  42. 'max_tokens' => new ShapeDescriptor(
  43. 'Maximum output words',
  44. 'The maximum number of words/tokens that can be generated in the completion.',
  45. EShapeType::Number
  46. ),
  47. 'model' => new ShapeDescriptor(
  48. 'Model',
  49. 'The model used to generate the completion',
  50. EShapeType::Enum
  51. ),
  52. ];
  53. }
  54. public function getOptionalInputShapeEnumValues(): array {
  55. return [
  56. 'model' => [
  57. new ShapeEnumValue('Model 1', 'model_1'),
  58. new ShapeEnumValue('Model 2', 'model_2'),
  59. new ShapeEnumValue('Model 3', 'model_3'),
  60. ],
  61. ];
  62. }
  63. public function getOptionalInputShapeDefaults(): array {
  64. return [
  65. 'max_tokens' => 1234,
  66. 'model' => 'model_2',
  67. ];
  68. }
  69. public function getOptionalOutputShape(): array {
  70. return [];
  71. }
  72. public function getOutputShapeEnumValues(): array {
  73. return [];
  74. }
  75. public function getOptionalOutputShapeEnumValues(): array {
  76. return [];
  77. }
  78. public function process(?string $userId, array $input, callable $reportProgress): array {
  79. if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) {
  80. throw new ProcessingException('Failing as set by AppConfig');
  81. }
  82. if (isset($input['model']) && is_string($input['model'])) {
  83. $model = $input['model'];
  84. } else {
  85. $model = 'unknown model';
  86. }
  87. if (!isset($input['input']) || !is_string($input['input'])) {
  88. throw new RuntimeException('Invalid prompt');
  89. }
  90. $prompt = $input['input'];
  91. $maxTokens = null;
  92. if (isset($input['max_tokens']) && is_int($input['max_tokens'])) {
  93. $maxTokens = $input['max_tokens'];
  94. }
  95. return [
  96. 'output' => 'This is a fake result: ' . "\n\n- Prompt: " . $prompt . "\n- Model: " . $model . "\n- Maximum number of words: " . $maxTokens,
  97. ];
  98. }
  99. }