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
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Application extends App implements IBootstrap {
public const DEFAULT_QUOTA_PERIOD = 30;
public const MAX_QUOTA_PERIOD_DAYS = 3650;
public const MAX_QUOTA_PERIOD_MONTHS = 120;
public const DEFAULT_SUMMARY_SYSTEM_PROMPT = '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.';
public const DEFAULT_QUOTA_CONFIG = ['length' => self::DEFAULT_QUOTA_PERIOD, 'unit' => 'day', 'day' => 1];

public const DEFAULT_OPENAI_TEXT_GENERATION_TIME = 10; // seconds
Expand Down
29 changes: 28 additions & 1 deletion lib/Service/OpenAiSettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class OpenAiSettingsService {
private const ADMIN_CONFIG_TYPES = [
'quota_period' => 'array',
'usage_storage_time' => 'integer',
'summary_system_prompt' => 'string',
];

private const USER_CONFIG_TYPES = [
Expand Down Expand Up @@ -135,6 +136,19 @@ public function getUsageStorageTime(): int {
return $this->appConfig->getValueInt(Application::APP_ID, 'usage_storage_time', Application::DEFAULT_QUOTA_PERIOD, lazy: true);
}

/**
* System prompt for the summary provider. Empty falls back to the default unless $raw is true.
*/
public function getSummarySystemPrompt(bool $raw = false): string {
$default = $raw ? '' : Application::DEFAULT_SUMMARY_SYSTEM_PROMPT;
return $this->appConfig->getValueString(
Application::APP_ID,
'summary_system_prompt',
$default,
lazy: true
) ?: $default;
}

/**
* @param string|null $userId
* @return string
Expand All @@ -146,12 +160,13 @@ public function getUserSTTLanguage(?string $userId): string {
/**
* Get the instance-wide admin config for the settings page
*
* @return array{quota_period: array, usage_storage_time: int}
* @return array{quota_period: array, usage_storage_time: int, summary_system_prompt: string}
*/
public function getAdminConfig(): array {
return [
'quota_period' => $this->getQuotaPeriod(),
'usage_storage_time' => $this->getUsageStorageTime(),
'summary_system_prompt' => $this->getSummarySystemPrompt(true),
];
}

Expand Down Expand Up @@ -218,6 +233,15 @@ public function setUsageStorageTime(int $usageStorageTime): void {
$this->appConfig->setValueInt(Application::APP_ID, 'usage_storage_time', $usageStorageTime, lazy: true);
}

public function setSummarySystemPrompt(string $summarySystemPrompt): void {
$this->appConfig->setValueString(
Application::APP_ID,
'summary_system_prompt',
$summarySystemPrompt,
lazy: true
);
}

/**
* @param string $userId
* @param string $language
Expand Down Expand Up @@ -250,6 +274,9 @@ public function setAdminConfig(array $adminConfig): void {
if (isset($adminConfig['usage_storage_time'])) {
$this->setUsageStorageTime($adminConfig['usage_storage_time']);
}
if (isset($adminConfig['summary_system_prompt'])) {
$this->setSummarySystemPrompt($adminConfig['summary_system_prompt']);
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/TaskProcessing/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\ChunkService;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\OpenAiSettingsService;
use OCA\OpenAi\Service\ServiceConfig;
use OCA\OpenAi\Service\ServicesService;
use OCA\OpenAi\Service\TranslateService;
Expand All @@ -35,6 +36,7 @@ class ProviderFactory {
public function __construct(
private ServicesService $servicesService,
private OpenAiAPIService $openAiAPIService,
private OpenAiSettingsService $openAiSettingsService,
private ChunkService $chunkService,
private TranslateService $translateService,
private WatermarkingService $watermarkingService,
Expand Down Expand Up @@ -80,7 +82,7 @@ private function getTextProviders(ServiceConfig $service, string $model): array
new TextToTextProvider($this->openAiAPIService, $this->l, $service, $model),
new TextToTextChatProvider($this->openAiAPIService, $this->l, $service, $model),
new TextToTextChatWithToolsProvider($this->openAiAPIService, $this->l, $service, $model),
new SummaryProvider($this->openAiAPIService, $this->l, $this->chunkService, $service, $model),
new SummaryProvider($this->openAiAPIService, $this->l, $this->chunkService, $this->openAiSettingsService, $service, $model),
new HeadlineProvider($this->openAiAPIService, $this->l, $service, $model),
new TopicsProvider($this->openAiAPIService, $this->l, $this->chunkService, $this->logger, $service, $model),
new ContextWriteProvider($this->openAiAPIService, $this->chunkService, $this->l, $service, $model),
Expand Down
5 changes: 3 additions & 2 deletions lib/TaskProcessing/SummaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use OCA\OpenAi\Service\ChunkService;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\OpenAiSettingsService;
use OCA\OpenAi\Service\ServiceConfig;
use OCP\IL10N;
use OCP\TaskProcessing\EShapeType;
Expand All @@ -28,6 +29,7 @@ public function __construct(
private OpenAiAPIService $openAiAPIService,
private IL10N $l,
private ChunkService $chunkService,
private OpenAiSettingsService $openAiSettingsService,
private ServiceConfig $service,
private string $model,
) {
Expand Down Expand Up @@ -142,8 +144,7 @@ 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. ';
$summarySystemPrompt = $this->openAiSettingsService->getSummarySystemPrompt() . ' ';
if (isset($input['format'])) {
if ($input['format'] === 'paragraph') {
$summarySystemPrompt .= 'Return the summary as a paragraph. ';
Expand Down
24 changes: 24 additions & 0 deletions src/components/AdminSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@
</NcButton>
</div>

<h3>{{ t('integration_openai', 'Text generation') }}</h3>
<div class="line line--full">
<NcTextArea
id="summary-system-prompt"
v-model="state.summary_system_prompt"
class="input input--full"
:label="t('integration_openai', 'Summary system prompt')"
:helper-text="t('integration_openai', 'System prompt used when generating text summaries. Leave empty to use the default prompt.')"
:rows="5"
resize="vertical"
@update:model-value="onInput()" />
</div>

<h3>{{ t('integration_openai', 'Usage limits') }}</h3>
<div class="line">
<QuotaPeriodPicker
Expand Down Expand Up @@ -115,6 +128,7 @@ import NcInputField from '@nextcloud/vue/components/NcInputField'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
import NcNoteCard from '@nextcloud/vue/components/NcNoteCard'
import NcSelect from '@nextcloud/vue/components/NcSelect'
import NcTextArea from '@nextcloud/vue/components/NcTextArea'

import axios from '@nextcloud/axios'
import { showError, showSuccess, showWarning } from '@nextcloud/dialogs'
Expand All @@ -141,6 +155,7 @@ export default {
NcLoadingIcon,
NcNoteCard,
NcSelect,
NcTextArea,
},

data() {
Expand Down Expand Up @@ -415,6 +430,7 @@ export default {
await this.saveAdminConfig({
quota_period: this.state.quota_period,
usage_storage_time: parseInt(this.state.usage_storage_time) || 1,
summary_system_prompt: this.state.summary_system_prompt,
})
}, 2000),
async saveAdminConfig(values) {
Expand Down Expand Up @@ -475,6 +491,14 @@ export default {
width: 300px;
}

.line--full {
width: 100%;
}

.line .input--full {
width: 100%;
}

.services {
display: flex;
flex-wrap: wrap;
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/Providers/OpenAiProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,12 @@ public function testChangeToneProvider(): void {
}

public function testSummaryProvider(): void {
$this->openAiSettingsService->setSummarySystemPrompt('This is a custom summary system prompt');
$summaryProvider = new SummaryProvider(
$this->openAiApiService,
$this->createMock(\OCP\IL10N::class),
$this->chunkService,
$this->openAiSettingsService,
$this->service,
self::TEXT_MODEL,
);
Expand Down Expand Up @@ -576,8 +578,7 @@ public function testSummaryProvider(): void {
$url = self::OPENAI_API_BASE . 'chat/completions';

$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. ';
$systemPrompt = 'This is a custom summary system prompt ';
$options['body'] = json_encode([
'model' => Application::DEFAULT_COMPLETION_MODEL_ID,
'messages' => [
Expand Down
Loading