Added an AI platform selector for settings

This commit is contained in:
Jan Böhmer 2026-04-26 01:10:00 +02:00
parent c9d2044949
commit 18bf07b19f
4 changed files with 72 additions and 10 deletions

View file

@ -0,0 +1,54 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Form\Settings;
use App\Services\AI\AIPlatformRegistry;
use App\Services\AI\AIPlatforms;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\OptionsResolver\OptionsResolver;
final class AiPlatformChoiceType extends AbstractType
{
public function __construct(private readonly AIPlatformRegistry $platformRegistry)
{
}
public function getParent(): ?string
{
return EnumType::class;
}
public function configureOptions(OptionsResolver $resolver): void
{
$choices = array_map(static fn(string $val) => AIPlatforms::from($val), array_keys($this->platformRegistry->getEnabledPlatforms()));
$resolver->setDefaults([
'class' => AIPlatforms::class,
'choices' => $choices,
'required' => false,
]);
}
}

View file

@ -25,8 +25,10 @@ namespace App\Services\AI;
use App\Settings\AISettings\LMStudioSettings;
use App\Settings\AISettings\OpenRouterSettings;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
enum AIPlatforms: string
enum AIPlatforms: string implements TranslatableInterface
{
case OPENROUTER = 'openrouter';
case LMSTUDIO = 'lmstudio';
@ -54,4 +56,11 @@ enum AIPlatforms: string
default => throw new \InvalidArgumentException(sprintf('No settings class defined for AI platform "%s".', $this->name)),
};
}
public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
$key = 'settings.ai.' . $this->value;
return $translator->trans($key, locale: $locale);
}
}

View file

@ -26,14 +26,11 @@ namespace App\Services\InfoProviderSystem\Providers;
use App\Exceptions\ProviderIDNotSupportedException;
use App\Services\AI\AIPlatformRegistry;
use App\Services\AI\AIPlatforms;
use App\Services\InfoProviderSystem\DTOJsonSchemaConverter;
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
use App\Settings\InfoProviderSystem\AIExtractorSettings;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\PlatformInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final class AIInfoExtractor implements InfoProviderInterface
@ -76,8 +73,7 @@ final class AIInfoExtractor implements InfoProviderInterface
public function isActive(): bool
{
return true;
//return !empty($this->settings->apiKey) && $this->settings->enabled;
return $this->settings->platform !== null && $this->settings->model !== '';
}
public function searchByKeyword(string $keyword): array
@ -172,10 +168,10 @@ final class AIInfoExtractor implements InfoProviderInterface
);
try {
$aiPlatform = $this->AIPlatformRegistry->getPlatform(AIPlatforms::OPENROUTER);
$aiPlatform = $this->AIPlatformRegistry->getPlatform($this->settings->platform ?? throw new \RuntimeException('No AI platform selected') );
//'openai/gpt-5-mini'
$result = $aiPlatform->invoke('openrouter/auto', $input, [
$result = $aiPlatform->invoke($this->settings->model, $input, [
'response_format' => [
'type' => 'json_schema',
'json_schema' => $this->jsonSchemaConverter->getJSONSchema(),

View file

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace App\Settings\InfoProviderSystem;
use App\Form\Settings\AiPlatformChoiceType;
use App\Services\AI\AIPlatforms;
use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
use Jbtronics\SettingsBundle\Settings\Settings;
@ -36,10 +38,11 @@ class AIExtractorSettings
{
use SettingsTrait;
#[SettingsParameter(label: new TM("settings.ips.ai_extractor.api_key"), description: new TM("settings.ips.ai_extractor.api_key.description"),
#[SettingsParameter(label: new TM("settings.ips.ai_extractor.ai_platform"), description: new TM("settings.ips.ai_extractor.ai_platform.help"),
formType: AiPlatformChoiceType::class,
envVar: "string:PROVIDER_AI_EXTRACTOR_API_KEY", envVarMode: EnvVarMode::OVERWRITE
)]
public ?string $apiKey = null;
public ?AIPlatforms $platform = null;
#[SettingsParameter(label: new TM("settings.ips.ai_extractor.model"), description: new TM("settings.ips.ai_extractor.model.description"),
envVar: "string:PROVIDER_AI_EXTRACTOR_MODEL", envVarMode: EnvVarMode::OVERWRITE