--- /dev/null
+<?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;
+ }
+}
namespace OCP\LanguageModel\Events;
use OCP\EventDispatcher\Event;
+use OCP\LanguageModel\AbstractLanguageModelTask;
/**
* @since 28.0.0
* @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;
}
}
+++ /dev/null
-<?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;
- }
-}
+++ /dev/null
-<?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;
- }
-}
+++ /dev/null
-<?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;
- }
-}
+++ /dev/null
-<?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;
- }
-}
--- /dev/null
+<?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;
+ }
+}
--- /dev/null
+<?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;
+ }
+}
--- /dev/null
+<?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;
+ }
+}
namespace OCP\LanguageModel;
use InvalidArgumentException;
+use OCP\LanguageModel\Events\AbstractLanguageModelEvent;
use OCP\PreConditionNotMetException;
use RuntimeException;
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;
}
/**
* @since 28.0.0
*/
-interface ISummaryProvider {
+interface ISummaryProvider extends ILanguageModelProvider {
/**
* @param string $text The text to summarize
--- /dev/null
+<?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;
+ }
+}