From 2e9dea20613fbc2cb2227cda19ade2daee7e6664 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Thu, 15 Jun 2023 10:30:04 +0200 Subject: [PATCH] Initial work on lm OCP API Signed-off-by: Marcel Klehr (cherry picked from commit 70d5bf79e315a1bc8e6380ff4c5a14842362c9c7) --- .../Events/AbstractLanguageModelEvent.php | 66 ++++++++++++++ .../Events/PromptFailedEvent.php | 24 +++++ .../Events/PromptSuccessfulEvent.php | 23 +++++ .../Events/SummaryFailedEvent.php | 24 +++++ .../Events/SummarySuccessfulEvent.php | 23 +++++ .../LanguageModel/ILanguageModelManager.php | 89 +++++++++++++++++++ .../LanguageModel/ILanguageModelProvider.php | 47 ++++++++++ lib/public/LanguageModel/ISummaryProvider.php | 43 +++++++++ 8 files changed, 339 insertions(+) create mode 100644 lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php create mode 100644 lib/public/LanguageModel/Events/PromptFailedEvent.php create mode 100644 lib/public/LanguageModel/Events/PromptSuccessfulEvent.php create mode 100644 lib/public/LanguageModel/Events/SummaryFailedEvent.php create mode 100644 lib/public/LanguageModel/Events/SummarySuccessfulEvent.php create mode 100644 lib/public/LanguageModel/ILanguageModelManager.php create mode 100644 lib/public/LanguageModel/ILanguageModelProvider.php create mode 100644 lib/public/LanguageModel/ISummaryProvider.php diff --git a/lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php b/lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php new file mode 100644 index 00000000000..593385b02f0 --- /dev/null +++ b/lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php @@ -0,0 +1,66 @@ + + * + * @author Marcel Klehr + * + * @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 . + * + */ +namespace OCP\LanguageModel\Events; + +use OCP\EventDispatcher\Event; + +/** + * @since 28.0.0 + */ +abstract class AbstractLanguageModelEvent extends Event { + /** + * @since 28.0.0 + */ + public function __construct( + private int $requestId, + private ?string $userId, + private string $appId, + ) { + parent::__construct(); + } + + /** + * @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; + } +} diff --git a/lib/public/LanguageModel/Events/PromptFailedEvent.php b/lib/public/LanguageModel/Events/PromptFailedEvent.php new file mode 100644 index 00000000000..2ad8848d084 --- /dev/null +++ b/lib/public/LanguageModel/Events/PromptFailedEvent.php @@ -0,0 +1,24 @@ +errorMessage; + } +} diff --git a/lib/public/LanguageModel/Events/PromptSuccessfulEvent.php b/lib/public/LanguageModel/Events/PromptSuccessfulEvent.php new file mode 100644 index 00000000000..f9e3a612318 --- /dev/null +++ b/lib/public/LanguageModel/Events/PromptSuccessfulEvent.php @@ -0,0 +1,23 @@ +output; + } +} diff --git a/lib/public/LanguageModel/Events/SummaryFailedEvent.php b/lib/public/LanguageModel/Events/SummaryFailedEvent.php new file mode 100644 index 00000000000..49070656ec4 --- /dev/null +++ b/lib/public/LanguageModel/Events/SummaryFailedEvent.php @@ -0,0 +1,24 @@ +errorMessage; + } +} diff --git a/lib/public/LanguageModel/Events/SummarySuccessfulEvent.php b/lib/public/LanguageModel/Events/SummarySuccessfulEvent.php new file mode 100644 index 00000000000..353394438f0 --- /dev/null +++ b/lib/public/LanguageModel/Events/SummarySuccessfulEvent.php @@ -0,0 +1,23 @@ +output; + } +} diff --git a/lib/public/LanguageModel/ILanguageModelManager.php b/lib/public/LanguageModel/ILanguageModelManager.php new file mode 100644 index 00000000000..68053099955 --- /dev/null +++ b/lib/public/LanguageModel/ILanguageModelManager.php @@ -0,0 +1,89 @@ + + * + * @author Marcel Klehr + * + * @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 . + */ + + +namespace OCP\LanguageModel; + +use InvalidArgumentException; +use OCP\PreConditionNotMetException; +use RuntimeException; + +interface ILanguageModelManager { + /** + * @since 28.0.0 + */ + public function hasProviders(): bool; + + /** + * @since 28.0.0 + */ + public function hasSummaryProviders(): bool; + + /** + * @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 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; + + /** + * Will schedule an LLM inference process in the background. The result will become available + * with the \OCP\LanguageModel\Events\PromptFinishedEvent + * + * @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 + * @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; +} diff --git a/lib/public/LanguageModel/ILanguageModelProvider.php b/lib/public/LanguageModel/ILanguageModelProvider.php new file mode 100644 index 00000000000..f44ee64f151 --- /dev/null +++ b/lib/public/LanguageModel/ILanguageModelProvider.php @@ -0,0 +1,47 @@ + + * + * @author Marcel Klehr + * + * @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 . + */ + + +namespace OCP\LanguageModel; + +use RuntimeException; + +/** + * @since 28.0.0 + */ +interface ILanguageModelProvider { + /** + * @since 28.0.0 + */ + public function getName(): string; + + /** + * @param string $prompt The prompt to call the model with + * @return string the output + * @since 28.0.0 + * @throws RuntimeException If the text could not be transcribed + */ + public function prompt(string $prompt): string; +} diff --git a/lib/public/LanguageModel/ISummaryProvider.php b/lib/public/LanguageModel/ISummaryProvider.php new file mode 100644 index 00000000000..125ef313f80 --- /dev/null +++ b/lib/public/LanguageModel/ISummaryProvider.php @@ -0,0 +1,43 @@ + + * + * @author Marcel Klehr + * + * @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 . + */ + + +namespace OCP\LanguageModel; + +use RuntimeException; + +/** + * @since 28.0.0 + */ +interface ISummaryProvider { + + /** + * @param string $text The text to summarize + * @returns string the summary + * @since 28.0.0 + * @throws RuntimeException If the text could not be transcribed + */ + public function summarize(string $text): string; +}