diff options
author | Marcel Klehr <mklehr@gmx.net> | 2023-06-16 13:06:47 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2023-08-09 09:55:18 +0200 |
commit | d20ee42580086a59c2166f00f571047911e9b87f (patch) | |
tree | 5ad1e50ef03ead598a6ecf2061d1ce25683555bc /lib/private | |
parent | 457f1eb4075a01f42fef8df4ccffa8d7a4b5e826 (diff) | |
download | nextcloud-server-d20ee42580086a59c2166f00f571047911e9b87f.tar.gz nextcloud-server-d20ee42580086a59c2166f00f571047911e9b87f.zip |
LLM OCP API: Implement private backend code + add ILanguageModelTask
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
(cherry picked from commit 34138736538f604af2c6c52aa43662d1d66087d0)
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/LanguageModel/Db/Task.php | 58 | ||||
-rw-r--r-- | lib/private/LanguageModel/Db/TaskMapper.php | 34 | ||||
-rw-r--r-- | lib/private/LanguageModel/LanguageModelManager.php | 162 | ||||
-rw-r--r-- | lib/private/LanguageModel/TaskBackgroundJob.php | 72 |
4 files changed, 326 insertions, 0 deletions
diff --git a/lib/private/LanguageModel/Db/Task.php b/lib/private/LanguageModel/Db/Task.php new file mode 100644 index 00000000000..cee6c2fd8b9 --- /dev/null +++ b/lib/private/LanguageModel/Db/Task.php @@ -0,0 +1,58 @@ +<?php + +namespace OC\LanguageModel\Db; + +use OCP\AppFramework\Db\Entity; +use OCP\LanguageModel\ILanguageModelTask; + +/** + * @method setType(string $type) + * @method string getType() + * @method setInput(string $type) + * @method string getInput() + * @method setStatus(int $type) + * @method int getStatus() + * @method setUserId(string $type) + * @method string getuserId() + * @method setAppId(string $type) + * @method string getAppId() + */ +class Task extends Entity { + + protected $type; + protected $input; + protected $status; + protected $userId; + protected $appId; + + /** + * @var string[] + */ + public static array $columns = ['id', 'type', 'input', 'status', 'user_id', 'app_id']; + + /** + * @var string[] + */ + public static array $fields = ['id', 'type', 'input', 'status', 'userId', 'appId']; + + + public function __construct() { + // add types in constructor + $this->addType('id', 'integer'); + $this->addType('type', 'string'); + $this->addType('input', 'string'); + $this->addType('status', 'integer'); + $this->addType('userId', 'string'); + $this->addType('appId', 'string'); + } + + public static function fromLanguageModelTask(ILanguageModelTask $task): Task { + return Task::fromParams([ + 'type' => $task->getType(), + 'status' => ILanguageModelTask::STATUS_UNKNOWN, + 'input' => $task->getInput(), + 'userId' => $task->getUserId(), + 'appId' => $task->getAppId(), + ]); + } +} diff --git a/lib/private/LanguageModel/Db/TaskMapper.php b/lib/private/LanguageModel/Db/TaskMapper.php new file mode 100644 index 00000000000..0b9004c4d96 --- /dev/null +++ b/lib/private/LanguageModel/Db/TaskMapper.php @@ -0,0 +1,34 @@ +<?php + +namespace OC\LanguageModel\Db; + +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; +use OCP\AppFramework\Db\QBMapper; +use OCP\DB\Exception; +use OCP\IDBConnection; + +/** + * @extends QBMapper<Task> + */ +class TaskMapper extends QBMapper { + + public function __construct(IDBConnection $db) { + parent::__construct($db, 'oc_llm_tasks', Task::class); + } + + /** + * @param int $id + * @return Task + * @throws Exception + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + */ + public function find(int $id): Task { + $qb = $this->db->getQueryBuilder(); + $qb->select(Task::$columns) + ->from($this->tableName) + ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id))); + return $this->findEntity($qb); + } +} diff --git a/lib/private/LanguageModel/LanguageModelManager.php b/lib/private/LanguageModel/LanguageModelManager.php new file mode 100644 index 00000000000..f9f13b15d6e --- /dev/null +++ b/lib/private/LanguageModel/LanguageModelManager.php @@ -0,0 +1,162 @@ +<?php + +namespace OC\LanguageModel; + +use OC\AppFramework\Bootstrap\Coordinator; +use OC\LanguageModel\Db\Task; +use OC\LanguageModel\Db\TaskMapper; +use OCP\LanguageModel\AbstractLanguageModelTask; +use OCP\LanguageModel\FreePromptTask; +use OCP\LanguageModel\SummaryTask; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; +use OCP\BackgroundJob\IJobList; +use OCP\DB\Exception; +use OCP\IServerContainer; +use OCP\LanguageModel\ILanguageModelManager; +use OCP\LanguageModel\ILanguageModelProvider; +use OCP\LanguageModel\ILanguageModelTask; +use OCP\LanguageModel\ISummaryProvider; +use OCP\PreConditionNotMetException; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use Psr\Log\LoggerInterface; +use RuntimeException; +use Throwable; + +class LanguageModelManager implements ILanguageModelManager { + + /** @var ?ILanguageModelProvider[] */ + private ?array $providers = null; + + public function __construct( + private IServerContainer $serverContainer, + private Coordinator $coordinator, + private LoggerInterface $logger, + private IJobList $jobList, + private TaskMapper $taskMapper, + ) { + } + + public function getProviders(): array { + $context = $this->coordinator->getRegistrationContext(); + if ($context === null) { + return []; + } + + if ($this->providers !== null) { + return $this->providers; + } + + $this->providers = []; + + foreach ($context->getSpeechToTextProviders() as $providerServiceRegistration) { + $class = $providerServiceRegistration->getService(); + try { + $this->providers[$class] = $this->serverContainer->get($class); + } catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) { + $this->logger->error('Failed to load LanguageModel provider ' . $class, [ + 'exception' => $e, + ]); + } + } + + return $this->providers; + } + + public function hasProviders(): bool { + $context = $this->coordinator->getRegistrationContext(); + if ($context === null) { + return false; + } + return !empty($context->getSpeechToTextProviders()); + } + + /** + * @inheritDoc + */ + public function getAvailableTasks(): array { + $tasks = []; + foreach ($this->getProviders() as $provider) { + $tasks[FreePromptTask::class] = true; + if ($provider instanceof ISummaryProvider) { + $tasks[SummaryTask::class] = true; + } + } + return array_keys($tasks); + } + + public function canHandleTask(ILanguageModelTask $task): bool { + return !empty(array_filter($this->getAvailableTasks(), fn ($class) => $task instanceof $class)); + } + + /** + * @inheritDoc + */ + public function runTask(ILanguageModelTask $task): string { + if (!$this->canHandleTask($task)) { + throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task'); + } + foreach ($this->getProviders() as $provider) { + if (!$task->canUseProvider($provider)) { + continue; + } + try { + $task->setStatus(ILanguageModelTask::STATUS_RUNNING); + $this->taskMapper->update(Task::fromLanguageModelTask($task)); + $output = $task->visitProvider($provider); + $task->setStatus(ILanguageModelTask::STATUS_SUCCESSFUL); + $this->taskMapper->update(Task::fromLanguageModelTask($task)); + return $output; + } catch (\RuntimeException $e) { + $this->logger->info('LanguageModel call using provider ' . $provider->getName() . ' failed', ['exception' => $e]); + $task->setStatus(ILanguageModelTask::STATUS_FAILED); + $this->taskMapper->update(Task::fromLanguageModelTask($task)); + throw $e; + } catch (\Throwable $e) { + $this->logger->info('LanguageModel call using provider ' . $provider->getName() . ' failed', ['exception' => $e]); + $task->setStatus(ILanguageModelTask::STATUS_FAILED); + $this->taskMapper->update(Task::fromLanguageModelTask($task)); + throw new RuntimeException('LanguageModel call using provider ' . $provider->getName() . ' failed: ' . $e->getMessage()); + } + } + + throw new RuntimeException('Could not transcribe file'); + } + + /** + * @inheritDoc + * @throws Exception + */ + public function scheduleTask(ILanguageModelTask $task): void { + if (!$this->canHandleTask($task)) { + throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task'); + } + $taskEntity = Task::fromLanguageModelTask($task); + $this->taskMapper->insert($taskEntity); + $task->setId($taskEntity->getId()); + $task->setStatus(ILanguageModelTask::STATUS_SCHEDULED); + $this->jobList->add(TaskBackgroundJob::class, [ + 'taskId' => $task->getId() + ]); + } + + /** + * @param int $id The id of the task + * @return ILanguageModelTask + * @throws RuntimeException If the query failed + * @throws \ValueError If the task could not be found + */ + public function getTask(int $id): ILanguageModelTask { + try { + $taskEntity = $this->taskMapper->find($id); + return AbstractLanguageModelTask::fromTaskEntity($taskEntity); + } catch (DoesNotExistException $e) { + throw new \ValueError('Could not find task with the provided id'); + } catch (MultipleObjectsReturnedException $e) { + throw new RuntimeException('Could not uniquely identify task with given id'); + } catch (Exception $e) { + throw new RuntimeException('Failure while trying to find task by id: '.$e->getMessage()); + } + } +} diff --git a/lib/private/LanguageModel/TaskBackgroundJob.php b/lib/private/LanguageModel/TaskBackgroundJob.php new file mode 100644 index 00000000000..55413ba3714 --- /dev/null +++ b/lib/private/LanguageModel/TaskBackgroundJob.php @@ -0,0 +1,72 @@ +<?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\LanguageModel; + +use OC\User\NoUserException; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\QueuedJob; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\File; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\LanguageModel\Events\TaskFailedEvent; +use OCP\LanguageModel\Events\TaskSuccessfulEvent; +use OCP\LanguageModel\ILanguageModelManager; +use OCP\PreConditionNotMetException; +use OCP\SpeechToText\Events\TranscriptionFailedEvent; +use OCP\SpeechToText\Events\TranscriptionSuccessfulEvent; +use OCP\SpeechToText\ISpeechToTextManager; +use Psr\Log\LoggerInterface; + +class TaskBackgroundJob extends QueuedJob { + public function __construct( + ITimeFactory $timeFactory, + private ILanguageModelManager $languageModelManager, + private IEventDispatcher $eventDispatcher, + ) { + parent::__construct($timeFactory); + $this->setAllowParallelRuns(false); + } + + /** + * @param array{taskId: int} $argument + * @inheritDoc + */ + protected function run($argument) { + $taskId = $argument['taskId']; + $task = $this->languageModelManager->getTask($taskId); + try { + $output = $this->languageModelManager->runTask($task); + $event = new TaskSuccessfulEvent($task, $output); + + } catch (\RuntimeException|PreConditionNotMetException $e) { + $event = new TaskFailedEvent($task, $e->getMessage()); + } + $this->eventDispatcher->dispatchTyped($event); + } +} |