Skip to content
Open
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
18 changes: 10 additions & 8 deletions core/components/modai/src/API/Prompt/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Text extends API

public function post(ServerRequestInterface $request): void
{
$contextKey = '';

if (!$this->modx->hasPermission('modai_client_text')) {
throw APIException::unauthorized();
}
Expand Down Expand Up @@ -60,7 +62,7 @@ public function post(ServerRequestInterface $request): void
if (!$resource) {
throw new LexiconException('modai.error.no_resource_found');
}

$contextKey = $resource->get('context_key');
$content = $resource->getContent();

if (empty($content)) {
Expand All @@ -71,13 +73,13 @@ public function post(ServerRequestInterface $request): void
$systemInstructions = [];

$stream = intval(Settings::getTextSetting($this->modx, $field, 'stream', $namespace)) === 1;
$model = Settings::getTextSetting($this->modx, $field, 'model', $namespace);
$temperature = (float)Settings::getTextSetting($this->modx, $field, 'temperature', $namespace);
$maxTokens = (int)Settings::getTextSetting($this->modx, $field, 'max_tokens', $namespace);
$output = Settings::getTextSetting($this->modx, $field, 'base_output', $namespace, false);
$base = Settings::getTextSetting($this->modx, $field, 'base_prompt', $namespace, false);
$fieldPrompt = Settings::getTextSetting($this->modx, $field, 'prompt', $namespace);
$customOptions = Settings::getTextSetting($this->modx, $field, 'custom_options', $namespace, false);
$model = Settings::getTextSetting($this->modx, $field, 'model', $namespace, 'openai/gpt-4o-mini', $contextKey);
$temperature = (float)Settings::getTextSetting($this->modx, $field, 'temperature', $namespace , 0, $contextKey);
$maxTokens = (int)Settings::getTextSetting($this->modx, $field, 'max_tokens', $namespace, 0, $contextKey);
$output = Settings::getTextSetting($this->modx, $field, 'base_output', $namespace, false, $contextKey);
$base = Settings::getTextSetting($this->modx, $field, 'base_prompt', $namespace, false, $contextKey);
$fieldPrompt = Settings::getTextSetting($this->modx, $field, 'prompt', $namespace, false, $contextKey);
$customOptions = Settings::getTextSetting($this->modx, $field, 'custom_options', $namespace, false, $contextKey);

if (!empty($output)) {
$systemInstructions[] = $output;
Expand Down
28 changes: 19 additions & 9 deletions core/components/modai/src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,38 @@ class Settings
* @param string $setting
* @return string|null
*/
private static function getOption(modX $modx, string $namespace, string $field, string $area, string $setting): ?string
private static function getOption(modX $modx, string $namespace, string $field, string $area, string $setting, string $contextKey = 'web'): ?string
{
$handler = $modx;

if (!empty($contextKey)) {

$context = $modx->getContext($contextKey);
if ($context) {
$handler = $context;
}
}

if (!empty($field)) {
$value = $modx->getOption("#sys.$field.$area.$setting");
$value = $handler->getOption("#sys.$field.$area.$setting");
if ($value !== null && $value !== '') {
return $value;
}
}

$value = $modx->getOption("#sys.global.$area.$setting");
$value = $handler->getOption("#sys.global.$area.$setting");
if ($value !== null && $value !== '') {
return $value;
}

if (!empty($field)) {
$value = $modx->getOption("$namespace.$field.$area.$setting");
$value = $handler->getOption("$namespace.$field.$area.$setting");
if ($value !== null && $value !== '') {
return $value;
}
}

$value = $modx->getOption("$namespace.global.$area.$setting");
$value = $handler->getOption("$namespace.global.$area.$setting");
if ($value !== null && $value !== '') {
return $value;
}
Expand All @@ -56,13 +66,13 @@ private static function getOption(modX $modx, string $namespace, string $field,
}

if (!empty($field)) {
$value = $modx->getOption("modai.$field.$area.$setting");
$value = $handler->getOption("modai.$field.$area.$setting");
if ($value !== null && $value !== '') {
return $value;
}
}

$value = $modx->getOption("modai.global.$area.$setting");
$value = $handler->getOption("modai.global.$area.$setting");
if ($value !== null && $value !== '') {
return $value;
}
Expand All @@ -73,9 +83,9 @@ private static function getOption(modX $modx, string $namespace, string $field,
/**
* @throws RequiredSettingException
*/
public static function getTextSetting(modX $modx, string $field, string $setting, string $namespace = 'modai', bool $required = true): ?string
public static function getTextSetting(modX $modx, string $field, string $setting, string $namespace = 'modai', bool $required = true, string $contextKey = ''): ?string
{
$value = self::getOption($modx, $namespace, $field, 'text', $setting);
$value = self::getOption($modx, $namespace, $field, 'text', $setting, $contextKey);

if ($required && ($value === null || $value === '')) {
throw new RequiredSettingException("modai.global.text.$setting");
Expand Down