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
2 changes: 1 addition & 1 deletion src/platform/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"phpstan/phpdoc-parser": "^2.1",
"psr/log": "^3.0",
"symfony/clock": "^7.3|^8.0",
"symfony/event-dispatcher": "^7.3|^8.0",
"symfony/http-client": "^7.3|^8.0",
"symfony/property-access": "^7.3|^8.0",
"symfony/property-info": "^7.3|^8.0",
Expand All @@ -66,7 +67,6 @@
"symfony/ai-agent": "@dev",
"symfony/console": "^7.3|^8.0",
"symfony/dotenv": "^7.3|^8.0",
"symfony/event-dispatcher": "^7.3|^8.0",
"symfony/finder": "^7.3|^8.0",
"symfony/process": "^7.3|^8.0",
"symfony/var-dumper": "^7.3|^8.0"
Expand Down
76 changes: 76 additions & 0 deletions src/platform/src/Event/InvocationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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\Event;

use Symfony\AI\Platform\Model;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Event dispatched before platform invocation to allow modification of input data.
*
* @author Ramy Hakam <[email protected]>
*/
final class InvocationEvent extends Event
{
/**
* @param array<string, mixed>|string|object $input
* @param array<string, mixed> $options
*/
public function __construct(
private Model $model,
private array|string|object $input,
private array $options = [],
) {
}

public function getModel(): Model
{
return $this->model;
}

public function setModel(Model $model): void
{
$this->model = $model;
}

/**
* @return array<string, mixed>|string|object
*/
public function getInput(): array|string|object
{
return $this->input;
}

/**
* @param array<string, mixed>|string|object $input
*/
public function setInput(array|string|object $input): void
{
$this->input = $input;
}

/**
* @return array<string, mixed>
*/
public function getOptions(): array
{
return $this->options;
}

/**
* @param array<string, mixed> $options
*/
public function setOptions(array $options): void
{
$this->options = $options;
}
}
41 changes: 41 additions & 0 deletions src/platform/src/EventListener/StringToMessageBagListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\EventListener;

use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Event\InvocationEvent;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

/**
* Converts string inputs to MessageBag for models that support INPUT_MESSAGES capability.
*
* @author Ramy Hakam <[email protected]>
*/
final class StringToMessageBagListener
{
public function __invoke(InvocationEvent $event): void
{
// Only process string inputs
if (!\is_string($event->getInput())) {
return;
}

// Only process models that support INPUT_MESSAGES capability
if (!$event->getModel()->supports(Capability::INPUT_MESSAGES)) {
return;
}

// Convert string to MessageBag with a user message
$event->setInput(new MessageBag(Message::ofUser($event->getInput())));
}
}
13 changes: 10 additions & 3 deletions src/platform/src/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\AI\Platform;

