Skip to content

Commit 01dd1a8

Browse files
committed
LLM OCP API: Rework to use Task objects
Signed-off-by: Marcel Klehr <[email protected]>
1 parent 70d5bf7 commit 01dd1a8

12 files changed

+185
-150
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace OCP\LanguageModel;
4+
5+
abstract class AbstractLanguageModelTask {
6+
7+
public const STATUS_UNKNOWN = 0;
8+
public const STATUS_RUNNING = 1;
9+
public const STATUS_SUCCESSFUL = 2;
10+
public const STATUS_FAILED = 4;
11+
12+
protected ?int $id;
13+
protected int $status = self::STATUS_UNKNOWN;
14+
15+
public function __construct(
16+
protected string $input,
17+
protected string $appId,
18+
protected ?string $userId,
19+
) {
20+
}
21+
22+
abstract public function visitProvider(ILanguageModelProvider $provider): string;
23+
24+
/**
25+
* @return int
26+
*/
27+
public function getStatus(): int {
28+
return $this->status;
29+
}
30+
31+
/**
32+
* @param int $status
33+
*/
34+
public function setStatus(int $status): void {
35+
$this->status = $status;
36+
}
37+
38+
/**
39+
* @return int|null
40+
*/
41+
public function getId(): ?int {
42+
return $this->id;
43+
}
44+
45+
/**
46+
* @param int|null $id
47+
*/
48+
public function setId(?int $id): void {
49+
$this->id = $id;
50+
}
51+
52+
/**
53+
* @return string
54+
*/
55+
public function getInput(): string {
56+
return $this->input;
57+
}
58+
59+
/**
60+
* @return string
61+
*/
62+
public function getAppId(): string {
63+
return $this->appId;
64+
}
65+
66+
/**
67+
* @return string|null
68+
*/
69+
public function getUserId(): ?string {
70+
return $this->userId;
71+
}
72+
}

lib/public/LanguageModel/Events/AbstractLanguageModelEvent.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace OCP\LanguageModel\Events;
2727

2828
use OCP\EventDispatcher\Event;
29+
use OCP\LanguageModel\AbstractLanguageModelTask;
2930

3031
/**
3132
* @since 28.0.0
@@ -35,32 +36,16 @@ abstract class AbstractLanguageModelEvent extends Event {
3536
* @since 28.0.0
3637
*/
3738
public function __construct(
38-
private int $requestId,
39-
private ?string $userId,
40-
private string $appId,
39+
private AbstractLanguageModelTask $task
4140
) {
4241
parent::__construct();
4342
}
4443

4544
/**
45+
* @return AbstractLanguageModelTask
4646
* @since 28.0.0
4747
*/
48-
public function getRequestId(): int {
49-
return $this->requestId;
50-
}
51-
52-
53-
/**
54-
* @since 28.0.0
55-
*/
56-
public function getUserId(): ?string {
57-
return $this->userId;
58-
}
59-
60-
/**
61-
* @since 28.0.0
62-
*/
63-
public function getAppId(): string {
64-
return $this->appId;
48+
public function getTask(): AbstractLanguageModelTask {
49+
return $this->task;
6550
}
6651
}

lib/public/LanguageModel/Events/PromptFailedEvent.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

lib/public/LanguageModel/Events/PromptSuccessfulEvent.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

lib/public/LanguageModel/Events/SummaryFailedEvent.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

