Skip to content

Commit cac775d

Browse files
committed
Create tool (call) normalizers for Responses API
Since we're migration from chat completions to Responses, which has a differnt data structure for function calls
1 parent a3c6e68 commit cac775d

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Bridge\OpenAi\Contract\Gpt;
13+
14+
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
15+
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
16+
use Symfony\AI\Platform\Model;
17+
use Symfony\AI\Platform\Result\ToolCall;
18+
19+
final class ToolCallNormalizer extends ModelContractNormalizer
20+
{
21+
/**
22+
* @param ToolCall $data
23+
*
24+
* @return array{
25+
* arguments: string,
26+
* call_id: string,
27+
* name: string,
28+
* type: 'function_call'
29+
* }
30+
*/
31+
public function normalize(mixed $data, ?string $format = null, array $context = []): array
32+
{
33+
return [
34+
'arguments' => json_encode($data->getArguments()),
35+
'call_id' => $data->getId(),
36+
'name' => $data->getName(),
37+
'type' => 'function_call',
38+
];
39+
}
40+
41+
protected function supportedDataClass(): string
42+
{
43+
return ToolCall::class;
44+
}
45+
46+
protected function supportsModel(Model $model): bool
47+
{
48+
return $model instanceof Gpt;
49+
}
50+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Bridge\OpenAi\Contract\Gpt;
13+
14+
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
15+
use Symfony\AI\Platform\Contract\JsonSchema\Factory;
16+
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
17+
use Symfony\AI\Platform\Model;
18+
use Symfony\AI\Platform\Tool\Tool;
19+
20+
/**
21+
* @phpstan-import-type JsonSchema from Factory
22+
*
23+
* @author Christopher Hertel <[email protected]>
24+
*/
25+
class ToolNormalizer extends ModelContractNormalizer
26+
{
27+
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
28+
{
29+
return $data instanceof Tool;
30+
}
31+
32+
/**
33+
* @param Tool $data
34+
*
35+
* @return array{
36+
* type: 'function',
37+
* name: string,
38+
* description: string,
39+
* parameters?: JsonSchema
40+
* }
41+
*/
42+
public function normalize(mixed $data, ?string $format = null, array $context = []): array
43+
{
44+
$function = [
45+
'type' => 'function',
46+
'name' => $data->getName(),
47+
'description' => $data->getDescription(),
48+
];
49+
50+
if (null !== $data->getParameters()) {
51+
$function['parameters'] = $data->getParameters();
52+
}
53+
54+
return $function;
55+
}
56+
57+
protected function supportedDataClass(): string
58+
{
59+
return Tool::class;
60+
}
61+
62+
protected function supportsModel(Model $model): bool
63+
{
64+
return $model instanceof Gpt;
65+
}
66+
}

0 commit comments

Comments
 (0)