FakeTextToTextSummaryProvider.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Testing\TaskProcessing;
  8. use OCA\Testing\AppInfo\Application;
  9. use OCP\TaskProcessing\EShapeType;
  10. use OCP\TaskProcessing\ISynchronousProvider;
  11. use OCP\TaskProcessing\ShapeDescriptor;
  12. use OCP\TaskProcessing\ShapeEnumValue;
  13. use OCP\TaskProcessing\TaskTypes\TextToTextSummary;
  14. use RuntimeException;
  15. class FakeTextToTextSummaryProvider implements ISynchronousProvider {
  16. public function __construct() {
  17. }
  18. public function getId(): string {
  19. return Application::APP_ID . '-text2text-summary';
  20. }
  21. public function getName(): string {
  22. return 'Fake text2text summary task processing provider';
  23. }
  24. public function getTaskTypeId(): string {
  25. return TextToTextSummary::ID;
  26. }
  27. public function getExpectedRuntime(): int {
  28. return 1;
  29. }
  30. public function getInputShapeEnumValues(): array {
  31. return [];
  32. }
  33. public function getInputShapeDefaults(): array {
  34. return [];
  35. }
  36. public function getOptionalInputShape(): array {
  37. return [
  38. 'max_tokens' => new ShapeDescriptor(
  39. 'Maximum output words',
  40. 'The maximum number of words/tokens that can be generated in the completion.',
  41. EShapeType::Number
  42. ),
  43. 'model' => new ShapeDescriptor(
  44. 'Model',
  45. 'The model used to generate the completion',
  46. EShapeType::Enum
  47. ),
  48. ];
  49. }
  50. public function getOptionalInputShapeEnumValues(): array {
  51. return [
  52. 'model' => [
  53. new ShapeEnumValue('Model 1', 'model_1'),
  54. new ShapeEnumValue('Model 2', 'model_2'),
  55. new ShapeEnumValue('Model 3', 'model_3'),
  56. ],
  57. ];
  58. }
  59. public function getOptionalInputShapeDefaults(): array {
  60. return [
  61. 'max_tokens' => 1234,
  62. 'model' => 'model_2',
  63. ];
  64. }
  65. public function getOptionalOutputShape(): array {
  66. return [];
  67. }
  68. public function getOutputShapeEnumValues(): array {
  69. return [];
  70. }
  71. public function getOptionalOutputShapeEnumValues(): array {
  72. return [];
  73. }
  74. public function process(?string $userId, array $input, callable $reportProgress): array {
  75. if (isset($input['model']) && is_string($input['model'])) {
  76. $model = $input['model'];
  77. } else {
  78. $model = 'unknown model';
  79. }
  80. if (!isset($input['input']) || !is_string($input['input'])) {
  81. throw new RuntimeException('Invalid prompt');
  82. }
  83. $prompt = $input['input'];
  84. $maxTokens = null;
  85. if (isset($input['max_tokens']) && is_int($input['max_tokens'])) {
  86. $maxTokens = $input['max_tokens'];
  87. }
  88. return [
  89. 'output' => 'This is a fake summary: ',// . "\n\n- Prompt: " . $prompt . "\n- Model: " . $model . "\n- Maximum number of words: " . $maxTokens,
  90. ];
  91. }
  92. }