Skip to content
Open
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
28 changes: 13 additions & 15 deletions examples/openai/audio-input.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
* file that was distributed with this source code.
*/

use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
use Symfony\AI\Platform\Message\Content\Audio;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Exception\RuntimeException;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());

$messages = new MessageBag(
Message::ofUser(
'What is this recording about?',
Audio::fromFile(dirname(__DIR__, 2).'/fixtures/audio.mp3'),
),
);
$result = $platform->invoke('gpt-4o-audio-preview', $messages);

echo $result->asText().\PHP_EOL;
throw new RuntimeException('This example is temporarily unavailable due to migration to Responses API (which does not support audio yet).');
// $platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
//
// $messages = new MessageBag(
// Message::ofUser(
// 'What is this recording about?',
// Audio::fromFile(dirname(__DIR__, 2).'/fixtures/audio.mp3'),
// ),
// );
// $result = $platform->invoke('gpt-4o-audio-preview', $messages);
//
// echo $result->asText().\PHP_EOL;
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
at the beginning and end, not throughout the code.
PROMPT;

$agent = new Agent($platform, 'o1-preview');
$agent = new Agent($platform, 'o3');
$result = $agent->call(new MessageBag(Message::ofUser($prompt)));

echo $result->getContent().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/openai/chat-with-string-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
Message::forSystem('You are a pirate and you write funny.'),
Message::ofUser('What is the Symfony framework?'),
);
$result = $platform->invoke('gpt-4o-mini?max_tokens=7', $messages);
$result = $platform->invoke('gpt-4o-mini?max_output_tokens=16', $messages);

echo $result->asText().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/openai/chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Message::ofUser('What is the Symfony framework?'),
);
$result = $platform->invoke('gpt-4o-mini', $messages, [
'max_tokens' => 500, // specific options just for this call
'max_output_tokens' => 500, // specific options just for this call
]);

echo $result->asText().\PHP_EOL;
24 changes: 11 additions & 13 deletions examples/openai/structured-output-clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,19 @@
$agent = new Agent($platform, 'gpt-4o-mini', [$toolProcessor], [$toolProcessor]);

$messages = new MessageBag(Message::ofUser('What date and time is it?'));
$result = $agent->call($messages, ['response_format' => [
$result = $agent->call($messages, ['text' => ['format' => [
'type' => 'json_schema',
'json_schema' => [
'name' => 'clock',
'strict' => true,
'schema' => [
'type' => 'object',
'properties' => [
'date' => ['type' => 'string', 'description' => 'The current date in the format YYYY-MM-DD.'],
'time' => ['type' => 'string', 'description' => 'The current time in the format HH:MM:SS.'],
],
'required' => ['date', 'time'],
'additionalProperties' => false,
'name' => 'clock',
'strict' => true,
'schema' => [
'type' => 'object',
'properties' => [
'date' => ['type' => 'string', 'description' => 'The current date in the format YYYY-MM-DD.'],
'time' => ['type' => 'string', 'description' => 'The current time in the format HH:MM:SS.'],
],
'required' => ['date', 'time'],
'additionalProperties' => false,
],
]]);
]]]);

dump($result->getContent());
2 changes: 1 addition & 1 deletion examples/openai/token-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Message::ofUser('What is the Symfony framework?'),
);
$result = $agent->call($messages, [
'max_tokens' => 500, // specific options just for this call
'max_output_tokens' => 500, // specific options just for this call
]);

print_token_usage($result->getMetadata());
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

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

namespace Symfony\AI\Platform\Bridge\OpenAi\Contract\Gpt\Message;

use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
use Symfony\AI\Platform\Message\AssistantMessage;
use Symfony\AI\Platform\Model;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;

/**
* @author Guillermo Lengemann <guillermo.lengemann@gmail.com>
*/
final class AssistantMessageNormalizer extends ModelContractNormalizer implements NormalizerAwareInterface
{
use NormalizerAwareTrait;

/**
* @param AssistantMessage $data
*
* @return array{
* role: 'assistant',
* type: 'message',
* content: ?string
* }
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
if ($data->hasToolCalls()) {
return $this->normalizer->normalize($data->getToolCalls(), $format, $context);
}

return [
'role' => $data->getRole()->value,
'type' => 'message',
'content' => $data->getContent(),
];
}

protected function supportedDataClass(): string
{
return AssistantMessage::class;
}

protected function supportsModel(Model $model): bool
{
return $model instanceof Gpt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

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

namespace Symfony\AI\Platform\Bridge\OpenAi\Contract\Gpt\Message\Content;

use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
use Symfony\AI\Platform\Message\Content\Document;
use Symfony\AI\Platform\Message\Content\File;
use Symfony\AI\Platform\Model;

/**
* @author Guillermo Lengemann <guillermo.lengemann@gmail.com>
*/
class DocumentNormalizer extends ModelContractNormalizer
{
/**
* @param File $data
*
* @return array{type: 'input_file', filename: string, file_data: string}
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'type' => 'input_file',
'filename' => $data->getFilename(),
'file_data' => $data->asDataUrl(),
];
}

protected function supportedDataClass(): string
{
return Document::class;
}

protected function supportsModel(Model $model): bool
{
return $model instanceof Gpt && $model->supports(Capability::INPUT_PDF);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

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

namespace Symfony\AI\Platform\Bridge\OpenAi\Contract\Gpt\Message\Content;

use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
use Symfony\AI\Platform\Message\Content\Image;
use Symfony\AI\Platform\Model;

/**
* See: https://platform.openai.com/docs/guides/images-vision#giving-a-model-images-as-input.
*/
final class ImageNormalizer extends ModelContractNormalizer
{
/**
* @param Image $data
*
* @return array{
* type: 'input_image',
* image_url: string
* }
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'type' => 'input_image',
'image_url' => $data->asDataUrl(),
];
}

protected function supportedDataClass(): string
{
return Image::class;
}

protected function supportsModel(Model $model): bool
{
return $model instanceof Gpt && $model->supports(Capability::INPUT_IMAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

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

namespace Symfony\AI\Platform\Bridge\OpenAi\Contract\Gpt\Message\Content;

use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
use Symfony\AI\Platform\Message\Content\ImageUrl;
use Symfony\AI\Platform\Model;

/**
* See: https://platform.openai.com/docs/guides/images-vision#giving-a-model-images-as-input.
*/
final class ImageUrlNormalizer extends ModelContractNormalizer
{
/**
* @param ImageUrl $data
*
* @return array{
* type: 'input_image',
* image_url: string
* }
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'type' => 'input_image',
'image_url' => $data->getUrl(),
];
}

protected function supportedDataClass(): string
{
return ImageUrl::class;
}

protected function supportsModel(Model $model): bool
{
return $model instanceof Gpt && $model->supports(Capability::INPUT_IMAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

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

namespace Symfony\AI\Platform\Bridge\OpenAi\Contract\Gpt\Message\Content;

use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
use Symfony\AI\Platform\Message\Content\Text;
use Symfony\AI\Platform\Model;

/**
* See: https://platform.openai.com/docs/guides/images-vision#giving-a-model-images-as-input.
*/
final class TextNormalizer extends ModelContractNormalizer
{
/**
* @param Text $data
*
* @return array{
* type: 'input_text',
* text: string
* }
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'type' => 'input_text',
'text' => $data->getText(),
];
}

protected function supportedDataClass(): string
{
return Text::class;
}

protected function supportsModel(Model $model): bool
{
return $model instanceof Gpt;
}
}
Loading