FakeContextWriteProvider.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\TaskProcessing\EShapeType;
  10. use OCP\TaskProcessing\ISynchronousProvider;
  11. use OCP\TaskProcessing\ShapeDescriptor;
  12. use OCP\TaskProcessing\ShapeEnumValue;
  13. use OCP\TaskProcessing\TaskTypes\ContextWrite;
  14. use RuntimeException;
  15. class FakeContextWriteProvider implements ISynchronousProvider {
  16. public function __construct() {
  17. }
  18. public function getId(): string {
  19. return Application::APP_ID . '-contextwrite';
  20. }
  21. public function getName(): string {
  22. return 'Fake context write task processing provider';
  23. }
  24. public function getTaskTypeId(): string {
  25. return ContextWrite::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' => 4321,
  62. 'model' => 'model_2',
  63. ];
  64. }
  65. public function getOutputShapeEnumValues(): array {
  66. return [];
  67. }
  68. public function getOptionalOutputShape(): 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 (
  76. !isset($input['style_input']) || !is_string($input['style_input'])
  77. || !isset($input['source_input']) || !is_string($input['source_input'])
  78. ) {
  79. throw new RuntimeException('Invalid inputs');
  80. }
  81. $writingStyle = $input['style_input'];
  82. $sourceMaterial = $input['source_input'];
  83. if (isset($input['model']) && is_string($input['model'])) {
  84. $model = $input['model'];
  85. } else {
  86. $model = 'unknown model';
  87. }
  88. $maxTokens = null;
  89. if (isset($input['max_tokens']) && is_int($input['max_tokens'])) {
  90. $maxTokens = $input['max_tokens'];
  91. }
  92. $fakeResult = 'This is a fake result: '
  93. . "\n\n- Style input: " . $writingStyle
  94. . "\n- Source input: " . $sourceMaterial
  95. . "\n- Model: " . $model
  96. . "\n- Maximum number of words: " . $maxTokens;
  97. return ['output' => $fakeResult];
  98. }
  99. }