-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38854 from nextcloud/enh/llm-api
- Loading branch information
Showing
30 changed files
with
1,989 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?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\Controller; | ||
|
||
use InvalidArgumentException; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\Common\Exception\NotFoundException; | ||
use OCP\IL10N; | ||
use OCP\IRequest; | ||
use OCP\TextProcessing\ITaskType; | ||
use OCP\TextProcessing\Task; | ||
use OCP\TextProcessing\IManager; | ||
use OCP\PreConditionNotMetException; | ||
use Psr\Container\ContainerExceptionInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class TextProcessingApiController extends \OCP\AppFramework\OCSController { | ||
public function __construct( | ||
string $appName, | ||
IRequest $request, | ||
private IManager $languageModelManager, | ||
private IL10N $l, | ||
private ?string $userId, | ||
private ContainerInterface $container, | ||
private LoggerInterface $logger, | ||
) { | ||
parent::__construct($appName, $request); | ||
} | ||
|
||
/** | ||
* This endpoint returns all available LanguageModel task types | ||
* | ||
* @PublicPage | ||
*/ | ||
public function taskTypes(): DataResponse { | ||
$typeClasses = $this->languageModelManager->getAvailableTaskTypes(); | ||
$types = []; | ||
foreach ($typeClasses as $typeClass) { | ||
try { | ||
/** @var ITaskType $object */ | ||
$object = $this->container->get($typeClass); | ||
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) { | ||
$this->logger->warning('Could not find ' . $typeClass, ['exception' => $e]); | ||
continue; | ||
} | ||
$types[] = [ | ||
'id' => $typeClass, | ||
'name' => $object->getName(), | ||
'description' => $object->getDescription(), | ||
]; | ||
} | ||
|
||
return new DataResponse([ | ||
'types' => $types, | ||
]); | ||
} | ||
|
||
/** | ||
* This endpoint allows scheduling a language model task | ||
* | ||
* @PublicPage | ||
* @UserRateThrottle(limit=20, period=120) | ||
* @AnonRateThrottle(limit=5, period=120) | ||
*/ | ||
public function schedule(string $input, string $type, string $appId, string $identifier = ''): DataResponse { | ||
try { | ||
$task = new Task($type, $input, $appId, $this->userId, $identifier); | ||
} catch (InvalidArgumentException) { | ||
return new DataResponse(['message' => $this->l->t('Requested task type does not exist')], Http::STATUS_BAD_REQUEST); | ||
} | ||
try { | ||
$this->languageModelManager->scheduleTask($task); | ||
|
||
$json = $task->jsonSerialize(); | ||
|
||
return new DataResponse([ | ||
'task' => $json, | ||
]); | ||
} catch (PreConditionNotMetException) { | ||
return new DataResponse(['message' => $this->l->t('Necessary language model provider is not available')], Http::STATUS_PRECONDITION_FAILED); | ||
} | ||
} | ||
|
||
/** | ||
* This endpoint allows checking the status and results of a task. | ||
* Tasks are removed 1 week after receiving their last update. | ||
* | ||
* @PublicPage | ||
* @param int $id The id of the task | ||
*/ | ||
public function getTask(int $id): DataResponse { | ||
try { | ||
$task = $this->languageModelManager->getTask($id); | ||
|
||
if ($this->userId !== $task->getUserId()) { | ||
return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
$json = $task->jsonSerialize(); | ||
|
||
return new DataResponse([ | ||
'task' => $json, | ||
]); | ||
} catch (NotFoundException $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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?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 llm_tasks table | ||
*/ | ||
class Version28000Date20230616104802 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('llm_tasks')) { | ||
$table = $schema->createTable('llm_tasks'); | ||
|
||
$table->addColumn('id', Types::BIGINT, [ | ||
'notnull' => true, | ||
'length' => 64, | ||
'autoincrement' => true, | ||
]); | ||
$table->addColumn('type', Types::STRING, [ | ||
'notnull' => true, | ||
'length' => 255, | ||
]); | ||
$table->addColumn('input', Types::TEXT, [ | ||
'notnull' => true, | ||
]); | ||
$table->addColumn('output', Types::TEXT, [ | ||
'notnull' => false, | ||
]); | ||
$table->addColumn('status', Types::INTEGER, [ | ||
'notnull' => false, | ||
'length' => 6, | ||
'default' => 0, | ||
]); | ||
$table->addColumn('user_id', Types::STRING, [ | ||
'notnull' => true, | ||
'length' => 64, | ||
]); | ||
$table->addColumn('app_id', Types::STRING, [ | ||
'notnull' => true, | ||
'length' => 32, | ||
'default' => '', | ||
]); | ||
$table->addColumn('identifier', Types::STRING, [ | ||
'notnull' => true, | ||
'length' => 255, | ||
'default' => '', | ||
]); | ||
$table->addColumn('last_updated', 'integer', [ | ||
'notnull' => false, | ||
'length' => 4, | ||
'default' => 0, | ||
'unsigned' => true, | ||
]); | ||
|
||
$table->setPrimaryKey(['id'], 'llm_tasks_id_index'); | ||
$table->addUniqueIndex(['status', 'type'], 'llm_tasks_status_type'); | ||
$table->addIndex(['last_updated'], 'llm_tasks_updated'); | ||
|
||
return $schema; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.