diff options
author | Marcel Klehr <mklehr@gmx.net> | 2023-06-16 13:06:47 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2023-07-07 13:39:10 +0200 |
commit | 34138736538f604af2c6c52aa43662d1d66087d0 (patch) | |
tree | 11a80b580159c57bbb5c3d45a0e7770da314030c /lib/public | |
parent | 01dd1a894dbf9eb6ed1fb013c4b5ee4816c32904 (diff) | |
download | nextcloud-server-34138736538f604af2c6c52aa43662d1d66087d0.tar.gz nextcloud-server-34138736538f604af2c6c52aa43662d1d66087d0.zip |
LLM OCP API: Implement private backend code + add ILanguageModelTask
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'lib/public')
8 files changed, 126 insertions, 46 deletions
diff --git a/lib/public/LanguageModel/AbstractLanguageModelTask.php b/lib/public/LanguageModel/AbstractLanguageModelTask.php index 50ae2095235..12aedc95fe5 100644 --- a/lib/public/LanguageModel/AbstractLanguageModelTask.php +++ b/lib/public/LanguageModel/AbstractLanguageModelTask.php @@ -2,71 +2,91 @@ namespace OCP\LanguageModel; -abstract class AbstractLanguageModelTask { - public const STATUS_UNKNOWN = 0; - public const STATUS_RUNNING = 1; - public const STATUS_SUCCESSFUL = 2; - public const STATUS_FAILED = 4; +use OC\LanguageModel\Db\Task; +abstract class AbstractLanguageModelTask implements ILanguageModelTask { protected ?int $id; - protected int $status = self::STATUS_UNKNOWN; + protected int $status = ILanguageModelTask::STATUS_UNKNOWN; - public function __construct( + public final function __construct( protected string $input, protected string $appId, protected ?string $userId, ) { } + /** + * @param ILanguageModelProvider $provider + * @return string + * @throws \RuntimeException + */ abstract public function visitProvider(ILanguageModelProvider $provider): string; + abstract public function canUseProvider(ILanguageModelProvider $provider): bool; + + abstract public function getType(): string; + /** * @return int */ - public function getStatus(): int { + public final function getStatus(): int { return $this->status; } /** * @param int $status */ - public function setStatus(int $status): void { + public final function setStatus(int $status): void { $this->status = $status; } /** * @return int|null */ - public function getId(): ?int { + public final function getId(): ?int { return $this->id; } /** * @param int|null $id */ - public function setId(?int $id): void { + public final function setId(?int $id): void { $this->id = $id; } /** * @return string */ - public function getInput(): string { + public final function getInput(): string { return $this->input; } /** * @return string */ - public function getAppId(): string { + public final function getAppId(): string { return $this->appId; } /** * @return string|null */ - public function getUserId(): ?string { + public final function getUserId(): ?string { return $this->userId; } + + public final static function fromTaskEntity(Task $taskEntity): ILanguageModelTask { + $task = self::factory($taskEntity->getType(), $taskEntity->getInput(), $taskEntity->getuserId(), $taskEntity->getAppId()); + $task->setId($taskEntity->getId()); + $task->setStatus($taskEntity->getStatus()); + return $task; + } + + 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'); + } + return new ILanguageModelTask::TYPES[$type]($input, $userId, $appId); + } } diff --git a/lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php b/lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php index 3d274330dc7..218a4480081 100644 --- a/lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php +++ b/lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php @@ -26,7 +26,7 @@ declare(strict_types=1); namespace OCP\LanguageModel\Events; use OCP\EventDispatcher\Event; -use OCP\LanguageModel\AbstractLanguageModelTask; +use OCP\LanguageModel\ILanguageModelTask; /** * @since 28.0.0 @@ -36,16 +36,16 @@ abstract class AbstractLanguageModelEvent extends Event { * @since 28.0.0 */ public function __construct( - private AbstractLanguageModelTask $task + private ILanguageModelTask $task ) { parent::__construct(); } /** - * @return AbstractLanguageModelTask + * @return ILanguageModelTask * @since 28.0.0 */ - public function getTask(): AbstractLanguageModelTask { + public function getTask(): ILanguageModelTask { return $this->task; } } diff --git a/lib/public/LanguageModel/Events/TaskFailedEvent.php b/lib/public/LanguageModel/Events/TaskFailedEvent.php index 2b0dea9153f..5134c37476a 100644 --- a/lib/public/LanguageModel/Events/TaskFailedEvent.php +++ b/lib/public/LanguageModel/Events/TaskFailedEvent.php @@ -2,14 +2,14 @@ namespace OCP\LanguageModel\Events; -use OCP\LanguageModel\AbstractLanguageModelTask; +use OCP\LanguageModel\ILanguageModelTask; /** * @since 28.0.0 */ class TaskFailedEvent extends AbstractLanguageModelEvent { - public function __construct(AbstractLanguageModelTask $task, + public function __construct(ILanguageModelTask $task, private string $errorMessage) { parent::__construct($task); } diff --git a/lib/public/LanguageModel/Events/TaskSuccessfulEvent.php b/lib/public/LanguageModel/Events/TaskSuccessfulEvent.php index 6cdb57143f9..156c5679e0b 100644 --- a/lib/public/LanguageModel/Events/TaskSuccessfulEvent.php +++ b/lib/public/LanguageModel/Events/TaskSuccessfulEvent.php @@ -2,14 +2,14 @@ namespace OCP\LanguageModel\Events; -use OCP\LanguageModel\AbstractLanguageModelTask; +use OCP\LanguageModel\ILanguageModelTask; /** * @since 28.0.0 */ class TaskSuccessfulEvent extends AbstractLanguageModelEvent { - public function __construct(AbstractLanguageModelTask $task, + public function __construct(ILanguageModelTask $task, private string $output) { parent::__construct($task); } diff --git a/lib/public/LanguageModel/FreePromptTask.php b/lib/public/LanguageModel/FreePromptTask.php index ff7fa7fffed..a179048631c 100644 --- a/lib/public/LanguageModel/FreePromptTask.php +++ b/lib/public/LanguageModel/FreePromptTask.php @@ -4,7 +4,8 @@ namespace OCP\LanguageModel; use RuntimeException; -class FreePromptTask extends AbstractLanguageModelTask { +final class FreePromptTask extends AbstractLanguageModelTask { + public const TYPE = 'free_prompt'; /** * @param ILanguageModelProvider $provider @@ -12,14 +13,14 @@ class FreePromptTask extends AbstractLanguageModelTask { * @return string */ public function visitProvider(ILanguageModelProvider $provider): string { - $this->setStatus(self::STATUS_RUNNING); - try { - $output = $provider->prompt($this->getInput()); - } catch (RuntimeException $e) { - $this->setStatus(self::STATUS_FAILED); - throw $e; - } - $this->setStatus(self::STATUS_SUCCESSFUL); - return $output; + return $provider->prompt($this->getInput()); + } + + public function canUseProvider(ILanguageModelProvider $provider): bool { + return true; + } + + public function getType(): string { + return self::TYPE; } } diff --git a/lib/public/LanguageModel/ILanguageModelManager.php b/lib/public/LanguageModel/ILanguageModelManager.php index e0d33777052..a4d3079c180 100644 --- a/lib/public/LanguageModel/ILanguageModelManager.php +++ b/lib/public/LanguageModel/ILanguageModelManager.php @@ -27,6 +27,7 @@ declare(strict_types=1); namespace OCP\LanguageModel; use InvalidArgumentException; +use OCP\LanguageModel\AbstractLanguageModelTask; use OCP\LanguageModel\Events\AbstractLanguageModelEvent; use OCP\PreConditionNotMetException; use RuntimeException; @@ -45,11 +46,10 @@ interface ILanguageModelManager { /** * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called - * @throws InvalidArgumentException If the file could not be found or is not of a supported type - * @throws RuntimeException If the transcription failed for other reasons + * @throws RuntimeException If something else failed * @since 28.0.0 */ - public function runTask(AbstractLanguageModelTask $task): AbstractLanguageModelEvent; + public function runTask(ILanguageModelTask $task): string; /** * Will schedule an LLM inference process in the background. The result will become available @@ -58,5 +58,7 @@ interface ILanguageModelManager { * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called * @since 28.0.0 */ - public function scheduleTask(AbstractLanguageModelTask $task) : void; + public function scheduleTask(ILanguageModelTask $task) : void; + + public function getTask(int $id): ILanguageModelTask; } diff --git a/lib/public/LanguageModel/ILanguageModelTask.php b/lib/public/LanguageModel/ILanguageModelTask.php new file mode 100644 index 00000000000..478ee54e8a3 --- /dev/null +++ b/lib/public/LanguageModel/ILanguageModelTask.php @@ -0,0 +1,56 @@ +<?php + +namespace OCP\LanguageModel; + +interface ILanguageModelTask { + public const STATUS_FAILED = 4; + public const STATUS_SUCCESSFUL = 3; + public const STATUS_RUNNING = 2; + public const STATUS_SCHEDULED = 1; + public const STATUS_UNKNOWN = 0; + + public const TYPES = [ + SummaryTask::TYPE => SummaryTask::class, + FreePromptTask::TYPE => FreePromptTask::class, + ]; + + /** + * @return string + */ + public function getType(): string; + + /** + * @return int + */ + public function getStatus(): int; + + /** + * @param int $status + */ + public function setStatus(int $status): void; + + /** + * @param int|null $id + */ + public function setId(?int $id): void; + + /** + * @return int|null + */ + public function getId(): ?int; + + /** + * @return string + */ + public function getInput(): string; + + /** + * @return string + */ + public function getAppId(): string; + + /** + * @return string|null + */ + public function getUserId(): ?string; +} diff --git a/lib/public/LanguageModel/SummaryTask.php b/lib/public/LanguageModel/SummaryTask.php index 0037beb4593..35f20cebfb6 100644 --- a/lib/public/LanguageModel/SummaryTask.php +++ b/lib/public/LanguageModel/SummaryTask.php @@ -4,7 +4,8 @@ namespace OCP\LanguageModel; use RuntimeException; -class SummaryTask extends AbstractLanguageModelTask { +final class SummaryTask extends AbstractLanguageModelTask { + public const TYPE = 'summarize'; /** * @param ILanguageModelProvider&ISummaryProvider $provider @@ -15,14 +16,14 @@ class SummaryTask extends AbstractLanguageModelTask { if (!$provider instanceof ISummaryProvider) { throw new \RuntimeException('SummaryTask#visitProvider expects ISummaryProvider'); } - $this->setStatus(self::STATUS_RUNNING); - try { - $output = $provider->summarize($this->getInput()); - } catch (RuntimeException $e) { - $this->setStatus(self::STATUS_FAILED); - throw $e; - } - $this->setStatus(self::STATUS_SUCCESSFUL); - return $output; + return $provider->summarize($this->getInput()); + } + + public function canUseProvider(ILanguageModelProvider $provider): bool { + return $provider instanceof ISummaryProvider; + } + + public function getType(): string { + return self::TYPE; } } |