Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(TextProcessing): Add two new provider interfaces #41271

Merged
merged 16 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions core/Controller/TextProcessingApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCP\AppFramework\Http\Attribute\UserRateLimit;
use OCP\AppFramework\Http\DataResponse;
use OCP\Common\Exception\NotFoundException;
use OCP\DB\Exception;
use OCP\IL10N;
use OCP\IRequest;
use OCP\TextProcessing\ITaskType;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function taskTypes(): DataResponse {
* @param string $appId ID of the app that will execute the task
* @param string $identifier An arbitrary identifier for the task
*
* @return DataResponse<Http::STATUS_OK, array{task: CoreTextProcessingTask}, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_PRECONDITION_FAILED, array{message: string}, array{}>
* @return DataResponse<Http::STATUS_OK, array{task: CoreTextProcessingTask}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_BAD_REQUEST|Http::STATUS_PRECONDITION_FAILED, array{message: string}, array{}>
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
*
* 200: Task scheduled successfully
* 400: Scheduling task is not possible
Expand All @@ -118,7 +119,11 @@ public function schedule(string $input, string $type, string $appId, string $ide
return new DataResponse(['message' => $this->l->t('Requested task type does not exist')], Http::STATUS_BAD_REQUEST);
}
try {
$this->textProcessingManager->scheduleTask($task);
try {
$this->textProcessingManager->runOrScheduleTask($task);
} catch(\RuntimeException) {
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
// noop, because the task object has the failure status set already, we just return the task json
}

$json = $task->jsonSerialize();

Expand All @@ -127,6 +132,8 @@ public function schedule(string $input, string $type, string $appId, string $ide
]);
} catch (PreConditionNotMetException) {
return new DataResponse(['message' => $this->l->t('Necessary language model provider is not available')], Http::STATUS_PRECONDITION_FAILED);
} catch (Exception) {
return new DataResponse(['message' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}

Expand Down
60 changes: 60 additions & 0 deletions core/Migrations/Version28000Date20231103104802.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
*
* @author Marcel Klehr <mklehr@gmx.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Core\Migrations;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* Introduce completion_expected_at column in textprocessing_tasks table
*/
class Version28000Date20231103104802 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
if ($schema->hasTable('textprocessing_tasks')) {
$table = $schema->getTable('textprocessing_tasks');

$table->addColumn('completion_expected_at', Types::DATETIME, [
'notnull' => false,
]);
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved

return $schema;
}

return null;
}
}
1 change: 1 addition & 0 deletions core/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
* input: string,
* output: ?string,
* identifier: string,
* completionExpectedAt: ?int
* }
*
* @psalm-type CoreTextToImageTask = array{
Expand Down
10 changes: 8 additions & 2 deletions lib/private/TextProcessing/Db/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
* @method string getAppId()
* @method setIdentifier(string $identifier)
* @method string getIdentifier()
* @method setCompletionExpectedAt(null|\DateTime $completionExpectedAt)
* @method null|\DateTime getCompletionExpectedAt()
*/
class Task extends Entity {
protected $lastUpdated;
Expand All @@ -55,16 +57,17 @@ class Task extends Entity {
protected $userId;
protected $appId;
protected $identifier;
protected $completionExpectedAt;

/**
* @var string[]
*/
public static array $columns = ['id', 'last_updated', 'type', 'input', 'output', 'status', 'user_id', 'app_id', 'identifier'];
public static array $columns = ['id', 'last_updated', 'type', 'input', 'output', 'status', 'user_id', 'app_id', 'identifier', 'completion_expected_at'];

/**
* @var string[]
*/
public static array $fields = ['id', 'lastUpdated', 'type', 'input', 'output', 'status', 'userId', 'appId', 'identifier'];
public static array $fields = ['id', 'lastUpdated', 'type', 'input', 'output', 'status', 'userId', 'appId', 'identifier', 'completionExpectedAt'];


public function __construct() {
Expand All @@ -78,6 +81,7 @@ public function __construct() {
$this->addType('userId', 'string');
$this->addType('appId', 'string');
$this->addType('identifier', 'string');
$this->addType('completionExpectedAt', 'datetime');
}

public function toRow(): array {
Expand All @@ -98,6 +102,7 @@ public static function fromPublicTask(OCPTask $task): Task {
'userId' => $task->getUserId(),
'appId' => $task->getAppId(),
'identifier' => $task->getIdentifier(),
'completionExpectedAt' => $task->getCompletionExpectedAt(),
]);
return $task;
}
Expand All @@ -107,6 +112,7 @@ public function toPublicTask(): OCPTask {
$task->setId($this->getId());
$task->setStatus($this->getStatus());
$task->setOutput($this->getOutput());
$task->setCompletionExpectedAt($this->getCompletionExpectedAt());
return $task;
}
}
69 changes: 55 additions & 14 deletions lib/private/TextProcessing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OC\AppFramework\Bootstrap\Coordinator;
use OC\TextProcessing\Db\Task as DbTask;
use OCP\IConfig;
use OCP\TextProcessing\IProvider2;
use OCP\TextProcessing\Task;
use OCP\TextProcessing\Task as OCPTask;
use OC\TextProcessing\Db\TaskMapper;
Expand Down Expand Up @@ -114,26 +115,19 @@ public function runTask(OCPTask $task): string {
if (!$this->canHandleTask($task)) {
throw new PreConditionNotMetException('No text processing provider is installed that can handle this task');
}
$providers = $this->getProviders();
$json = $this->config->getAppValue('core', 'ai.textprocessing_provider_preferences', '');
if ($json !== '') {
$preferences = json_decode($json, true);
if (isset($preferences[$task->getType()])) {
// If a preference for this task type is set, move the preferred provider to the start
$provider = current(array_filter($providers, fn ($provider) => $provider::class === $preferences[$task->getType()]));
if ($provider !== false) {
$providers = array_filter($providers, fn ($p) => $p !== $provider);
array_unshift($providers, $provider);
}
}
}
$providers = $this->getPreferredProviders($task);

foreach ($providers as $provider) {
if (!$task->canUseProvider($provider)) {
continue;
}
try {
$task->setStatus(OCPTask::STATUS_RUNNING);
if ($provider instanceof IProvider2) {
$completionExpectedAt = new \DateTime('now');
$completionExpectedAt->add(new \DateInterval('PT'.$provider->getExpectedRuntime().'S'));
$task->setCompletionExpectedAt($completionExpectedAt);
}
if ($task->getId() === null) {
$taskEntity = $this->taskMapper->insert(DbTask::fromPublicTask($task));
$task->setId($taskEntity->getId());
Expand All @@ -158,18 +152,25 @@ public function runTask(OCPTask $task): string {
}
}

$task->setStatus(OCPTask::STATUS_FAILED);
$this->taskMapper->update(DbTask::fromPublicTask($task));
throw new RuntimeException('Could not run task');
}

/**
* @inheritDoc
* @throws Exception
*/
public function scheduleTask(OCPTask $task): void {
if (!$this->canHandleTask($task)) {
throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task');
}
$task->setStatus(OCPTask::STATUS_SCHEDULED);
[$provider, ] = $this->getPreferredProviders($task);
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
if ($provider instanceof IProvider2) {
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
$completionExpectedAt = new \DateTime('now');
$completionExpectedAt->add(new \DateInterval('PT'.$provider->getExpectedRuntime().'S'));
$task->setCompletionExpectedAt($completionExpectedAt);
}
$taskEntity = DbTask::fromPublicTask($task);
$this->taskMapper->insert($taskEntity);
$task->setId($taskEntity->getId());
Expand All @@ -178,6 +179,25 @@ public function scheduleTask(OCPTask $task): void {
]);
}

/**
* @inheritDoc
*/
public function runOrScheduleTask(OCPTask $task) : bool {
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
if (!$this->canHandleTask($task)) {
throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task');
}
[$provider,] = $this->getPreferredProviders($task);
$maxExecutionTime = (int) ini_get('max_execution_time');
// Offload the task to a background job if the expected runtime of the likely provider is longer than 80% of our max execution time
// or if the provider doesn't provide a getExpectedRuntime() method
if (!$provider instanceof IProvider2 || $provider->getExpectedRuntime() > $maxExecutionTime * 0.8) {
$this->scheduleTask($task);
return false;
}
$this->runTask($task);
return true;
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -253,4 +273,25 @@ public function getUserTasksByApp(string $userId, string $appId, ?string $identi
throw new RuntimeException('Failure while trying to find tasks by appId and identifier: ' . $e->getMessage(), 0, $e);
}
}

/**
* @param OCPTask $task
* @return IProvider[]
*/
public function getPreferredProviders(OCPTask $task): array {
$providers = $this->getProviders();
$json = $this->config->getAppValue('core', 'ai.textprocessing_provider_preferences', '');
if ($json !== '') {
$preferences = json_decode($json, true);
if (isset($preferences[$task->getType()])) {
// If a preference for this task type is set, move the preferred provider to the start
$provider = current(array_filter($providers, fn ($provider) => $provider::class === $preferences[$task->getType()]));
if ($provider !== false) {
$providers = array_filter($providers, fn ($p) => $p !== $provider);
array_unshift($providers, $provider);
}
}
}
return $providers;
}
}
16 changes: 16 additions & 0 deletions lib/public/TextProcessing/IManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OCP\TextProcessing;

use OCP\Common\Exception\NotFoundException;
use OCP\DB\Exception;
use OCP\PreConditionNotMetException;
use RuntimeException;

Expand Down Expand Up @@ -68,10 +69,25 @@ public function runTask(Task $task): string;
*
* @param Task $task The task to schedule
* @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
* @throws Exception storing the task in the database failed
* @since 27.1.0
*/
public function scheduleTask(Task $task) : void;

/**
* If the designated provider for the passed task provides an expected average runtime, we check if the runtime fits into the
* max execution time of this php process and run it synchronously if it does, if it doesn't fit (or the provider doesn't provide that information)
* execution is deferred to a background job
*
* @param Task $task The task to schedule
* @returns bool A boolean indicating whether the task was run synchronously (`true`) or offloaded to a background job (`false`)
* @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
* @throws RuntimeException If running the task failed
* @throws Exception storing the task in the database failed
* @since 28.0.0
*/
public function runOrScheduleTask(Task $task): bool;

/**
* Delete a task that has been scheduled before
*
Expand Down
48 changes: 48 additions & 0 deletions lib/public/TextProcessing/IProvider2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
*
* @author Marcel Klehr <mklehr@gmx.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


namespace OCP\TextProcessing;

/**
* This interface supersedes IProvider. It allows the system to learn
* the provider's expected runtime and lets the provider know which user is running a task
* @since 28.0.0
* @template T of ITaskType
* @template-extends IProvider<T>
*/
interface IProvider2 extends IProvider {
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param ?string $userId the current user's id
* @since 28.0.0
*/
public function setUserId(?string $userId): string;

/**
* @return int The expected average runtime of a task in seconds
* @since 28.0.0
*/
public function getExpectedRuntime(): int;
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
}
Loading
Loading