]> source.dussan.org Git - nextcloud-server.git/commitdiff
LLM OCP API: Rework to use Task objects
authorMarcel Klehr <mklehr@gmx.net>
Thu, 15 Jun 2023 11:22:16 +0000 (13:22 +0200)
committerMarcel Klehr <mklehr@gmx.net>
Wed, 9 Aug 2023 07:55:16 +0000 (09:55 +0200)
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
(cherry picked from commit 01dd1a894dbf9eb6ed1fb013c4b5ee4816c32904)

12 files changed:
lib/public/LanguageModel/AbstractLanguageModelTask.php [new file with mode: 0644]
lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php
lib/public/LanguageModel/Events/PromptFailedEvent.php [deleted file]
lib/public/LanguageModel/Events/PromptSuccessfulEvent.php [deleted file]
lib/public/LanguageModel/Events/SummaryFailedEvent.php [deleted file]
lib/public/LanguageModel/Events/SummarySuccessfulEvent.php [deleted file]
lib/public/LanguageModel/Events/TaskFailedEvent.php [new file with mode: 0644]
lib/public/LanguageModel/Events/TaskSuccessfulEvent.php [new file with mode: 0644]
lib/public/LanguageModel/FreePromptTask.php [new file with mode: 0644]
lib/public/LanguageModel/ILanguageModelManager.php
lib/public/LanguageModel/ISummaryProvider.php
lib/public/LanguageModel/SummaryTask.php [new file with mode: 0644]

diff --git a/lib/public/LanguageModel/AbstractLanguageModelTask.php b/lib/public/LanguageModel/AbstractLanguageModelTask.php
new file mode 100644 (file)
index 0000000..50ae209
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+
+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;
+
+       protected ?int $id;
+       protected int $status = self::STATUS_UNKNOWN;
+
+       public function __construct(
+               protected string $input,
+               protected string $appId,
+               protected ?string $userId,
+       ) {
+       }
+
+       abstract public function visitProvider(ILanguageModelProvider $provider): string;
+
+       /**
+        * @return int
+        */
+       public function getStatus(): int {
+               return $this->status;
+       }
+
+       /**
+        * @param int $status
+        */
+       public function setStatus(int $status): void {
+               $this->status = $status;
+       }
+
+       /**
+        * @return int|null
+        */
+       public function getId(): ?int {
+               return $this->id;
+       }
+
+       /**
+        * @param int|null $id
+        */
+       public function setId(?int $id): void {
+               $this->id = $id;
+       }
+
+       /**
+        * @return string
+        */
+       public function getInput(): string {
+               return $this->input;
+       }
+
+       /**
+        * @return string
+        */
+       public function getAppId(): string {
+               return $this->appId;
+       }
+
+       /**
+        * @return string|null
+        */
+       public function getUserId(): ?string {
+               return $this->userId;
+       }
+}
index 593385b02f0ed4b52b86273e68275c39d8484b01..3d274330dc7e1f3268397df7145ee98f1242d8c7 100644 (file)
@@ -26,6 +26,7 @@ declare(strict_types=1);
 namespace OCP\LanguageModel\Events;
 
 use OCP\EventDispatcher\Event;
+use OCP\LanguageModel\AbstractLanguageModelTask;
 
 /**
  * @since 28.0.0
@@ -35,32 +36,16 @@ abstract class AbstractLanguageModelEvent extends Event {
         * @since 28.0.0
         */
        public function __construct(
-               private int $requestId,
-               private ?string $userId,
-               private string $appId,
+               private AbstractLanguageModelTask $task
        ) {
                parent::__construct();
        }
 
        /**
+        * @return AbstractLanguageModelTask
         * @since 28.0.0
         */
-       public function getRequestId(): int {
-               return $this->requestId;
-       }
-
-
-       /**
-        * @since 28.0.0
-        */
-       public function getUserId(): ?string {
-               return $this->userId;
-       }
-
-       /**
-        * @since 28.0.0
-        */
-       public function getAppId(): string {
-               return $this->appId;
+       public function getTask(): AbstractLanguageModelTask {
+               return $this->task;
        }
 }
