|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Platform\Factory; |
| 13 | + |
| 14 | +use Symfony\AI\Platform\Provider\ProviderConfig; |
| 15 | +use Symfony\AI\Platform\Transport\Dsn; |
| 16 | + |
| 17 | +final class ProviderConfigFactory |
| 18 | +{ |
| 19 | + public static function fromDsn(string|Dsn $dsn): ProviderConfig |
| 20 | + { |
| 21 | + $dsn = \is_string($dsn) ? Dsn::fromString($dsn) : $dsn; |
| 22 | + |
| 23 | + $provider = strtolower($dsn->getProvider()); |
| 24 | + if ('' === $provider) { |
| 25 | + throw new \InvalidArgumentException('DSN must include a provider (e.g. "ai+openai://...").'); |
| 26 | + } |
| 27 | + |
| 28 | + $host = $dsn->getHost(); |
| 29 | + if ('' === $host) { |
| 30 | + $host = self::defaultHostOrFail($provider); |
| 31 | + } |
| 32 | + |
| 33 | + $scheme = 'https'; |
| 34 | + $port = $dsn->getPort(); |
| 35 | + $baseUri = $scheme.'://'.$host.($port ? ':'.$port : ''); |
| 36 | + |
| 37 | + $q = $dsn->getQuery(); |
| 38 | + |
| 39 | + $headers = []; |
| 40 | + |
| 41 | + if (isset($q['headers']) && \is_array($q['headers'])) { |
| 42 | + foreach ($q['headers'] as $hk => $hv) { |
| 43 | + $headers[$hk] = $hv; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + foreach ($q as $k => $v) { |
| 48 | + if (preg_match('/^headers\[(.+)\]$/', (string) $k, $m)) { |
| 49 | + $headers[$m[1]] = $v; |
| 50 | + continue; |
| 51 | + } |
| 52 | + if (str_starts_with((string) $k, 'headers_')) { |
| 53 | + $hk = substr((string) $k, \strlen('headers_')); |
| 54 | + if ('' !== $hk) { |
| 55 | + $headers[$hk] = $v; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + $options = array_filter([ |
| 61 | + 'model' => $q['model'] ?? null, |
| 62 | + 'version' => $q['version'] ?? null, |
| 63 | + 'deployment' => $q['deployment'] ?? null, |
| 64 | + 'organization' => $q['organization'] ?? null, |
| 65 | + 'location' => $q['location'] ?? ($q['region'] ?? null), |
| 66 | + 'timeout' => isset($q['timeout']) ? (int) $q['timeout'] : null, |
| 67 | + 'verify_peer' => isset($q['verify_peer']) ? self::toBool($q['verify_peer']) : null, |
| 68 | + 'proxy' => $q['proxy'] ?? null, |
| 69 | + ], static fn ($v) => null !== $v && '' !== $v); |
| 70 | + |
| 71 | + switch ($provider) { |
| 72 | + case 'azure': |
| 73 | + $engine = strtolower((string) ($q['engine'] ?? 'openai')); |
| 74 | + if (!\in_array($engine, ['openai', 'meta'], true)) { |
| 75 | + throw new \InvalidArgumentException(\sprintf('Unsupported Azure engine "%s". Supported: "openai", "meta".', $engine)); |
| 76 | + } |
| 77 | + $options['engine'] = $engine; |
| 78 | + |
| 79 | + if ('' === $dsn->getHost()) { |
| 80 | + throw new \InvalidArgumentException('Azure DSN requires host: "<resource>.openai.azure.com" or "<resource>.meta.azure.com".'); |
| 81 | + } |
| 82 | + if (!isset($options['deployment']) || '' === $options['deployment']) { |
| 83 | + throw new \InvalidArgumentException('Azure DSN requires "deployment" query param.'); |
| 84 | + } |
| 85 | + if (!isset($options['version']) || '' === $options['version']) { |
| 86 | + throw new \InvalidArgumentException('Azure DSN requires "version" query param.'); |
| 87 | + } |
| 88 | + break; |
| 89 | + |
| 90 | + case 'openai': |
| 91 | + case 'anthropic': |
| 92 | + case 'gemini': |
| 93 | + case 'vertex': |
| 94 | + case 'ollama': |
| 95 | + break; |
| 96 | + |
| 97 | + default: |
| 98 | + throw new \InvalidArgumentException(\sprintf('Unknown AI provider "%s".', $provider)); |
| 99 | + } |
| 100 | + |
| 101 | + return new ProviderConfig( |
| 102 | + provider: $provider, |
| 103 | + baseUri: $baseUri, |
| 104 | + apiKey: $dsn->getUser(), |
| 105 | + options: $options, |
| 106 | + headers: $headers |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + private static function toBool(mixed $value): bool |
| 111 | + { |
| 112 | + if (\is_bool($value)) { |
| 113 | + return $value; |
| 114 | + } |
| 115 | + $v = strtolower((string) $value); |
| 116 | + |
| 117 | + return \in_array($v, ['1', 'true', 'yes', 'on'], true); |
| 118 | + } |
| 119 | + |
| 120 | + private static function defaultHostOrFail(string $provider): string |
| 121 | + { |
| 122 | + return match ($provider) { |
| 123 | + 'openai' => 'api.openai.com', |
| 124 | + 'anthropic' => 'api.anthropic.com', |
| 125 | + 'gemini' => 'generativelanguage.googleapis.com', |
| 126 | + 'vertex' => 'us-central1-aiplatform.googleapis.com', |
| 127 | + 'ollama' => 'localhost', |
| 128 | + 'azure' => throw new \InvalidArgumentException('Azure DSN must specify host (e.g. "<resource>.openai.azure.com").'), |
| 129 | + default => throw new \InvalidArgumentException(\sprintf('Unknown AI provider "%s".', $provider)), |
| 130 | + }; |
| 131 | + } |
| 132 | +} |
0 commit comments