diff options
author | Marcel Klehr <mklehr@gmx.net> | 2023-06-27 16:43:54 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2023-08-09 10:00:56 +0200 |
commit | c8bbe8fc75f24262573f9cafee533701680fb1e1 (patch) | |
tree | c5851c2b284be2e7b957086e6b2bc8733c067170 /lib/public | |
parent | 44050274715bc389ed5cde3d8ac58dd241da7865 (diff) | |
download | nextcloud-server-c8bbe8fc75f24262573f9cafee533701680fb1e1.tar.gz nextcloud-server-c8bbe8fc75f24262573f9cafee533701680fb1e1.zip |
LLM OCP API: Type shenanigans for Visitor pattern
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
(cherry picked from commit 906e9b7014203ac4e7608155287efe793a83aa8e)
Diffstat (limited to 'lib/public')
-rw-r--r-- | lib/public/LanguageModel/AbstractLanguageModelTask.php | 21 | ||||
-rw-r--r-- | lib/public/LanguageModel/FreePromptTask.php | 5 | ||||
-rw-r--r-- | lib/public/LanguageModel/HeadlineTask.php | 9 | ||||
-rw-r--r-- | lib/public/LanguageModel/ILanguageModelProvider.php | 2 | ||||
-rw-r--r-- | lib/public/LanguageModel/ILanguageModelTask.php | 9 | ||||
-rw-r--r-- | lib/public/LanguageModel/SummaryTask.php | 9 | ||||
-rw-r--r-- | lib/public/LanguageModel/TopicsTask.php | 9 |
7 files changed, 30 insertions, 34 deletions
diff --git a/lib/public/LanguageModel/AbstractLanguageModelTask.php b/lib/public/LanguageModel/AbstractLanguageModelTask.php index 05503f4d95c..9621b3d4905 100644 --- a/lib/public/LanguageModel/AbstractLanguageModelTask.php +++ b/lib/public/LanguageModel/AbstractLanguageModelTask.php @@ -5,7 +5,11 @@ namespace OCP\LanguageModel; use OC\LanguageModel\Db\Task; /** - * @since 28.0.0 + * This is an abstract LanguageModel task that implements basic + * goodies for downstream tasks + * @since 28.0. + * @template T of ILanguageModelProvider + * @template-implements ILanguageModelTask<T> */ abstract class AbstractLanguageModelTask implements ILanguageModelTask { protected ?int $id; @@ -26,21 +30,6 @@ abstract class AbstractLanguageModelTask implements ILanguageModelTask { } /** - * @param ILanguageModelProvider $provider - * @return string - * @throws \RuntimeException - * @since 28.0.0 - */ - abstract public function visitProvider(ILanguageModelProvider $provider): string; - - /** - * @param ILanguageModelProvider $provider - * @return bool - * @since 28.0.0 - */ - abstract public function canUseProvider(ILanguageModelProvider $provider): bool; - - /** * @return string * @since 28.0.0 */ diff --git a/lib/public/LanguageModel/FreePromptTask.php b/lib/public/LanguageModel/FreePromptTask.php index 02eb6803276..1471e13eae2 100644 --- a/lib/public/LanguageModel/FreePromptTask.php +++ b/lib/public/LanguageModel/FreePromptTask.php @@ -4,6 +4,7 @@ namespace OCP\LanguageModel; /** * @since 28.0.0 + * @template-extends AbstractLanguageModelTask<ILanguageModelProvider> */ final class FreePromptTask extends AbstractLanguageModelTask { /** @@ -15,7 +16,7 @@ final class FreePromptTask extends AbstractLanguageModelTask { * @inheritDoc * @since 28.0.0 */ - public function visitProvider(ILanguageModelProvider $provider): string { + public function visitProvider($provider): string { return $provider->prompt($this->getInput()); } @@ -23,7 +24,7 @@ final class FreePromptTask extends AbstractLanguageModelTask { * @inheritDoc * @since 28.0.0 */ - public function canUseProvider(ILanguageModelProvider $provider): bool { + public function canUseProvider($provider): bool { return true; } diff --git a/lib/public/LanguageModel/HeadlineTask.php b/lib/public/LanguageModel/HeadlineTask.php index 92abc551cc6..1f3680b3116 100644 --- a/lib/public/LanguageModel/HeadlineTask.php +++ b/lib/public/LanguageModel/HeadlineTask.php @@ -6,6 +6,7 @@ namespace OCP\LanguageModel; * This LanguageModel Task represents headline generation * which generates a headline for the passed text * @since 28.0.0 + * @template-extends AbstractLanguageModelTask<IHeadlineProvider> */ final class HeadlineTask extends AbstractLanguageModelTask { /** @@ -17,9 +18,9 @@ final class HeadlineTask extends AbstractLanguageModelTask { * @inheritDoc * @since 28.0.0 */ - public function visitProvider(ILanguageModelProvider $provider): string { - if (!$provider instanceof IHeadlineProvider) { - throw new \RuntimeException('SummaryTask#visitProvider expects IHeadlineProvider'); + public function visitProvider($provider): string { + if (!$this->canUseProvider($provider)) { + throw new \RuntimeException('HeadlineTask#visitProvider expects IHeadlineProvider'); } return $provider->findHeadline($this->getInput()); } @@ -28,7 +29,7 @@ final class HeadlineTask extends AbstractLanguageModelTask { * @inheritDoc * @since 28.0.0 */ - public function canUseProvider(ILanguageModelProvider $provider): bool { + public function canUseProvider($provider): bool { return $provider instanceof IHeadlineProvider; } diff --git a/lib/public/LanguageModel/ILanguageModelProvider.php b/lib/public/LanguageModel/ILanguageModelProvider.php index f44ee64f151..56c20d1e7cc 100644 --- a/lib/public/LanguageModel/ILanguageModelProvider.php +++ b/lib/public/LanguageModel/ILanguageModelProvider.php @@ -29,6 +29,8 @@ namespace OCP\LanguageModel; use RuntimeException; /** + * This is the minimum interface is implemented by apps that + * implement a LanguageModel provider * @since 28.0.0 */ interface ILanguageModelProvider { diff --git a/lib/public/LanguageModel/ILanguageModelTask.php b/lib/public/LanguageModel/ILanguageModelTask.php index d0d7f825ffb..1bb06c25942 100644 --- a/lib/public/LanguageModel/ILanguageModelTask.php +++ b/lib/public/LanguageModel/ILanguageModelTask.php @@ -4,6 +4,7 @@ namespace OCP\LanguageModel; /** * @since 28.0.0 + * @template T of ILanguageModelProvider */ interface ILanguageModelTask extends \JsonSerializable { /** @@ -38,18 +39,18 @@ interface ILanguageModelTask extends \JsonSerializable { ]; /** - * @param ILanguageModelProvider $provider + * @param T $provider * @return string * @since 28.0.0 */ - public function visitProvider(ILanguageModelProvider $provider): string; + public function visitProvider($provider): string; /** - * @param ILanguageModelProvider $provider + * @param T $provider * @return bool * @since 28.0.0 */ - public function canUseProvider(ILanguageModelProvider $provider): bool; + public function canUseProvider($provider): bool; /** diff --git a/lib/public/LanguageModel/SummaryTask.php b/lib/public/LanguageModel/SummaryTask.php index ffc7aa01e71..5bb158f00dd 100644 --- a/lib/public/LanguageModel/SummaryTask.php +++ b/lib/public/LanguageModel/SummaryTask.php @@ -3,9 +3,10 @@ namespace OCP\LanguageModel; /** - * This LanguageModel Task represents summarization + * This is an absctract LanguageModel Task represents summarization * which sums up the passed text. * @since 28.0.0 + * @template-extends AbstractLanguageModelTask<ISummaryProvider> */ final class SummaryTask extends AbstractLanguageModelTask { /** @@ -17,8 +18,8 @@ final class SummaryTask extends AbstractLanguageModelTask { * @inheritDoc * @since 28.0.0 */ - public function visitProvider(ILanguageModelProvider $provider): string { - if (!$provider instanceof ISummaryProvider) { + public function visitProvider($provider): string { + if (!$this->canUseProvider($provider)) { throw new \RuntimeException('SummaryTask#visitProvider expects ISummaryProvider'); } return $provider->summarize($this->getInput()); @@ -28,7 +29,7 @@ final class SummaryTask extends AbstractLanguageModelTask { * @inheritDoc * @since 28.0.0 */ - public function canUseProvider(ILanguageModelProvider $provider): bool { + public function canUseProvider($provider): bool { return $provider instanceof ISummaryProvider; } diff --git a/lib/public/LanguageModel/TopicsTask.php b/lib/public/LanguageModel/TopicsTask.php index a628695a59a..41571fb7e21 100644 --- a/lib/public/LanguageModel/TopicsTask.php +++ b/lib/public/LanguageModel/TopicsTask.php @@ -6,6 +6,7 @@ namespace OCP\LanguageModel; * This LanguageModel Task represents topics synthesis * which outputs comma-separated topics for the passed text * @since 28.0.0 + * @template-extends AbstractLanguageModelTask<ITopicsProvider> */ final class TopicsTask extends AbstractLanguageModelTask { /** @@ -17,9 +18,9 @@ final class TopicsTask extends AbstractLanguageModelTask { * @inheritDoc * @since 28.0.0 */ - public function visitProvider(ILanguageModelProvider $provider): string { - if (!$provider instanceof ITopicsProvider) { - throw new \RuntimeException('SummaryTask#visitProvider expects IHeadlineProvider'); + public function visitProvider($provider): string { + if (!$this->canUseProvider($provider)) { + throw new \RuntimeException('TopicsTask#visitProvider expects ITopicsProvider'); } return $provider->findTopics($this->getInput()); } @@ -28,7 +29,7 @@ final class TopicsTask extends AbstractLanguageModelTask { * @inheritDoc * @since 28.0.0 */ - public function canUseProvider(ILanguageModelProvider $provider): bool { + public function canUseProvider($provider): bool { return $provider instanceof ITopicsProvider; } |