-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
502ad53
commit 184c7f8
Showing
9 changed files
with
280 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,6 +276,53 @@ $instructrice->get( | |
); | ||
``` | ||
|
||
#### DSN | ||
|
||
You may configure the LLM using a DSN: | ||
- the scheme is the provider: `openai`, `anthropic`, `google` | ||
- the password is the api key | ||
- the host, port and path are the api endpoints without the scheme | ||
- the query string: | ||
- `model` is the model name | ||
- `context` is the context window | ||
- `strategy` is the strategy to use: | ||
- `json` for json mode with the schema in the prompt only | ||
- `json_with_schema` for json mode with probably the completion perfectly constrained to the schema | ||
- `tool_any` | ||
- `tool_auto` | ||
- `tool_function` | ||
|
||
Examples: | ||
```php | ||
use AdrienBrault\Instructrice\InstructriceFactory; | ||
|
||
$instructrice = InstructriceFactory::create( | ||
defaultLlm: 'openai://:[email protected]/v1/chat/completions?model=gpt-3.5-turbo&strategy=tool_auto&context=16000' | ||
); | ||
|
||
$instructrice->get( | ||
..., | ||
llm: 'openai-http://localhost:11434?model=adrienbrault/nous-hermes2theta-llama3-8b&strategy=json&context=8000&scheme=http' | ||
); | ||
|
||
$instructrice->get( | ||
..., | ||
llm: 'openai://:[email protected]/inference/v1/chat/completions?model=accounts/fireworks/models/llama-v3-70b-instruct&context=8000&strategy=json_with_schema' | ||
); | ||
|
||
$instructrice->get( | ||
..., | ||
llm: 'google://:[email protected]/v1beta/models?model=gemini-1.5-flash&context=1000000' | ||
); | ||
|
||
$instructrice->get( | ||
..., | ||
llm: 'anthropic://:[email protected]?model=claude-3-haiku-20240307&context=200000' | ||
); | ||
``` | ||
|
||
#### LLMInterface | ||
|
||
You may also implement [LLMInterface](src/LLM/LLMInterface.php). | ||
|
||
## Acknowledgements | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AdrienBrault\Instructrice\LLM; | ||
|
||
use AdrienBrault\Instructrice\LLM\Client\AnthropicLLM; | ||
use AdrienBrault\Instructrice\LLM\Client\GoogleLLM; | ||
use AdrienBrault\Instructrice\LLM\Client\OpenAiLLM; | ||
use InvalidArgumentException; | ||
|
||
use function Psl\Type\int; | ||
use function Psl\Type\literal_scalar; | ||
use function Psl\Type\optional; | ||
use function Psl\Type\shape; | ||
use function Psl\Type\string; | ||
use function Psl\Type\union; | ||
|
||
class DSNParser | ||
{ | ||
public function parse(string $dsn): LLMConfig | ||
{ | ||
$parsedUrl = parse_url($dsn); | ||
|
||
if (! \is_array($parsedUrl)) { | ||
throw new InvalidArgumentException('The DSN could not be parsed'); | ||
} | ||
|
||
$parsedUrl = shape([ | ||
'scheme' => string(), | ||
'pass' => optional(string()), | ||
'host' => string(), | ||
'port' => optional(int()), | ||
'path' => optional(string()), | ||
'query' => string(), | ||
], true)->coerce($parsedUrl); | ||
|
||
$apiKey = $parsedUrl['pass'] ?? null; | ||
$host = $parsedUrl['host']; | ||
$port = $parsedUrl['port'] ?? null; | ||
$path = $parsedUrl['path'] ?? null; | ||
$query = $parsedUrl['query']; | ||
|
||
$hostWithPort = $host . ($port === null ? '' : ':' . $port); | ||
|
||
$client = union( | ||
literal_scalar('openai'), | ||
literal_scalar('openai-http'), | ||
literal_scalar('anthropic'), | ||
literal_scalar('google') | ||
)->coerce($parsedUrl['scheme']); | ||
|
||
parse_str($query, $parsedQuery); | ||
$model = $parsedQuery['model']; | ||
$strategyName = $parsedQuery['strategy'] ?? null; | ||
$context = (int) ($parsedQuery['context'] ?? null); | ||
|
||
if (! \is_string($model)) { | ||
throw new InvalidArgumentException('The DSN "model" query string must be a string'); | ||
} | ||
|
||
if ($context <= 0) { | ||
throw new InvalidArgumentException('The DSN "context" query string must be a positive integer'); | ||
} | ||
|
||
$scheme = 'https'; | ||
|
||
$strategy = null; | ||
if ($strategyName === 'json') { | ||
$strategy = OpenAiJsonStrategy::JSON; | ||
} elseif ($strategyName === 'json_with_schema') { | ||
$strategy = OpenAiJsonStrategy::JSON_WITH_SCHEMA; | ||
} elseif ($strategyName === 'tool_any') { | ||
$strategy = OpenAiToolStrategy::ANY; | ||
} elseif ($strategyName === 'tool_auto') { | ||
$strategy = OpenAiToolStrategy::AUTO; | ||
} elseif ($strategyName === 'tool_function') { | ||
$strategy = OpenAiToolStrategy::FUNCTION; | ||
} | ||
|
||
if ($client === 'anthropic') { | ||
$headers = [ | ||
'x-api-key' => $apiKey, | ||
]; | ||
$llmClass = AnthropicLLM::class; | ||
$path ??= '/v1/messages'; | ||
} elseif ($client === 'google') { | ||
$headers = [ | ||
'x-api-key' => $apiKey, | ||
]; | ||
$llmClass = GoogleLLM::class; | ||
$path ??= '/v1beta/models'; | ||
} elseif ($client === 'openai' || $client === 'openai-http') { | ||
$path ??= '/v1/chat/completions'; | ||
$headers = $apiKey === null ? [] : [ | ||
'Authorization' => 'Bearer ' . $apiKey, | ||
]; | ||
|
||
$llmClass = OpenAiLLM::class; | ||
|
||
if ($client === 'openai-http') { | ||
$scheme = 'http'; | ||
} | ||
} else { | ||
throw new InvalidArgumentException(sprintf('Unknown client "%s", use one of %s', $client, implode(', ', ['openai', 'anthropic', 'google']))); | ||
} | ||
|
||
$uri = $scheme . '://' . $hostWithPort . $path; | ||
|
||
return new LLMConfig( | ||
$uri, | ||
$model, | ||
$context, | ||
$model, | ||
$hostWithPort, | ||
new Cost(), | ||
$strategy, | ||
headers: $headers, | ||
llmClass: $llmClass | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.