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