use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\AI\Platform\Event\InvocationEvent;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
use Symfony\AI\Platform\Result\DeferredResult;
Expand Down Expand Up @@ -38,8 +40,9 @@ final class Platform implements PlatformInterface
public function __construct(
iterable $modelClients,
iterable $resultConverters,
private ModelCatalogInterface $modelCatalog,
private readonly ModelCatalogInterface $modelCatalog,
private ?Contract $contract = null,
private readonly ?EventDispatcherInterface $eventDispatcher = null,
) {
$this->contract = $contract ?? Contract::create();
$this->modelClients = $modelClients instanceof \Traversable ? iterator_to_array($modelClients) : $modelClients;
Expand All @@ -49,8 +52,12 @@ public function __construct(
public function invoke(string $model, array|string|object $input, array $options = []): DeferredResult
{
$model = $this->modelCatalog->getModel($model);
$payload = $this->contract->createRequestPayload($model, $input);
$options = array_merge($model->getOptions(), $options);

$event = new InvocationEvent($model, $input, $options);
$this->eventDispatcher?->dispatch($event);

$payload = $this->contract->createRequestPayload($event->getModel(), $event->getInput());
$options = array_merge($model->getOptions(), $event->getOptions());

if (isset($options['tools'])) {
$options['tools'] = $this->contract->createToolOption($options['tools'], $model);
Expand Down
70 changes: 70 additions & 0 deletions src/platform/tests/Event/InvocationEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\Tests\Event;

use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Event\InvocationEvent;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Model;

final class InvocationEventTest extends TestCase
{
public function testGettersReturnCorrectValues()
{
$model = new class('test-model', [Capability::INPUT_MESSAGES, Capability::OUTPUT_TEXT]) extends Model {
};

$input = 'Hello, world!';
$options = ['temperature' => 0.7];

$event = new InvocationEvent($model, $input, $options);

$this->assertSame($model, $event->getModel());
$this->assertSame($input, $event->getInput());
$this->assertSame($options, $event->getOptions());
}

public function testSetInputChangesInput()
{
$model = new class('test-model', [Capability::INPUT_MESSAGES, Capability::OUTPUT_TEXT]) extends Model {
};

$originalInput = 'Hello, world!';
$newInput = new MessageBag(Message::ofUser('Hello, world!'));

$event = new InvocationEvent($model, $originalInput);
$event->setInput($newInput);

$this->assertSame($newInput, $event->getInput());
}

public function testWorksWithDifferentInputTypes()
{
$model = new class('test-model', [Capability::INPUT_MESSAGES, Capability::OUTPUT_TEXT]) extends Model {
};

// Test with string
$stringEvent = new InvocationEvent($model, 'string input');
$this->assertIsString($stringEvent->getInput());

// Test with array
$arrayEvent = new InvocationEvent($model, ['key' => 'value']);
$this->assertIsArray($arrayEvent->getInput());

// Test with object
$objectInput = new MessageBag();
$objectEvent = new InvocationEvent($model, $objectInput);
$this->assertSame($objectInput, $objectEvent->getInput());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Event\InvocationEvent;
use Symfony\AI\Platform\EventListener\StringToMessageBagListener;
use Symfony\AI\Platform\Message\Content\Text;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Message\UserMessage;
use Symfony\AI\Platform\Model;

final class StringToMessageBagListenerTest extends TestCase
{
public function testConvertsStringInputToMessageBagForMessagesCapableModel()
{
$model = new class('test-model', [Capability::INPUT_MESSAGES, Capability::OUTPUT_TEXT]) extends Model {
};

$event = new InvocationEvent($model, 'Hello, world!');
$listener = new StringToMessageBagListener();

$listener($event);

$this->assertInstanceOf(MessageBag::class, $event->getInput());
$this->assertCount(1, $event->getInput()->getMessages());
$message = $event->getInput()->getMessages()[0];
$this->assertInstanceOf(UserMessage::class, $message);
$this->assertCount(1, $message->getContent());
$content = $message->getContent()[0];
$this->assertInstanceOf(Text::class, $content);
$this->assertSame('Hello, world!', $content->getText());
}

public function testDoesNotConvertStringInputForNonMessagesCapableModel()
{
$model = new class('test-model', [Capability::INPUT_TEXT, Capability::OUTPUT_TEXT]) extends Model {
};

$originalInput = 'Hello, world!';
$event = new InvocationEvent($model, $originalInput);
$listener = new StringToMessageBagListener();

$listener($event);

$this->assertSame($originalInput, $event->getInput());
}

public function testDoesNotConvertNonStringInput()
{
$model = new class('test-model', [Capability::INPUT_MESSAGES, Capability::OUTPUT_TEXT]) extends Model {
};

$originalInput = new MessageBag(Message::ofUser('Hello'));
$event = new InvocationEvent($model, $originalInput);
$listener = new StringToMessageBagListener();

$listener($event);

$this->assertSame($originalInput, $event->getInput());
}

public function testDoesNotConvertArrayInput()
{
$model = new class('test-model', [Capability::INPUT_MESSAGES, Capability::OUTPUT_TEXT]) extends Model {
};

$originalInput = ['key' => 'value'];
$event = new InvocationEvent($model, $originalInput);
$listener = new StringToMessageBagListener();

$listener($event);

$this->assertSame($originalInput, $event->getInput());
}
}