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
21 changes: 19 additions & 2 deletions Classes/Provider/SymfonyAi/SymfonyAiPlatformAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ class SymfonyAiPlatformAdapter implements
/** @var array<string, PlatformInterface> Platforms cached by configuration key */
private array $platforms = [];

private readonly string $maxTokensKey;

/**
* @param string $factoryClass Fully-qualified class name of the bridge's PlatformFactory
* @param string $factoryParam Name of the factory parameter to pass the config value to ('apiKey' or 'endpoint')
*/
public function __construct(
private readonly string $factoryClass,
private readonly string $factoryParam = 'apiKey',
) {}
) {
$this->maxTokensKey = self::resolveMaxTokensKey($factoryClass);
}

public function processVisionRequest(VisionRequest $request): TextResponse
{
Expand Down Expand Up @@ -433,13 +437,26 @@ private function buildMessageBag(array $aiMessages, string $systemPrompt): Messa
*/
private function buildOptions(string $model, int $maxTokens, float $temperature, array $extra = []): array
{
$options = ['max_output_tokens' => $maxTokens] + $extra;
$options = [$this->maxTokensKey => $maxTokens] + $extra;
if (!$this->isReasoningModel($model)) {
$options['temperature'] = $temperature;
}
return $options;
}

/**
* Resolve the max-tokens option key expected by a Symfony AI bridge based on the used bridge
*/
public static function resolveMaxTokensKey(string $factoryClass): string
{
if (str_contains($factoryClass, '\\Bridge\\OpenAi\\')
|| str_contains($factoryClass, '\\Bridge\\OpenResponses\\')
) {
return 'max_output_tokens';
}
return 'max_tokens';
}

/**
* Check if a model is a reasoning model that doesn't support temperature.
*
Expand Down
56 changes: 56 additions & 0 deletions Tests/Unit/Provider/SymfonyAi/SymfonyAiPlatformAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* This file is part of TYPO3 CMS-based extension "aim" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

namespace B13\Aim\Tests\Unit\Provider\SymfonyAi;

use B13\Aim\Provider\SymfonyAi\SymfonyAiPlatformAdapter;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

final class SymfonyAiPlatformAdapterTest extends TestCase
{
public static function maxTokensKeyProvider(): \Generator
{
yield 'OpenAI Responses API' => [
'Symfony\\AI\\Platform\\Bridge\\OpenAi\\PlatformFactory',
'max_output_tokens',
];
yield 'OpenResponses bridge' => [
'Symfony\\AI\\Platform\\Bridge\\OpenResponses\\PlatformFactory',
'max_output_tokens',
];
yield 'Anthropic Messages API' => [
'Symfony\\AI\\Platform\\Bridge\\Anthropic\\PlatformFactory',
'max_tokens',
];
yield 'Mistral' => [
'Symfony\\AI\\Platform\\Bridge\\Mistral\\PlatformFactory',
'max_tokens',
];
yield 'Ollama' => [
'Symfony\\AI\\Platform\\Bridge\\Ollama\\PlatformFactory',
'max_tokens',
];
yield 'unknown bridge falls back to max_tokens' => [
'Acme\\AiBridge\\PlatformFactory',
'max_tokens',
];
}

#[Test]
#[DataProvider('maxTokensKeyProvider')]
public function resolveMaxTokensKeyMapsBridgeToCorrectOptionName(string $factoryClass, string $expectedKey): void
{
self::assertSame($expectedKey, SymfonyAiPlatformAdapter::resolveMaxTokensKey($factoryClass));
}
}