--- /dev/null
+<?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);
+ }
+ }
+}
$table->addColumn('input', Types::TEXT, [
'notnull' => true,
]);
+ $table->addColumn('output', Types::TEXT, [
+ 'notnull' => false,
+ ]);
$table->addColumn('status', Types::INTEGER, [
'notnull' => false,
'length' => 6,
['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'],
],
]);
/**
* @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() {
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(),
]);
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));
}
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);
$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());
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;
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;
$this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class);
+ $this->registerAlias(ILanguageModelManager::class, LanguageModelManager::class);
+
$this->connectDispatcher();
}
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;
*/
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
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());
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');
*/
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;
- }
}
*/
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
*/
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;
}