Skip to content

[Platform] Use JSON Path to convert responses #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: simplify-choice-handling
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions examples/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"symfony/event-dispatcher": "^6.4|^7.0",
"symfony/filesystem": "^6.4|^7.0",
"symfony/finder": "^6.4|^7.0",
"symfony/json-path": "7.3.*",
"symfony/process": "^6.4|^7.0",
"symfony/var-dumper": "^6.4|^7.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/platform/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"psr/log": "^3.0",
"symfony/clock": "^6.4 || ^7.1",
"symfony/http-client": "^6.4 || ^7.1",
"symfony/json-path": "7.3.*",
"symfony/property-access": "^6.4 || ^7.1",
"symfony/property-info": "^6.4 || ^7.1",
"symfony/serializer": "^6.4 || ^7.1",
Expand Down
3 changes: 1 addition & 2 deletions src/platform/src/Bridge/Albert/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\AI\Platform\Bridge\Albert;

use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAI\GPT;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -40,7 +39,7 @@ public static function create(
new GPTModelClient($httpClient, $apiKey, $baseUrl),
new EmbeddingsModelClient($httpClient, $apiKey, $baseUrl),
],
[new GPT\ResultConverter(), new Embeddings\ResultConverter()],
[new GPT\ResultConverter(), Contract\ResultConverter::create()],
Contract::create(),
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/platform/src/Bridge/Azure/OpenAI/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\AI\Platform\Bridge\Azure\OpenAI;

use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAI\GPT;
use Symfony\AI\Platform\Bridge\OpenAI\Whisper;
use Symfony\AI\Platform\Bridge\OpenAI\Whisper\AudioNormalizer;
Expand Down Expand Up @@ -41,7 +40,7 @@ public static function create(

return new Platform(
[$GPTModelClient, $embeddingsModelClient, $whisperModelClient],
[new GPT\ResultConverter(), new Embeddings\ResultConverter(), new Whisper\ResultConverter()],
[new GPT\ResultConverter(), new Whisper\ResultConverter(), Contract\ResultConverter::create()],
$contract ?? Contract::create(new AudioNormalizer()),
);
}
Expand Down
47 changes: 0 additions & 47 deletions src/platform/src/Bridge/Gemini/Embeddings/ResultConverter.php

This file was deleted.

8 changes: 5 additions & 3 deletions src/platform/src/Bridge/Gemini/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

use Symfony\AI\Platform\Bridge\Gemini\Contract\GeminiContract;
use Symfony\AI\Platform\Bridge\Gemini\Embeddings\ModelClient as EmbeddingsModelClient;
use Symfony\AI\Platform\Bridge\Gemini\Embeddings\ResultConverter as EmbeddingsResultConverter;
use Symfony\AI\Platform\Bridge\Gemini\Gemini\ModelClient as GeminiModelClient;
use Symfony\AI\Platform\Bridge\Gemini\Gemini\ResultConverter as GeminiResultConverter;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Contract\JsonPathConverter\VectorResultExtractor;
use Symfony\AI\Platform\Contract\ResultConverter;
use Symfony\AI\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -36,7 +36,9 @@ public static function create(

return new Platform(
[new EmbeddingsModelClient($httpClient, $apiKey), new GeminiModelClient($httpClient, $apiKey)],
[new EmbeddingsResultConverter(), new GeminiResultConverter()],
[new Gemini\ResultConverter(), ResultConverter::create([
new VectorResultExtractor('$.embeddings[*].values'),
])],
$contract ?? GeminiContract::create(),
);
}
Expand Down
54 changes: 0 additions & 54 deletions src/platform/src/Bridge/Mistral/Embeddings/ResultConverter.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/platform/src/Bridge/Mistral/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function create(

return new Platform(
[new Embeddings\ModelClient($httpClient, $apiKey), new Llm\ModelClient($httpClient, $apiKey)],
[new Embeddings\ResultConverter(), new Llm\ResultConverter()],
[new Llm\ResultConverter(), Contract\ResultConverter::create()],
$contract ?? Contract::create(new ToolNormalizer()),
);
}
Expand Down
47 changes: 0 additions & 47 deletions src/platform/src/Bridge/OpenAI/Embeddings/ResultConverter.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/platform/src/Bridge/OpenAI/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public static function create(
],
[
new GPT\ResultConverter(),
new Embeddings\ResultConverter(),
new DallE\ResultConverter(),
new WhisperResponseConverter(),
Contract\ResultConverter::create(),
],
$contract ?? Contract::create(new AudioNormalizer()),
);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/src/Bridge/Voyage/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public static function create(
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);

return new Platform([new ModelClient($httpClient, $apiKey)], [new ResultConverter()], $contract);
return new Platform([new ModelClient($httpClient, $apiKey)], [Contract\ResultConverter::create()], $contract);
}
}
44 changes: 0 additions & 44 deletions src/platform/src/Bridge/Voyage/ResultConverter.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Contract\JsonPathConverter;

use Symfony\AI\Platform\Result\ResultInterface;
use Symfony\Component\JsonPath\JsonCrawler;

interface ResultExtractorInterface
{
public function supports(JsonCrawler $crawler): bool;

/**
* @return ResultInterface[]
*/
public function extract(JsonCrawler $crawler): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Contract\JsonPathConverter;

use Symfony\AI\Platform\Result\TextResult;
use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

readonly class TextResultExtractor implements ResultExtractorInterface
{
public function __construct(
private string|JsonPath $jsonPath = '$.choices[?length(@.message.content) >= 0].message.content',
) {
}

public function supports(JsonCrawler $crawler): bool
{
return [] !== array_filter($crawler->find($this->jsonPath));
}

public function extract(JsonCrawler $crawler): array
{
$data = $crawler->find($this->jsonPath);

return array_map(static fn (string $text): TextResult => new TextResult($text), $data);
}
}
Loading