Skip to content

Commit 18bf07b

Browse files
committed
Added an AI platform selector for settings
1 parent c9d2044 commit 18bf07b

4 files changed

Lines changed: 72 additions & 10 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
24+
namespace App\Form\Settings;
25+
26+
use App\Services\AI\AIPlatformRegistry;
27+
use App\Services\AI\AIPlatforms;
28+
use Symfony\Component\Form\AbstractType;
29+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
30+
use Symfony\Component\Form\Extension\Core\Type\EnumType;
31+
use Symfony\Component\OptionsResolver\OptionsResolver;
32+
33+
final class AiPlatformChoiceType extends AbstractType
34+
{
35+
public function __construct(private readonly AIPlatformRegistry $platformRegistry)
36+
{
37+
}
38+
39+
public function getParent(): ?string
40+
{
41+
return EnumType::class;
42+
}
43+
44+
public function configureOptions(OptionsResolver $resolver): void
45+
{
46+
$choices = array_map(static fn(string $val) => AIPlatforms::from($val), array_keys($this->platformRegistry->getEnabledPlatforms()));
47+
48+
$resolver->setDefaults([
49+
'class' => AIPlatforms::class,
50+
'choices' => $choices,
51+
'required' => false,
52+
]);
53+
}
54+
}

src/Services/AI/AIPlatforms.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
use App\Settings\AISettings\LMStudioSettings;
2727
use App\Settings\AISettings\OpenRouterSettings;
28+
use Symfony\Contracts\Translation\TranslatableInterface;
29+
use Symfony\Contracts\Translation\TranslatorInterface;
2830

29-
enum AIPlatforms: string
31+
enum AIPlatforms: string implements TranslatableInterface
3032
{
3133
case OPENROUTER = 'openrouter';
3234
case LMSTUDIO = 'lmstudio';
@@ -54,4 +56,11 @@ public function toSettingsClass(): string
5456
default => throw new \InvalidArgumentException(sprintf('No settings class defined for AI platform "%s".', $this->name)),
5557
};
5658
}
59+
60+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
61+
{
62+
$key = 'settings.ai.' . $this->value;
63+
64+
return $translator->trans($key, locale: $locale);
65+
}
5766
}

src/Services/InfoProviderSystem/Providers/AIInfoExtractor.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@
2626

2727
use App\Exceptions\ProviderIDNotSupportedException;
2828
use App\Services\AI\AIPlatformRegistry;
29-
use App\Services\AI\AIPlatforms;
3029
use App\Services\InfoProviderSystem\DTOJsonSchemaConverter;
3130
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
3231
use App\Settings\InfoProviderSystem\AIExtractorSettings;
3332
use Symfony\AI\Platform\Message\Message;
3433
use Symfony\AI\Platform\Message\MessageBag;
35-
use Symfony\AI\Platform\PlatformInterface;
36-
use Symfony\Component\DependencyInjection\Attribute\Autowire;
3734
use Symfony\Contracts\HttpClient\HttpClientInterface;
3835

3936
final class AIInfoExtractor implements InfoProviderInterface
@@ -76,8 +73,7 @@ public function getProviderKey(): string
7673

7774
public function isActive(): bool
7875
{
79-
return true;
80-
//return !empty($this->settings->apiKey) && $this->settings->enabled;
76+
return $this->settings->platform !== null && $this->settings->model !== '';
8177
}
8278

8379
public function searchByKeyword(string $keyword): array
@@ -172,10 +168,10 @@ private function callLLM(string $htmlContent, string $url): array
172168
);
173169

174170
try {
175-
$aiPlatform = $this->AIPlatformRegistry->getPlatform(AIPlatforms::OPENROUTER);
171+
$aiPlatform = $this->AIPlatformRegistry->getPlatform($this->settings->platform ?? throw new \RuntimeException('No AI platform selected') );
176172

177173
//'openai/gpt-5-mini'
178-
$result = $aiPlatform->invoke('openrouter/auto', $input, [
174+
$result = $aiPlatform->invoke($this->settings->model, $input, [
179175
'response_format' => [
180176
'type' => 'json_schema',
181177
'json_schema' => $this->jsonSchemaConverter->getJSONSchema(),

src/Settings/InfoProviderSystem/AIExtractorSettings.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
namespace App\Settings\InfoProviderSystem;
2525

26+
use App\Form\Settings\AiPlatformChoiceType;
27+
use App\Services\AI\AIPlatforms;
2628
use App\Settings\SettingsIcon;
2729
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
2830
use Jbtronics\SettingsBundle\Settings\Settings;
@@ -36,10 +38,11 @@ class AIExtractorSettings
3638
{
3739
use SettingsTrait;
3840

39-
#[SettingsParameter(label: new TM("settings.ips.ai_extractor.api_key"), description: new TM("settings.ips.ai_extractor.api_key.description"),
41+
#[SettingsParameter(label: new TM("settings.ips.ai_extractor.ai_platform"), description: new TM("settings.ips.ai_extractor.ai_platform.help"),
42+
formType: AiPlatformChoiceType::class,
4043
envVar: "string:PROVIDER_AI_EXTRACTOR_API_KEY", envVarMode: EnvVarMode::OVERWRITE
4144
)]
42-
public ?string $apiKey = null;
45+
public ?AIPlatforms $platform = null;
4346

4447
#[SettingsParameter(label: new TM("settings.ips.ai_extractor.model"), description: new TM("settings.ips.ai_extractor.model.description"),
4548
envVar: "string:PROVIDER_AI_EXTRACTOR_MODEL", envVarMode: EnvVarMode::OVERWRITE

0 commit comments

Comments
 (0)