1
0

FakeContextWriteProvider.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\ContextWrite;
  16. use RuntimeException;
  17. class FakeContextWriteProvider implements ISynchronousProvider {
  18. public function __construct(
  19. protected IAppConfig $appConfig,
  20. ) {
  21. }
  22. public function getId(): string {
  23. return Application::APP_ID . '-contextwrite';
  24. }
  25. public function getName(): string {
  26. return 'Fake context write task processing provider';
  27. }
  28. public function getTaskTypeId(): string {
  29. return ContextWrite::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' => 4321,
  66. 'model' => 'model_2',
  67. ];
  68. }
  69. public function getOutputShapeEnumValues(): array {
  70. return [];
  71. }
  72. public function getOptionalOutputShape(): 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 (
  83. !isset($input['style_input']) || !is_string($input['style_input'])
  84. || !isset($input['source_input']) || !is_string($input['source_input'])
  85. ) {
  86. throw new RuntimeException('Invalid inputs');
  87. }
  88. $writingStyle = $input['style_input'];
  89. $sourceMaterial = $input['source_input'];
  90. if (isset($input['model']) && is_string($input['model'])) {
  91. $model = $input['model'];
  92. } else {
  93. $model = 'unknown model';
  94. }
  95. $maxTokens = null;
  96. if (isset($input['max_tokens']) && is_int($input['max_tokens'])) {
  97. $maxTokens = $input['max_tokens'];
  98. }
  99. $fakeResult = 'This is a fake result: '
  100. . "\n\n- Style input: " . $writingStyle
  101. . "\n- Source input: " . $sourceMaterial
  102. . "\n- Model: " . $model
  103. . "\n- Maximum number of words: " . $maxTokens;
  104. return ['output' => $fakeResult];
  105. }
  106. }