]> source.dussan.org Git - nextcloud-server.git/commitdiff
LLM OCP API: Implement ocs API
authorMarcel Klehr <mklehr@gmx.net>
Tue, 20 Jun 2023 12:41:58 +0000 (14:41 +0200)
committerMarcel Klehr <mklehr@gmx.net>
Wed, 9 Aug 2023 07:59:58 +0000 (09:59 +0200)
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
(cherry picked from commit 795b097122a8dd70b4d6b9ebe044440396be9104)

core/Controller/LanguageModelApiController.php [new file with mode: 0644]
core/Migrations/Version28000Date20230616104802.php
core/routes.php
lib/private/LanguageModel/Db/Task.php
lib/private/LanguageModel/LanguageModelManager.php
lib/private/LanguageModel/TaskBackgroundJob.php
lib/private/Server.php
lib/public/AppFramework/Bootstrap/IRegistrationContext.php
lib/public/LanguageModel/AbstractLanguageModelTask.php
lib/public/LanguageModel/Events/TaskSuccessfulEvent.php
lib/public/LanguageModel/ILanguageModelManager.php

diff --git a/core/Controller/LanguageModelApiController.php b/core/Controller/LanguageModelApiController.php
new file mode 100644 (file)
index 0000000..5699dd7
--- /dev/null
@@ -0,0 +1,96 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
+ *
+ * @author Julius Härtl <jus@bitgrid.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\Controller;
+
+use InvalidArgumentException;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\IL10N;
+use OCP\IRequest;
+use OCP\LanguageModel\AbstractLanguageModelTask;
+use OCP\LanguageModel\ILanguageModelManager;
+use OCP\PreConditionNotMetException;
+
+class LanguageModelApiController extends \OCP\AppFramework\OCSController {
+       public function __construct(
+               string $appName,
+               IRequest $request,
+               private ILanguageModelManager $languageModelManager,
+               private IL10N $l,
+               private ?string $userId,
+       ) {
+               parent::__construct($appName, $request);
+       }
+
+       /**
+        * @PublicPage
+        */
+       public function tasks(): DataResponse {
+               return new DataResponse([
+                       'tasks' => $this->languageModelManager->getAvailableTaskTypes(),
+               ]);
+       }
+
+       /**
+        * @PublicPage
+        * @UserRateThrottle(limit=20, period=120)
+        * @AnonRateThrottle(limit=5, period=120)
+        */
+       public function schedule(string $text, string $type, ?string $appId): DataResponse {
+               try {
+                       $task = AbstractLanguageModelTask::factory($type, $text, $this->userId, $appId);
+               } catch (InvalidArgumentException $e) {
+                       return new DataResponse(['message' => $this->l->t('Requested task type does not exist')], Http::STATUS_BAD_REQUEST);
+               }
+               try {
+                       $this->languageModelManager->scheduleTask($task);
+
+                       return new DataResponse([
+                               'task' => $task,
+                       ]);
+               } catch (PreConditionNotMetException) {
+                       return new DataResponse(['message' => $this->l->t('Necessary language model provider is not available')], Http::STATUS_PRECONDITION_FAILED);
+               }
+       }
+
+       /**
+        * @PublicPage
+        */
+       public function getTask(int $id): DataResponse {
+               try {
+                       $task = $this->languageModelManager->getTask($id);
+
+                       return new DataResponse([
+                               'task' => $task,
+                       ]);
+               } catch (\ValueError $e) {
+                       return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
+               } catch (\RuntimeException $e) {
+                       return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
+               }
+       }
+}
index ffb2b38f57e2f39c26639cedba3b3b41dfa17ce3..76d8173861f41a7624e60b0a82b26e9fc1889c61 100644 (file)
@@ -61,6 +61,9 @@ class Version28000Date20230616104802 extends SimpleMigrationStep {
                        $table->addColumn('input', Types::TEXT, [
                                'notnull' => true,
                        ]);
+                       $table->addColumn('output', Types::TEXT, [
+                               'notnull' => false,
+                       ]);
                        $table->addColumn('status', Types::INTEGER, [
                                'notnull' => false,
                                'length' => 6,
index 0f9729e54eb09103e951654f548af3b70e7796ad..20be6ef63f4561aed88184b292b8662fd228f08e 100644 (file)
@@ -145,6 +145,10 @@ $application->registerRoutes($this, [
 
                ['root' => '/translation', 'name' => 'TranslationApi#languages', 'url' => '/languages', 'verb' => 'GET'],
                ['root' => '/translation', 'name' => 'TranslationApi#translate', 'url' => '/translate', 'verb' => 'POST'],
+
+               ['root' => '/languagemodel', 'name' => 'LanguageModelApi#tasks', 'url' => '/tasks', 'verb' => 'GET'],
+               ['root' => '/languagemodel', 'name' => 'LanguageModelApi#schedule', 'url' => '/schedule', 'verb' => 'POST'],
+               ['root' => '/languagemodel', 'name' => 'LanguageModelApi#getTask', 'url' => '/task/{id}', 'verb' => 'GET'],
        ],
 ]);
 
index cee6c2fd8b9c8ba16eb0c61f3d914ebef1f78bd7..e5d2f79e45383d2cd6baafba5ac0b95360b83e9c 100644 (file)
@@ -28,12 +28,12 @@ class Task extends Entity {
        /**
         * @var string[]
         */
-       public static array $columns = ['id', 'type', 'input', 'status', 'user_id', 'app_id'];
+       public static array $columns = ['id', 'type', 'input', 'output', 'status', 'user_id', 'app_id'];
 
        /**
         * @var string[]
         */
-       public static array $fields = ['id', 'type', 'input', 'status', 'userId', 'appId'];
+       public static array $fields = ['id', 'type', 'input', 'output', 'status', 'userId', 'appId'];
 
 
        public function __construct() {
@@ -49,8 +49,9 @@ class Task extends Entity {
        public static function fromLanguageModelTask(ILanguageModelTask $task): Task {
                return Task::fromParams([
                        'type' => $task->getType(),
-                       'status' => ILanguageModelTask::STATUS_UNKNOWN,
+                       'status' => $task->getStatus(),
                        'input' => $task->getInput(),
+                       'output' => $task->getOutput(),
                        'userId' => $task->getUserId(),
                        'appId' => $task->getAppId(),
                ]);
index b0e45f5812ab4dae63efcae3786d8cc1b6b7a1b3..7db2e656a0a9b06071315f74f2a52acd19007233 100644 (file)
@@ -86,6 +86,13 @@ class LanguageModelManager implements ILanguageModelManager {
                return array_keys($tasks);
        }
 
+       /**
+        * @inheritDoc
+        */
+       public function getAvailableTaskTypes(): array {
+               return array_map(fn ($taskClass) => $taskClass::TYPE, $this->getAvailableTasks());
+       }
+
        public function canHandleTask(ILanguageModelTask $task): bool {
                return !empty(array_filter($this->getAvailableTasks(), fn ($class) => $task instanceof $class));
        }
@@ -104,10 +111,10 @@ class LanguageModelManager implements ILanguageModelManager {
                        try {
                                $task->setStatus(ILanguageModelTask::STATUS_RUNNING);
                                $this->taskMapper->update(Task::fromLanguageModelTask($task));
-                               $output = $task->visitProvider($provider);
+                               $task->setOutput($task->visitProvider($provider));
                                $task->setStatus(ILanguageModelTask::STATUS_SUCCESSFUL);
                                $this->taskMapper->update(Task::fromLanguageModelTask($task));
-                               return $output;
+                               return $task->getOutput();
                        } catch (\RuntimeException $e) {
                                $this->logger->info('LanguageModel call using provider ' . $provider->getName() . ' failed', ['exception' => $e]);
                                $task->setStatus(ILanguageModelTask::STATUS_FAILED);
index 55413ba3714715c4fe8cc5f4af9c7941b2f6aade..3c18ff0310225ede19afe24550fb09a52d4c53cf 100644 (file)
@@ -61,8 +61,8 @@ class TaskBackgroundJob extends QueuedJob {
                $taskId = $argument['taskId'];
                $task = $this->languageModelManager->getTask($taskId);
                try {
-                       $output = $this->languageModelManager->runTask($task);
-                       $event = new TaskSuccessfulEvent($task, $output);
+                       $this->languageModelManager->runTask($task);
+                       $event = new TaskSuccessfulEvent($task);
 
                } catch (\RuntimeException|PreConditionNotMetException $e) {
                        $event = new TaskFailedEvent($task, $e->getMessage());
index bb4e217efa3bb1b77799a0c1d84656f5756cf7fe..d1f18a1235f03c23471450074c6eaace61376860 100644 (file)
@@ -110,6 +110,7 @@ use OC\IntegrityCheck\Checker;
 use OC\IntegrityCheck\Helpers\AppLocator;
 use OC\IntegrityCheck\Helpers\EnvironmentHelper;
 use OC\IntegrityCheck\Helpers\FileAccessHelper;
+use OC\LanguageModel\LanguageModelManager;
 use OC\LDAP\NullLDAPProviderFactory;
 use OC\KnownUser\KnownUserService;
 use OC\Lock\DBLockingProvider;
@@ -228,6 +229,7 @@ use OCP\IURLGenerator;
 use OCP\IUserManager;
 use OCP\IUserSession;
 use OCP\L10N\IFactory;
+use OCP\LanguageModel\ILanguageModelManager;
 use OCP\LDAP\ILDAPProvider;
 use OCP\LDAP\ILDAPProviderFactory;
 use OCP\Lock\ILockingProvider;
@@ -1461,6 +1463,8 @@ class Server extends ServerContainer implements IServerContainer {
 
                $this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class);
 
+               $this->registerAlias(ILanguageModelManager::class, LanguageModelManager::class);
+
                $this->connectDispatcher();
        }
 
index 66cf1ef23063ad82a0cb1845b7ab4287c7b96919..19ef6832a2c2c3a07e2c7e44595d86de2b274af8 100644 (file)
@@ -37,6 +37,7 @@ use OCP\Collaboration\Reference\IReferenceProvider;
 use OCP\EventDispatcher\IEventDispatcher;
 use OCP\Files\Template\ICustomTemplateProvider;
 use OCP\IContainer;
+use OCP\LanguageModel\ILanguageModelProvider;
 use OCP\Notification\INotifier;
 use OCP\Preview\IProviderV2;
 use OCP\SpeechToText\ISpeechToTextProvider;
@@ -219,6 +220,16 @@ interface IRegistrationContext {
         */
        public function registerSpeechToTextProvider(string $providerClass): void;
 
+       /**
+        * Register a custom LanguageModel provider class that provides a promptable language model
+        * through the OCP\LanguageModel APIs
+        *
+        * @param string $providerClass
+        * @psalm-param class-string<ILanguageModelProvider> $providerClass
+        * @since 27.0.0
+        */
+       public function registerLanguageModelProvider(string $providerClass): void;
+
        /**
         * Register a custom template provider class that is able to inject custom templates
         * in addition to the user defined ones
index 12aedc95fe56e779221a08025d16a570d159c04e..63b6396fb437a6bfe7097ac9a320dce00df506b6 100644 (file)
@@ -76,6 +76,19 @@ abstract class AbstractLanguageModelTask implements ILanguageModelTask {
                return $this->userId;
        }
 
+       public function jsonSerialize() {
+               return [
+                       'id' => $this->getId(),
+                       'type' => $this->getType(),
+                       'status' => $this->getStatus(),
+                       'userId' => $this->getUserId(),
+                       'appId' => $this->getAppId(),
+                       'input' => $this->getInput(),
+                       'output' => $this->getOutput(),
+               ];
+       }
+
+
        public final static function fromTaskEntity(Task $taskEntity): ILanguageModelTask  {
                $task = self::factory($taskEntity->getType(), $taskEntity->getInput(), $taskEntity->getuserId(), $taskEntity->getAppId());
                $task->setId($taskEntity->getId());
@@ -83,6 +96,14 @@ abstract class AbstractLanguageModelTask implements ILanguageModelTask {
                return $task;
        }
 
+       /**
+        * @param string $type
+        * @param string $input
+        * @param string|null $userId
+        * @param string $appId
+        * @return ILanguageModelTask
+        * @throws \InvalidArgumentException
+        */
        public final static function factory(string $type, string $input, ?string $userId, string $appId): ILanguageModelTask {
                if (!in_array($type, self::TYPES)) {
                        throw new \InvalidArgumentException('Unknown task type');
index 156c5679e0beb74249b1c095940be8f67586be79..61be3a20cd172fbc0286262b4213e8148ab0bea9 100644 (file)
@@ -9,15 +9,7 @@ use OCP\LanguageModel\ILanguageModelTask;
  */
 class TaskSuccessfulEvent extends AbstractLanguageModelEvent {
 
-       public function __construct(ILanguageModelTask $task,
-                                                               private string $output) {
+       public function __construct(ILanguageModelTask $task) {
                parent::__construct($task);
        }
-
-       /**
-        * @return string
-        */
-       public function getErrorMessage(): string {
-               return $this->output;
-       }
 }
index a4d3079c180b3bd780dcea540e9342a418f14487..439cfb761764f76d824566bb8bd2fcb4fb52ef90 100644 (file)
@@ -44,6 +44,12 @@ interface ILanguageModelManager {
         */
        public function getAvailableTasks(): array;
 
+       /**
+        * @return string[]
+        * @since 28.0.0
+        */
+       public function getAvailableTaskTypes(): array;
+
        /**
         * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
         * @throws RuntimeException If something else failed
@@ -60,5 +66,11 @@ interface ILanguageModelManager {
         */
        public function scheduleTask(ILanguageModelTask $task) : void;
 
+       /**
+        * @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;
 }