-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for OpenRouter & Generic Models
- Loading branch information
Showing
6 changed files
with
177 additions
and
0 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
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,28 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenRouter\GenericModel; | ||
use PhpLlm\LlmChain\Bridge\OpenRouter\PlatformFactory; | ||
use PhpLlm\LlmChain\Chain; | ||
use PhpLlm\LlmChain\Model\Message\Message; | ||
use PhpLlm\LlmChain\Model\Message\MessageBag; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); | ||
|
||
if (empty($_ENV['OPENROUTER_KEY'])) { | ||
echo 'Please set the OPENROUTER_KEY environment variable.'.PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$platform = PlatformFactory::create($_ENV['OPENROUTER_KEY']); | ||
$llm = new GenericModel('google/gemini-2.0-flash-thinking-exp:free'); | ||
|
||
$chain = new Chain($platform, $llm); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are a helpful assistant.'), | ||
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'), | ||
); | ||
$response = $chain->call($messages); | ||
|
||
echo $response->getContent().PHP_EOL; |
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Bridge\OpenRouter; | ||
|
||
use PhpLlm\LlmChain\Model\Message\MessageBagInterface; | ||
use PhpLlm\LlmChain\Model\Model; | ||
use PhpLlm\LlmChain\Platform\ModelClient; | ||
use PhpLlm\LlmChain\Platform\ResponseConverter; | ||
use Symfony\Component\HttpClient\EventSourceHttpClient; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
use Webmozart\Assert\Assert; | ||
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; | ||
use PhpLlm\LlmChain\Model\Response\TextResponse; | ||
use PhpLlm\LlmChain\Exception\RuntimeException; | ||
|
||
final readonly class Client implements ModelClient, ResponseConverter | ||
{ | ||
private EventSourceHttpClient $httpClient; | ||
|
||
public function __construct( | ||
HttpClientInterface $httpClient, | ||
#[\SensitiveParameter] private string $apiKey, | ||
) { | ||
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient); | ||
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.'); | ||
Assert::startsWith($apiKey, 'sk-', 'The API key must start with "sk-".'); | ||
} | ||
|
||
public function supports(Model $model, array|string|object $input): bool | ||
{ | ||
return $input instanceof MessageBagInterface; | ||
} | ||
|
||
public function request(Model $model, object|array|string $input, array $options = []): ResponseInterface | ||
{ | ||
return $this->httpClient->request('POST', 'https://openrouter.ai/api/v1/chat/completions', [ | ||
'auth_bearer' => $this->apiKey, | ||
'json' => array_merge($options, [ | ||
'model' => $model->getVersion(), | ||
'messages' => $input, | ||
]), | ||
]); | ||
} | ||
|
||
|
||
public function convert(ResponseInterface $response, array $options = []): LlmResponse | ||
{ | ||
$data = $response->toArray(); | ||
|
||
if (!isset($data['choices'][0]['message'])) { | ||
throw new RuntimeException('Response does not contain message'); | ||
} | ||
|
||
if (!isset($data['choices'][0]['message']['content'])) { | ||
throw new RuntimeException('Message does not contain content'); | ||
} | ||
|
||
return new TextResponse($data['choices'][0]['message']['content']); | ||
} | ||
|
||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Bridge\OpenRouter; | ||
|
||
use PhpLlm\LlmChain\Bridge\Meta\Llama; | ||
use PhpLlm\LlmChain\Model\LanguageModel; | ||
|
||
final readonly class GenericModel implements LanguageModel | ||
{ | ||
|
||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function __construct( | ||
private string $version = Llama::LLAMA_3_2_90B_VISION_INSTRUCT, | ||
private array $options = [], | ||
) { | ||
} | ||
|
||
public function getVersion(): string | ||
{ | ||
return $this->version; | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function supportsAudioInput(): bool | ||
{ | ||
return false; // it does, but implementation here is still open. | ||
} | ||
|
||
public function supportsImageInput(): bool | ||
{ | ||
return false; // it does, but implementation here is still open. | ||
} | ||
|
||
public function supportsStreaming(): bool | ||
{ | ||
return false; // it does, but implementation here is still open. | ||
} | ||
|
||
public function supportsToolCalling(): bool | ||
{ | ||
return false; // it does, but implementation here is still open. | ||
} | ||
|
||
public function supportsStructuredOutput(): bool | ||
{ | ||
return false; // it does, but implementation here is still open. | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Bridge\OpenRouter; | ||
|
||
use PhpLlm\LlmChain\Platform; | ||
use Symfony\Component\HttpClient\EventSourceHttpClient; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
final class PlatformFactory | ||
{ | ||
public static function create( | ||
#[\SensitiveParameter] | ||
string $apiKey, | ||
?HttpClientInterface $httpClient = null, | ||
): Platform { | ||
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient); | ||
$handler = new Client($httpClient, $apiKey); | ||
|
||
return new Platform([$handler], [$handler]); | ||
} | ||
} |