Skip to content

Commit

Permalink
use max_completion_tokens instead of deprecated max_tokens by default…
Browse files Browse the repository at this point in the history
…, add admin option to choose

Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Jan 15, 2025
1 parent dc3a767 commit eb16453
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,6 @@ public function createChatCompletion(
throw new Exception($this->l10n->t('Text generation quota exceeded'), Http::STATUS_TOO_MANY_REQUESTS);
}

$maxTokensLimit = $this->openAiSettingsService->getMaxTokens();
if ($maxTokens === null || $maxTokens > $maxTokensLimit) {
$maxTokens = $maxTokensLimit;
}

$messages = [];
if ($systemPrompt !== null) {
$messages[] = ['role' => 'system', 'content' => $systemPrompt];
Expand Down Expand Up @@ -419,9 +414,20 @@ public function createChatCompletion(
$params = [
'model' => $model === Application::DEFAULT_MODEL_ID ? Application::DEFAULT_COMPLETION_MODEL_ID : $model,
'messages' => $messages,
'max_tokens' => $maxTokens,
'n' => $n,
];

$maxTokensLimit = $this->openAiSettingsService->getMaxTokens();
if ($maxTokens === null || $maxTokens > $maxTokensLimit) {
$maxTokens = $maxTokensLimit;
}
if ($this->openAiSettingsService->getUseMaxCompletionTokensParam()) {
// max_tokens is now deprecated https://platform.openai.com/docs/api-reference/chat/create
$params['max_completion_tokens'] = $maxTokens;
} else {
$params['max_tokens'] = $maxTokens;
}

if ($tools !== null) {
$params['tools'] = $tools;
}
Expand Down
20 changes: 20 additions & 0 deletions lib/Service/OpenAiSettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class OpenAiSettingsService {
'default_image_size' => 'string',
'chunk_size' => 'integer',
'max_tokens' => 'integer',
'use_max_completion_tokens_param' => 'boolean',
'llm_extra_params' => 'string',
'quota_period' => 'integer',
'quotas' => 'array',
Expand Down Expand Up @@ -268,6 +269,7 @@ public function getAdminConfig(): array {
'default_image_size' => $this->getAdminDefaultImageSize(),
'chunk_size' => strval($this->getChunkSize()),
'max_tokens' => $this->getMaxTokens(),
'use_max_completion_tokens_param' => $this->getUseMaxCompletionTokensParam(),
'llm_extra_params' => $this->getLlmExtraParams(),
// Updated to get max tokens
'quota_period' => $this->getQuotaPeriod(),
Expand Down Expand Up @@ -301,6 +303,13 @@ public function getUserConfig(string $userId): array {
];
}

/**
* @return bool
*/
public function getUseMaxCompletionTokensParam(): bool {
return $this->appConfig->getValueString(Application::APP_ID, 'use_max_completion_tokens_param', '1') === '1';
}

/**
* @return bool
*/
Expand Down Expand Up @@ -610,6 +619,9 @@ public function setAdminConfig(array $adminConfig): void {
if (isset($adminConfig['quotas'])) {
$this->setQuotas($adminConfig['quotas']);
}
if (isset($adminConfig['use_max_completion_tokens_param'])) {
$this->setUseMaxCompletionParam($adminConfig['use_max_completion_tokens_param']);
}
if (isset($adminConfig['translation_provider_enabled'])) {
$this->setTranslationProviderEnabled($adminConfig['translation_provider_enabled']);
}
Expand Down Expand Up @@ -661,6 +673,14 @@ public function setUserConfig(string $userId, array $userConfig): void {
}
}

/**
* @param bool $enabled
* @return void
*/
public function setUseMaxCompletionParam(bool $enabled): void {
$this->appConfig->setValueString(Application::APP_ID, 'use_max_completion_tokens_param', $enabled ? '1' : '0');
}

/**
* @param bool $enabled
* @return void
Expand Down
5 changes: 5 additions & 0 deletions src/components/AdminSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@
</template>
</NcButton>
</div>
<NcCheckboxRadioSwitch
:checked="state.use_max_completion_tokens_param"
@update:checked="onCheckboxChanged($event, 'use_max_completion_tokens_param', false)">
{{ t('integration_openai', 'Use "{newParam}" parameter instead of the deprecated "{deprecatedParam}"', { newParam: 'max_completion_tokens', deprecatedParam: 'max_tokens' }) }}
</NcCheckboxRadioSwitch>
</div>
<div>
<h2>
Expand Down

0 comments on commit eb16453

Please sign in to comment.