Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/platform/src/Bridge/Generic/Embeddings/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
namespace Symfony\AI\Platform\Bridge\Generic\Embeddings;

use Symfony\AI\Platform\Bridge\Generic\EmbeddingsModel;
use Symfony\AI\Platform\Exception\AuthenticationException;
use Symfony\AI\Platform\Exception\BadRequestException;
use Symfony\AI\Platform\Exception\RateLimitExceededException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Result\RawResultInterface;
Expand All @@ -23,6 +26,7 @@
* This default result converter assumes the same response format as OpenAI's embeddings endpoint.
*
* @author Christopher Hertel <mail@christopher-hertel.de>
* @author Tim Lochmüller <tim@fruit-lab.de>
*/
class ResultConverter implements ResultConverterInterface
{
Expand All @@ -33,16 +37,32 @@ public function supports(Model $model): bool

public function convert(RawResultInterface $result, array $options = []): VectorResult
{
$response = $result->getObject();
$data = $result->getData();

if (!isset($data['data'])) {
if (401 === $response->getStatusCode()) {
$errorMessage = json_decode($response->getContent(false), true)['error']['message'];
throw new AuthenticationException($errorMessage);
}

if (400 === $response->getStatusCode() || 404 === $response->getStatusCode()) {
$errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request';
throw new BadRequestException($errorMessage);
}

if (429 === $response->getStatusCode()) {
$errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request';
throw new RateLimitExceededException($errorMessage);
}

if (!isset($data['data'][0]['embedding'])) {
throw new RuntimeException('Response does not contain data.');
}

return new VectorResult(
...array_map(
static fn (array $item): Vector => new Vector($item['embedding']),
$data['data']
$data['data'],
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\AI\Platform\Bridge\OpenRouter;

use Symfony\AI\Platform\Bridge\Generic\CompletionsModel;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\ModelCatalog\AbstractModelCatalog;

/**
Expand All @@ -35,11 +35,11 @@ public function __construct()
{
$this->models = [
'openrouter/auto' => [
'class' => Model::class,
'class' => CompletionsModel::class,
'capabilities' => Capability::cases(),
],
'@preset' => [
'class' => Model::class,
'class' => CompletionsModel::class,
'capabilities' => Capability::cases(),
],
];
Expand Down
54 changes: 0 additions & 54 deletions src/platform/src/Bridge/OpenRouter/Completions/ModelClient.php

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions src/platform/src/Bridge/OpenRouter/Embeddings.php

This file was deleted.

53 changes: 0 additions & 53 deletions src/platform/src/Bridge/OpenRouter/Embeddings/ModelClient.php

This file was deleted.

This file was deleted.

8 changes: 5 additions & 3 deletions src/platform/src/Bridge/OpenRouter/ModelApiCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\AI\Platform\Bridge\OpenRouter;

use Symfony\AI\Platform\Bridge\Generic\CompletionsModel;
use Symfony\AI\Platform\Bridge\Generic\EmbeddingsModel;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\Model;
Expand Down Expand Up @@ -100,21 +102,21 @@ protected function fetchRemoteModels(): iterable
}

yield $model['id'] => [
'class' => Model::class,
'class' => CompletionsModel::class,
'capabilities' => $capabilities,
];
}
}

/**
* @return iterable<string, array{class: class-string<Embeddings>, capabilities: list<Capability::*>}>
* @return iterable<string, array{class: class-string<EmbeddingsModel>, capabilities: list<Capability::*>}>
*/
protected function fetchRemoteEmbeddings(): iterable
{
$responseEmbeddings = $this->httpClient->request('GET', 'https://openrouter.ai/api/v1/embeddings/models');
foreach ($responseEmbeddings->toArray()['data'] as $embedding) {
yield $embedding['id'] => [
'class' => Embeddings::class,
'class' => EmbeddingsModel::class,
'capabilities' => [Capability::INPUT_TEXT, Capability::EMBEDDINGS],
];
}
Expand Down
Loading