Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions lib/TaskProcessing/SummaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCP\TaskProcessing\Exception\UserFacingProcessingException;
use OCP\TaskProcessing\ISynchronousProvider;
use OCP\TaskProcessing\ShapeDescriptor;
use OCP\TaskProcessing\ShapeEnumValue;
use OCP\TaskProcessing\TaskTypes\TextToTextSummary;

class SummaryProvider implements ISynchronousProvider {
Expand Down Expand Up @@ -58,6 +59,16 @@ public function getInputShapeDefaults(): array {

public function getOptionalInputShape(): array {
return [
'format' => new ShapeDescriptor(
$this->l->t('Format'),
$this->l->t('The format of the summary'),
EShapeType::Enum
),
'complexity' => new ShapeDescriptor(
$this->l->t('Complexity'),
$this->l->t('The complexity of the summary'),
EShapeType::Enum
),
'max_tokens' => new ShapeDescriptor(
$this->l->t('Maximum output words'),
$this->l->t('The maximum number of words/tokens that can be generated in the completion.'),
Expand All @@ -74,6 +85,17 @@ public function getOptionalInputShape(): array {
public function getOptionalInputShapeEnumValues(): array {
return [
'model' => $this->openAiAPIService->getModelEnumValues($this->userId),
'format' => [
new ShapeEnumValue($this->l->t('Auto'), 'auto'),
new ShapeEnumValue($this->l->t('One Sentence'), 'sentence'),
new ShapeEnumValue($this->l->t('One Paragraph'), 'paragraph'),
new ShapeEnumValue($this->l->t('Bullet Points'), 'bullet_points')
],
'complexity' => [
new ShapeEnumValue($this->l->t('Simple'), 'simple'),
new ShapeEnumValue($this->l->t('Medium'), 'medium'),
new ShapeEnumValue($this->l->t('Complex'), 'complex')
],
];
}

Expand All @@ -82,6 +104,8 @@ public function getOptionalInputShapeDefaults(): array {
return [
'max_tokens' => $this->openAiSettingsService->getMaxTokens(),
'model' => $adminModel,
'format' => 'auto',
'complexity' => 'medium',
];
}

Expand Down Expand Up @@ -129,9 +153,25 @@ public function process(?string $userId, array $input, callable $reportProgress)

try {
$completions = [];
$summarySystemPrompt = 'You are a helpful assistant that summarizes text in the same language as the text. '
. 'You should only return the summary without any additional information. ';
if (isset($input['format'])) {
if ($input['format'] === 'paragraph') {
$summarySystemPrompt .= 'Return the summary as a paragraph. ';
} elseif ($input['format'] === 'bullet_points') {
$summarySystemPrompt .= 'Return the summary as a list of bullet points. ';
} elseif ($input['format'] === 'sentence') {
$summarySystemPrompt .= 'Return the summary as a single sentence. Do not include more than one sentence. ';
}
}
if (isset($input['complexity'])) {
if ($input['complexity'] === 'complex') {
$summarySystemPrompt .= 'Use complex language and vocabulary appropriate for an expert in the subject. ';
} elseif ($input['complexity'] === 'simple') {
$summarySystemPrompt .= 'Use simple language and vocabulary appropriate for a 5 year old. ';
}
}
if ($this->openAiAPIService->isUsingOpenAi() || $this->openAiSettingsService->getChatEndpointEnabled()) {
$summarySystemPrompt = 'You are a helpful assistant that summarizes text in the same language as the text. '
. 'You should only return the summary without any additional information.';

foreach ($prompts as $p) {
$completion = $this->openAiAPIService->createChatCompletion($userId, $model, $p, $summarySystemPrompt, null, 1, $maxTokens);
Expand All @@ -143,11 +183,8 @@ public function process(?string $userId, array $input, callable $reportProgress)
}
}
} else {
$wrapSummaryPrompt = function (string $p): string {
return 'You are a helpful assistant that summarizes text in the same language as the text. '
. 'You should only return the summary without any additional information. '
. 'Here is the text to summarize:\n\n' . $p . '\n';
};
$wrapSummaryPrompt = static fn (string $p) => $summarySystemPrompt
. 'Here is the text to summarize:\n\n' . $p . '\n';

foreach (array_map($wrapSummaryPrompt, $prompts) as $p) {
$completions[] = $this->openAiAPIService->createCompletion($userId, $p, 1, $model, $maxTokens);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Providers/OpenAiProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public function testSummaryProvider(): void {

$options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']];
$systemPrompt = 'You are a helpful assistant that summarizes text in the same language as the text. '
. 'You should only return the summary without any additional information.';
. 'You should only return the summary without any additional information. ';
$options['body'] = json_encode([
'model' => Application::DEFAULT_COMPLETION_MODEL_ID,
'messages' => [
Expand Down
Loading