diff --git a/lib/public/LanguageModel/Events/PromptFailedEvent.php b/lib/public/LanguageModel/Events/PromptFailedEvent.php
deleted file mode 100644 (file)
index 2ad8848..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-<?php
-
-namespace OCP\LanguageModel\Events;
-
-/**
- * @since 28.0.0
- */
-class PromptFailedEvent extends AbstractLanguageModelEvent {
-
-       public function __construct(int $requestId,
-                                                               ?string $userId,
-                                                               string $appId,
-                                                               private string $errorMessage) {
-               parent::__construct($requestId, $userId, $appId);
-       }
-
-       /**
-        * @since 28.0.0
-        * @return string
-        */
-       public function getErrorMessage(): string {
-               return $this->errorMessage;
-       }
-}
diff --git a/lib/public/LanguageModel/Events/PromptSuccessfulEvent.php b/lib/public/LanguageModel/Events/PromptSuccessfulEvent.php
deleted file mode 100644 (file)
index f9e3a61..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-<?php
-
-namespace OCP\LanguageModel\Events;
-
-/**
- * @since 28.0.0
- */
-class PromptSuccessfulEvent extends AbstractLanguageModelEvent {
-
-       public function __construct(int $requestId,
-                                                               ?string $userId,
-                                                               string $appId,
-                                                               private string $output) {
-               parent::__construct($requestId, $userId, $appId);
-       }
-
-       /**
-        * @return string
-        */
-       public function getOutput(): string {
-               return $this->output;
-       }
-}
diff --git a/lib/public/LanguageModel/Events/SummaryFailedEvent.php b/lib/public/LanguageModel/Events/SummaryFailedEvent.php
deleted file mode 100644 (file)
index 4907065..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-<?php
-
-namespace OCP\LanguageModel\Events;
-
-/**
- * @since 28.0.0
- */
-class SummaryFailedEvent extends AbstractLanguageModelEvent {
-
-       public function __construct(int $requestId,
-                                                               ?string $userId,
-                                                               string $appId,
-                                                               private string $errorMessage) {
-               parent::__construct($requestId, $userId, $appId);
-       }
-
-       /**
-        * @since 28.0.0
-        * @return string
-        */
-       public function getErrorMessage(): string {
-               return $this->errorMessage;
-       }
-}
diff --git a/lib/public/LanguageModel/Events/SummarySuccessfulEvent.php b/lib/public/LanguageModel/Events/SummarySuccessfulEvent.php
deleted file mode 100644 (file)
index 3533944..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-<?php
-
-namespace OCP\LanguageModel\Events;
-
-/**
- * @since 28.0.0
- */
-class SummarySuccessfulEvent extends AbstractLanguageModelEvent {
-
-       public function __construct(int $requestId,
-                                                               ?string $userId,
-                                                               string $appId,
-                                                               private string $output) {
-               parent::__construct($requestId, $userId, $appId);
-       }
-
-       /**
-        * @return string
-        */
-       public function getOutput(): string {
-               return $this->output;
-       }
-}
diff --git a/lib/public/LanguageModel/Events/TaskFailedEvent.php b/lib/public/LanguageModel/Events/TaskFailedEvent.php
new file mode 100644 (file)
index 0000000..2b0dea9
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+namespace OCP\LanguageModel\Events;
+
+use OCP\LanguageModel\AbstractLanguageModelTask;
+
+/**
+ * @since 28.0.0
+ */
+class TaskFailedEvent extends AbstractLanguageModelEvent {
+
+       public function __construct(AbstractLanguageModelTask $task,
+                                                               private string $errorMessage) {
+               parent::__construct($task);
+       }
+
+       /**
+        * @return string
+        */
+       public function getErrorMessage(): string {
+               return $this->errorMessage;
+       }
+}
diff --git a/lib/public/LanguageModel/Events/TaskSuccessfulEvent.php b/lib/public/LanguageModel/Events/TaskSuccessfulEvent.php
new file mode 100644 (file)
index 0000000..6cdb571
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+namespace OCP\LanguageModel\Events;
+
+use OCP\LanguageModel\AbstractLanguageModelTask;
+
+/**
+ * @since 28.0.0
+ */
+class TaskSuccessfulEvent extends AbstractLanguageModelEvent {
+
+       public function __construct(AbstractLanguageModelTask $task,
+                                                               private string $output) {
+               parent::__construct($task);
+       }
+
+       /**
+        * @return string
+        */
+       public function getErrorMessage(): string {
+               return $this->output;
+       }
+}
diff --git a/lib/public/LanguageModel/FreePromptTask.php b/lib/public/LanguageModel/FreePromptTask.php
new file mode 100644 (file)
index 0000000..ff7fa7f
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+namespace OCP\LanguageModel;
+
+use RuntimeException;
+
+class FreePromptTask extends AbstractLanguageModelTask {
+
+       /**
+        * @param ILanguageModelProvider $provider
+        * @throws RuntimeException
+        * @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;
+       }
+}
index 6805309995502fd96b4ed07ecfaf00bd3226bfcb..e0d33777052a1259c8f3bdf736698765f7392726 100644 (file)
@@ -27,6 +27,7 @@ declare(strict_types=1);
 namespace OCP\LanguageModel;
 
 use InvalidArgumentException;
+use OCP\LanguageModel\Events\AbstractLanguageModelEvent;
 use OCP\PreConditionNotMetException;
 use RuntimeException;
 
@@ -37,53 +38,25 @@ interface ILanguageModelManager {
        public function hasProviders(): bool;
 
        /**
+        * @return string[]
         * @since 28.0.0
         */
-       public function hasSummaryProviders(): bool;
+       public function getAvailableTasks(): array;
 
        /**
-        * @param string $prompt The prompt to call the Language model with
-        * @returns string The output
-        * @throws PreConditionNotMetException If no provider was registered but this method was still called
+        * @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
         * @since 28.0.0
         */
-       public function prompt(string $prompt): string;
+       public function runTask(AbstractLanguageModelTask $task): AbstractLanguageModelEvent;
 
        /**
         * Will schedule an LLM inference process in the background. The result will become available
-        * with the \OCP\LanguageModel\Events\PromptFinishedEvent
+        * with the \OCP\LanguageModel\Events\TaskFinishedEvent
         *
-        * @param string $prompt The prompt to call the Language model with
-        * @param ?string $userId The user that triggered this request (only for convenience, will be available on the TranscriptEvents)
-        * @param string $appId The app that triggered this request (only for convenience, will be available on the TranscriptEvents)
-        * @returns int The id of the prompt request
-        * @throws PreConditionNotMetException If no provider was registered but this method was still called
+        * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
         * @since 28.0.0
         */
-       public function schedulePrompt(string $prompt, ?string $userId, string $appId): int;
-
-       /**
-        * Will schedule an LLM inference process in the background. The result will become available
-        * with the \OCP\LanguageModel\Events\PromptFinishedEvent
-        *
-        * @param string $text The text to summarize
-        * @param ?string $userId The user that triggered this request (only for convenience, will be available on the TranscriptEvents)
-        * @param string $appId The app that triggered this request (only for convenience, will be available on the TranscriptEvents)
-        * @returns int The id of the prompt request
-        * @throws PreConditionNotMetException If no summary provider was registered but this method was still called
-        * @since 28.0.0
-        */
-       public function scheduleSummary(string $text, ?string $userId, string $appId): int;
-
-       /**
-        * @param string $text The text to summarize
-        * @returns string The output
-        * @throws PreConditionNotMetException If no summary 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
-        * @since 28.0.0
-        */
-       public function summarize(string $text): string;
+       public function scheduleTask(AbstractLanguageModelTask $task) : void;
 }
index 125ef313f809446244557f245e05b5e4d83d3e08..fa4bf873e8fcc9d299244b16efd354851a8f1a57 100644 (file)
@@ -31,7 +31,7 @@ use RuntimeException;
 /**
  * @since 28.0.0
  */
-interface ISummaryProvider {
+interface ISummaryProvider extends ILanguageModelProvider {
 
        /**
         * @param string $text The text to summarize
diff --git a/lib/public/LanguageModel/SummaryTask.php b/lib/public/LanguageModel/SummaryTask.php
new file mode 100644 (file)
index 0000000..0037beb
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+namespace OCP\LanguageModel;
+
+use RuntimeException;
+
+class SummaryTask extends AbstractLanguageModelTask {
+
+       /**
+        * @param ILanguageModelProvider&ISummaryProvider $provider
+        * @throws RuntimeException
+        * @return string
+        */
+       public function visitProvider(ILanguageModelProvider $provider): string {
+               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;
+       }
+}