lib/public/LanguageModel/Events/SummarySuccessfulEvent.php

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace OCP\LanguageModel\Events;
4+
5+
use OCP\LanguageModel\AbstractLanguageModelTask;
6+
7+
/**
8+
* @since 28.0.0
9+
*/
10+
class TaskFailedEvent extends AbstractLanguageModelEvent {
11+
12+
public function __construct(AbstractLanguageModelTask $task,
13+
private string $errorMessage) {
14+
parent::__construct($task);
15+
}
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getErrorMessage(): string {
21+
return $this->errorMessage;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace OCP\LanguageModel\Events;
4+
5+
use OCP\LanguageModel\AbstractLanguageModelTask;
6+
7+
/**
8+
* @since 28.0.0
9+
*/
10+
class TaskSuccessfulEvent extends AbstractLanguageModelEvent {
11+
12+
public function __construct(AbstractLanguageModelTask $task,
13+
private string $output) {
14+
parent::__construct($task);
15+
}
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getErrorMessage(): string {
21+
return $this->output;
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace OCP\LanguageModel;
4+
5+
use RuntimeException;
6+
7+
class FreePromptTask extends AbstractLanguageModelTask {
8+
9+
/**
10+
* @param ILanguageModelProvider $provider
11+
* @throws RuntimeException
12+
* @return string
13+
*/
14+
public function visitProvider(ILanguageModelProvider $provider): string {
15+
$this->setStatus(self::STATUS_RUNNING);
16+
try {
17+
$output = $provider->prompt($this->getInput());
18+
} catch (RuntimeException $e) {
19+
$this->setStatus(self::STATUS_FAILED);
20+
throw $e;
21+
}
22+
$this->setStatus(self::STATUS_SUCCESSFUL);
23+
return $output;
24+
}
25+
}

lib/public/LanguageModel/ILanguageModelManager.php

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
namespace OCP\LanguageModel;
2828

2929
use InvalidArgumentException;
30+
use OCP\LanguageModel\Events\AbstractLanguageModelEvent;
3031
use OCP\PreConditionNotMetException;
3132
use RuntimeException;
3233

@@ -37,53 +38,25 @@ interface ILanguageModelManager {
3738
public function hasProviders(): bool;
3839

3940
/**
41+
* @return string[]
4042
* @since 28.0.0
4143
*/
42-
public function hasSummaryProviders(): bool;
44+
public function getAvailableTasks(): array;
4345

4446
/**
45-
* @param string $prompt The prompt to call the Language model with
46-
* @returns string The output
47-
* @throws PreConditionNotMetException If no provider was registered but this method was still called
47+
* @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
4848
* @throws InvalidArgumentException If the file could not be found or is not of a supported type
4949
* @throws RuntimeException If the transcription failed for other reasons
5050
* @since 28.0.0
5151
*/
52-
public function prompt(string $prompt): string;
52+
public function runTask(AbstractLanguageModelTask $task): AbstractLanguageModelEvent;
5353

5454
/**
5555
* Will schedule an LLM inference process in the background. The result will become available
56-
* with the \OCP\LanguageModel\Events\PromptFinishedEvent
56+
* with the \OCP\LanguageModel\Events\TaskFinishedEvent
5757
*
58-
* @param string $prompt The prompt to call the Language model with
59-
* @param ?string $userId The user that triggered this request (only for convenience, will be available on the TranscriptEvents)
60-
* @param string $appId The app that triggered this request (only for convenience, will be available on the TranscriptEvents)
61-
* @returns int The id of the prompt request
62-
* @throws PreConditionNotMetException If no provider was registered but this method was still called
58+
* @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
6359
* @since 28.0.0
6460
*/
65-
public function schedulePrompt(string $prompt, ?string $userId, string $appId): int;
66-
67-
/**
68-
* Will schedule an LLM inference process in the background. The result will become available
69-
* with the \OCP\LanguageModel\Events\PromptFinishedEvent
70-
*
71-
* @param string $text The text to summarize
72-
* @param ?string $userId The user that triggered this request (only for convenience, will be available on the TranscriptEvents)
73-
* @param string $appId The app that triggered this request (only for convenience, will be available on the TranscriptEvents)
74-
* @returns int The id of the prompt request
75-
* @throws PreConditionNotMetException If no summary provider was registered but this method was still called
76-
* @since 28.0.0
77-
*/
78-
public function scheduleSummary(string $text, ?string $userId, string $appId): int;
79-
80-
/**
81-
* @param string $text The text to summarize
82-
* @returns string The output
83-
* @throws PreConditionNotMetException If no summary provider was registered but this method was still called
84-
* @throws InvalidArgumentException If the file could not be found or is not of a supported type
85-
* @throws RuntimeException If the transcription failed for other reasons
86-
* @since 28.0.0
87-
*/
88-
public function summarize(string $text): string;
61+
public function scheduleTask(AbstractLanguageModelTask $task) : void;
8962
}

0 commit comments

Comments
 (0)