|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AdrienBrault\Instructrice\LLM; |
| 6 | + |
| 7 | +use AdrienBrault\Instructrice\LLM\Client\AnthropicLLM; |
| 8 | +use AdrienBrault\Instructrice\LLM\Client\GoogleLLM; |
| 9 | +use AdrienBrault\Instructrice\LLM\Client\OpenAiLLM; |
| 10 | +use InvalidArgumentException; |
| 11 | + |
| 12 | +use function Psl\Type\int; |
| 13 | +use function Psl\Type\literal_scalar; |
| 14 | +use function Psl\Type\optional; |
| 15 | +use function Psl\Type\shape; |
| 16 | +use function Psl\Type\string; |
| 17 | +use function Psl\Type\union; |
| 18 | + |
| 19 | +class DSNParser |
| 20 | +{ |
| 21 | + public function parse(string $dsn): LLMConfig |
| 22 | + { |
| 23 | + $parsedUrl = parse_url($dsn); |
| 24 | + |
| 25 | + if (! \is_array($parsedUrl)) { |
| 26 | + throw new InvalidArgumentException('The DSN could not be parsed'); |
| 27 | + } |
| 28 | + |
| 29 | + $parsedUrl = shape([ |
| 30 | + 'scheme' => string(), |
| 31 | + 'pass' => optional(string()), |
| 32 | + 'host' => string(), |
| 33 | + 'port' => optional(int()), |
| 34 | + 'path' => optional(string()), |
| 35 | + 'query' => string(), |
| 36 | + ], true)->coerce($parsedUrl); |
| 37 | + |
| 38 | + $apiKey = $parsedUrl['pass'] ?? null; |
| 39 | + $host = $parsedUrl['host']; |
| 40 | + $port = $parsedUrl['port'] ?? null; |
| 41 | + $path = $parsedUrl['path'] ?? null; |
| 42 | + $query = $parsedUrl['query']; |
| 43 | + |
| 44 | + $hostWithPort = $host . ($port === null ? '' : ':' . $port); |
| 45 | + |
| 46 | + $client = union( |
| 47 | + literal_scalar('openai'), |
| 48 | + literal_scalar('openai-http'), |
| 49 | + literal_scalar('anthropic'), |
| 50 | + literal_scalar('google') |
| 51 | + )->coerce($parsedUrl['scheme']); |
| 52 | + |
| 53 | + parse_str($query, $parsedQuery); |
| 54 | + $model = $parsedQuery['model']; |
| 55 | + $strategyName = $parsedQuery['strategy'] ?? null; |
| 56 | + $context = (int) ($parsedQuery['context'] ?? null); |
| 57 | + |
| 58 | + if (! \is_string($model)) { |
| 59 | + throw new InvalidArgumentException('The DSN "model" query string must be a string'); |
| 60 | + } |
| 61 | + |
| 62 | + if ($context <= 0) { |
| 63 | + throw new InvalidArgumentException('The DSN "context" query string must be a positive integer'); |
| 64 | + } |
| 65 | + |
| 66 | + $scheme = 'https'; |
| 67 | + |
| 68 | + $strategy = null; |
| 69 | + if ($strategyName === 'json') { |
| 70 | + $strategy = OpenAiJsonStrategy::JSON; |
| 71 | + } elseif ($strategyName === 'json_with_schema') { |
| 72 | + $strategy = OpenAiJsonStrategy::JSON_WITH_SCHEMA; |
| 73 | + } elseif ($strategyName === 'tool_any') { |
| 74 | + $strategy = OpenAiToolStrategy::ANY; |
| 75 | + } elseif ($strategyName === 'tool_auto') { |
| 76 | + $strategy = OpenAiToolStrategy::AUTO; |
| 77 | + } elseif ($strategyName === 'tool_function') { |
| 78 | + $strategy = OpenAiToolStrategy::FUNCTION; |
| 79 | + } |
| 80 | + |
| 81 | + if ($client === 'anthropic') { |
| 82 | + $headers = [ |
| 83 | + 'x-api-key' => $apiKey, |
| 84 | + ]; |
| 85 | + $llmClass = AnthropicLLM::class; |
| 86 | + $path ??= '/v1/messages'; |
| 87 | + } elseif ($client === 'google') { |
| 88 | + $headers = [ |
| 89 | + 'x-api-key' => $apiKey, |
| 90 | + ]; |
| 91 | + $llmClass = GoogleLLM::class; |
| 92 | + $path ??= '/v1beta/models'; |
| 93 | + } elseif ($client === 'openai' || $client === 'openai-http') { |
| 94 | + $path ??= '/v1/chat/completions'; |
| 95 | + $headers = $apiKey === null ? [] : [ |
| 96 | + 'Authorization' => 'Bearer ' . $apiKey, |
| 97 | + ]; |
| 98 | + |
| 99 | + $llmClass = OpenAiLLM::class; |
| 100 | + |
| 101 | + if ($client === 'openai-http') { |
| 102 | + $scheme = 'http'; |
| 103 | + } |
| 104 | + } else { |
| 105 | + throw new InvalidArgumentException(sprintf('Unknown client "%s", use one of %s', $client, implode(', ', ['openai', 'anthropic', 'google']))); |
| 106 | + } |
| 107 | + |
| 108 | + $uri = $scheme . '://' . $hostWithPort . $path; |
| 109 | + |
| 110 | + return new LLMConfig( |
| 111 | + $uri, |
| 112 | + $model, |
| 113 | + $context, |
| 114 | + $model, |
| 115 | + $hostWithPort, |
| 116 | + new Cost(), |
| 117 | + $strategy, |
| 118 | + headers: $headers, |
| 119 | + llmClass: $llmClass |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
0 commit comments