summaryrefslogtreecommitdiffstats
path: root/core/Controller
diff options
context:
space:
mode:
authorMarcel Klehr <mklehr@gmx.net>2023-06-20 14:41:58 +0200
committerMarcel Klehr <mklehr@gmx.net>2023-08-09 09:59:58 +0200
commit236c32b13f190f49fa369e4eb20ec4a85a76ba04 (patch)
tree4fea77e1e266efd327b5585e652da1495d8c1c01 /core/Controller
parent5807c431b854cca5e46fda4222782cd488137222 (diff)
downloadnextcloud-server-236c32b13f190f49fa369e4eb20ec4a85a76ba04.tar.gz
nextcloud-server-236c32b13f190f49fa369e4eb20ec4a85a76ba04.zip
LLM OCP API: Implement ocs API
Signed-off-by: Marcel Klehr <mklehr@gmx.net> (cherry picked from commit 795b097122a8dd70b4d6b9ebe044440396be9104)
Diffstat (limited to 'core/Controller')
-rw-r--r--core/Controller/LanguageModelApiController.php96
1 files changed, 96 insertions, 0 deletions
diff --git a/core/Controller/LanguageModelApiController.php b/core/Controller/LanguageModelApiController.php
new file mode 100644
index 00000000000..5699dd75526
--- /dev/null
+++ b/core/Controller/LanguageModelApiController.php
@@ -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);
+ }
+ }
+}