Skip to content

Commit 2631ff4

Browse files
committed
Introduced subsystem to configure AI providers and allow services to select them dynamiclly
1 parent c0017d2 commit 2631ff4

10 files changed

Lines changed: 342 additions & 5 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
ai:
22
platform:
3-
lmstudio: null
3+
lmstudio:
4+
host_url: '%env(string:settings:ai_lmstudio:hostURL)%'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
ai:
22
platform:
33
openrouter:
4-
api_key: '%env(OPENROUTER_API_KEY)%'
4+
api_key: '%env(string:settings:ai_openrouter:apiKey)%'
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\Services\AI;
25+
26+
use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface;
27+
use Symfony\AI\Platform\PlatformInterface;
28+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
29+
30+
final readonly class AIPlatformRegistry
31+
{
32+
/**
33+
* All registered platforms, indexed by their service tag name (e.g. "openrouter", "lmstudio")
34+
* @var array<string, PlatformInterface> $allPlatforms
35+
*/
36+
private array $allPlatforms;
37+
38+
/**
39+
* All registered platforms, indexed by their AIPlatforms enum value (e.g. AIPlatforms::OPENROUTER->value)
40+
* @var array<string, PlatformInterface> $enabledPlatforms
41+
*/
42+
private array $enabledPlatforms;
43+
44+
public function __construct(
45+
SettingsManagerInterface $settingsManager,
46+
47+
#[AutowireIterator(tag: 'ai.platform', indexAttribute: 'name')]
48+
iterable $platforms,
49+
) {
50+
$this->allPlatforms = iterator_to_array($platforms);
51+
52+
//Check which platforms are active based on the settings and store them in $activePlatforms
53+
$tmp = [];
54+
foreach (AIPlatforms::cases() as $platform) {
55+
if (isset($this->allPlatforms[$platform->toServiceTagName()])) {
56+
//Check if the platform is active by calling its isActive() on the settings class
57+
$settings = $settingsManager->get($platform->toSettingsClass());
58+
if (!$settings->isAIPlatformEnabled()) {
59+
continue;
60+
}
61+
62+
$tmp[$platform->value] = $this->allPlatforms[$platform->toServiceTagName()];
63+
}
64+
}
65+
$this->enabledPlatforms = $tmp;
66+
}
67+
68+
public function getPlatform(AIPlatforms $platform): PlatformInterface
69+
{
70+
if (!isset($this->enabledPlatforms[$platform->value])) {
71+
throw new \InvalidArgumentException(sprintf('AI platform "%s" is not active or does not exist.', $platform->name));
72+
}
73+
74+
return $this->enabledPlatforms[$platform->value];
75+
}
76+
77+
/**
78+
* Check if the given platform is active (i.e. it is registered and its settings are properly configured)
79+
* @param AIPlatforms $platform
80+
* @return bool
81+
*/
82+
public function isEnabled(AIPlatforms $platform): bool
83+
{
84+
return isset($this->enabledPlatforms[$platform->value]);
85+
}
86+
87+
/**
88+
* Returns an array of all active platforms, indexed by their AIPlatforms enum value (e.g. AIPlatforms::OPENROUTER->value)
89+
* @return PlatformInterface[]
90+
*/
91+
public function getEnabledPlatforms(): array
92+
{
93+
return $this->enabledPlatforms;
94+
}
95+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Services\AI;
25+
26+
interface AIPlatformSettingsInterface
27+
{
28+
/**
29+
* Returns true, if the AI platform is enabled in the settings and can be used, false otherwise.
30+
* @return bool
31+
*/
32+
public function isAIPlatformEnabled(): bool;
33+
}

src/Services/AI/AIPlatforms.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Services\AI;
25+
26+
use App\Settings\AISettings\LMStudioSettings;
27+
use App\Settings\AISettings\OpenRouterSettings;
28+
29+
enum AIPlatforms: string
30+
{
31+
case OPENROUTER = 'openrouter';
32+
case LMSTUDIO = 'lmstudio';
33+
34+
/**
35+
* Returns the name attribute of the service tag for this platform, which is used to register the platform in the AIPlatformRegistry
36+
* @return string
37+
*/
38+
public function toServiceTagName(): string
39+
{
40+
return $this->value;
41+
}
42+
43+
/**
44+
* Returns the class name of the settings class for this platform, which implements AIPlatformSettingsInterface
45+
* @return string
46+
* @phpstan-return class-string<AIPlatformSettingsInterface>
47+
*/
48+
public function toSettingsClass(): string
49+
{
50+
return match ($this) {
51+
self::LMSTUDIO => LMStudioSettings::class,
52+
self::OPENROUTER => OpenRouterSettings::class,
53+
54+
default => throw new \InvalidArgumentException(sprintf('No settings class defined for AI platform "%s".', $this->name)),
55+
};
56+
}
57+
}

src/Services/InfoProviderSystem/Providers/AIInfoExtractor.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
namespace App\Services\InfoProviderSystem\Providers;
2626

2727
use App\Exceptions\ProviderIDNotSupportedException;
28+
use App\Services\AI\AIPlatformRegistry;
29+
use App\Services\AI\AIPlatforms;
2830
use App\Services\InfoProviderSystem\DTOJsonSchemaConverter;
2931
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
3032
use App\Settings\InfoProviderSystem\AIExtractorSettings;
@@ -45,8 +47,7 @@ final class AIInfoExtractor implements InfoProviderInterface
4547
public function __construct(
4648
HttpClientInterface $httpClient,
4749
private readonly AIExtractorSettings $settings,
48-
#[Autowire(service: "ai.traceable_platform.openrouter")]
49-
private readonly PlatformInterface $aiPlatform,
50+
private readonly AIPlatformRegistry $AIPlatformRegistry,
5051
private readonly DTOJsonSchemaConverter $jsonSchemaConverter,
5152
) {
5253
$this->httpClient = $httpClient->withOptions([
@@ -171,8 +172,10 @@ private function callLLM(string $htmlContent, string $url): array
171172
);
172173

173174
try {
175+
$aiPlatform = $this->AIPlatformRegistry->getPlatform(AIPlatforms::OPENROUTER);
176+
174177
//'openai/gpt-5-mini'
175-
$result = $this->aiPlatform->invoke('openrouter/auto', $input, [
178+
$result = $aiPlatform->invoke('openrouter/auto', $input, [
176179
'response_format' => 'json_schema',
177180
'json_schema' => $this->jsonSchemaConverter->getJSONSchema(),
178181
]);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Settings\AISettings;
25+
26+
use App\Settings\SettingsIcon;
27+
use Jbtronics\SettingsBundle\Settings\EmbeddedSettings;
28+
use Jbtronics\SettingsBundle\Settings\Settings;
29+
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
30+
use Symfony\Component\Translation\TranslatableMessage as TM;
31+
32+
#[Settings(label: new TM("settings.ai"), description: "settings.ai.help")]
33+
#[SettingsIcon("fa-brain")]
34+
class AISettings
35+
{
36+
use SettingsTrait;
37+
38+
#[EmbeddedSettings]
39+
public ?OpenRouterSettings $openRouter = null;
40+
41+
#[EmbeddedSettings]
42+
public ?LMStudioSettings $lmstudio = null;
43+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Settings\AISettings;
25+
26+
use App\Form\Type\APIKeyType;
27+
use App\Services\AI\AIPlatformSettingsInterface;
28+
use App\Settings\SettingsIcon;
29+
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
30+
use Jbtronics\SettingsBundle\Settings\Settings;
31+
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
32+
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
33+
use Symfony\Component\Form\Extension\Core\Type\UrlType;
34+
use Symfony\Component\Translation\TranslatableMessage as TM;
35+
36+
#[Settings(name: 'ai_lmstudio', label: new TM("settings.ai.openrouter"), description: "settings.ai.lmstudio.help")]
37+
#[SettingsIcon("fa-brain")]
38+
class LMStudioSettings implements AIPlatformSettingsInterface
39+
{
40+
use SettingsTrait;
41+
42+
#[SettingsParameter(label: new TM("settings.ai.lmstudio.hosturl"),
43+
formType: UrlType::class,
44+
envVar: "AI_LMSTUDIO_HOSTURL", envVarMode: EnvVarMode::OVERWRITE)]
45+
public ?string $hostURL = null;
46+
47+
public function isAIPlatformEnabled(): bool
48+
{
49+
return $this->hostURL !== null && $this->hostURL !== "";
50+
}
51+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Settings\AISettings;
25+
26+
use App\Form\Type\APIKeyType;
27+
use App\Services\AI\AIPlatformSettingsInterface;
28+
use App\Settings\SettingsIcon;
29+
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
30+
use Jbtronics\SettingsBundle\Settings\Settings;
31+
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
32+
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
33+
use Symfony\Component\Translation\TranslatableMessage as TM;
34+
35+
#[Settings(name: 'ai_openrouter', label: new TM("settings.ai.openrouter"), description: "settings.ai.openrouter.help")]
36+
#[SettingsIcon("fa-brain")]
37+
class OpenRouterSettings implements AIPlatformSettingsInterface
38+
{
39+
use SettingsTrait;
40+
41+
#[SettingsParameter(label: new TM("settings.ips.element14.apiKey"),
42+
formType: APIKeyType::class,
43+
formOptions: ["help_html" => true], envVar: "AI_OPENROUTER_KEY", envVarMode: EnvVarMode::OVERWRITE)]
44+
public ?string $apiKey = null;
45+
46+
public function isAIPlatformEnabled(): bool
47+
{
48+
return $this->apiKey !== null && $this->apiKey !== "";
49+
}
50+
}

src/Settings/AppSettings.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace App\Settings;
2525

26+
use App\Settings\AISettings\AISettings;
2627
use App\Settings\BehaviorSettings\BehaviorSettings;
2728
use App\Settings\InfoProviderSystem\InfoProviderSettings;
2829
use App\Settings\MiscSettings\MiscSettings;
@@ -50,6 +51,9 @@ class AppSettings
5051
#[EmbeddedSettings]
5152
public ?SynonymSettings $synonyms = null;
5253

54+
#[EmbeddedSettings]
55+
public ?AISettings $ai = null;
56+
5357
#[EmbeddedSettings()]
5458
public ?MiscSettings $miscSettings = null;
5559

0 commit comments

Comments
 (0)