]> source.dussan.org Git - nextcloud-server.git/commitdiff
LLM OCP API: Type shenanigans for Visitor pattern
authorMarcel Klehr <mklehr@gmx.net>
Tue, 27 Jun 2023 14:43:54 +0000 (16:43 +0200)
committerMarcel Klehr <mklehr@gmx.net>
Wed, 9 Aug 2023 08:00:56 +0000 (10:00 +0200)
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
(cherry picked from commit 906e9b7014203ac4e7608155287efe793a83aa8e)

lib/public/LanguageModel/AbstractLanguageModelTask.php
lib/public/LanguageModel/FreePromptTask.php
lib/public/LanguageModel/HeadlineTask.php
lib/public/LanguageModel/ILanguageModelProvider.php
lib/public/LanguageModel/ILanguageModelTask.php
lib/public/LanguageModel/SummaryTask.php
lib/public/LanguageModel/TopicsTask.php

index 05503f4d95cb3cd033500ae762a520e4eae18248..9621b3d49055570bed9b1e0b8c5d11e1bbf88a4a 100644 (file)
@@ -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;
@@ -25,21 +29,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
index 02eb6803276ed11f84798c74d97a4bc62c5160e9..1471e13eae20e85bb78b4df90dcaec1979d40df4 100644 (file)
@@ -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;
        }
 
index 92abc551cc6fbd257e5ee857900a8043dadd31fb..1f3680b31166112445ed7ef7962bbcb66ff8922b 100644 (file)
@@ -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;
        }
 
index f44ee64f151690fb66c00d08bb31f90a60a8dbeb..56c20d1e7cca8fe9c7fb907b11a30793df6157f4 100644 (file)
@@ -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 {
index d0d7f825ffbb68ec27924e4465f2fa840caffceb..1bb06c259422cb6407764273a2b9891bf36c8fc1 100644 (file)
@@ -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;
 
 
        /**
index ffc7aa01e7158da4479e15844514f279af793a35..5bb158f00ddb9dda96923cf6aaedeedb01e710ca 100644 (file)
@@ -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;
        }
 
index a628695a59a3685fce7880d94129fca7448caec1..41571fb7e2119df061b3c75e8522140fc1b7b520 100644 (file)
@@ -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;
        }