Skip to content

[Platform] [Anthropic] Allow beta feature flags to be passed into platform invocations #274

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions src/platform/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ CHANGELOG
* Add response promises for async operations
* Add InMemoryPlatform and InMemoryRawResult for testing Platform without external Providers calls
* Add tool calling support for Ollama platform
* Allow beta feature flags to be passed into Anthropic model options


2 changes: 1 addition & 1 deletion src/platform/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ start for vendor-specific models and their capabilities, see ``Symfony\AI\Platfo
supports a specific feature, like ``Capability::INPUT_AUDIO`` or ``Capability::OUTPUT_IMAGE``.

**Options** are additional parameters that can be passed to the model, like ``temperature`` or ``max_tokens``, and are
usually defined by the specific models and their documentation.
usually defined by the specific models and their documentation. Flags for beta features are also passed in here, handled, and then removed before being sent to the provider (beta feature flags are currently only supported for the `Anthropic` platform). A more robust approach for handling provider feature flags may be implemented in the future.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this again, I don't think we should give those model specific examples in this rather generic section.


**Supported Models & Platforms**

Expand Down
15 changes: 11 additions & 4 deletions src/platform/src/Bridge/Anthropic/ModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,22 @@ public function supports(Model $model): bool

public function request(Model $model, array|string $payload, array $options = []): RawHttpResult
{
$headers = [
'x-api-key' => $this->apiKey,
'anthropic-version' => $this->version,
];

if (isset($options['tools'])) {
$options['tool_choice'] = ['type' => 'auto'];
}

if (isset($options['beta_features']) && !empty($options['beta_features'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about also checking for array here?

$headers['anthropic-beta'] = implode(',', $options['beta_features']);
unset($options['beta_features']);
}

return new RawHttpResult($this->httpClient->request('POST', 'https://api.anthropic.com/v1/messages', [
'headers' => [
'x-api-key' => $this->apiKey,
'anthropic-version' => $this->version,
],
'headers' => $headers,
'json' => array_merge($options, $payload),
]));
}
Expand Down
113 changes: 113 additions & 0 deletions src/platform/tests/Bridge/Anthropic/ModelClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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\Bridge\Anthropic\Tests;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\Anthropic\Claude;
use Symfony\AI\Platform\Bridge\Anthropic\ModelClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

#[CoversClass(ModelClient::class)]
class ModelClientTest extends TestCase
{
private MockHttpClient $httpClient;
private ModelClient $modelClient;
private Claude $model;

protected function setUp(): void
{
$this->model = new Claude();
}

private function parseHeaders(array $headers): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move private methods below public methods

i wonder why php-cs-fixer doesn't complain 🤔

{
$parsed = [];
foreach ($headers as $header) {
if (strpos($header, ':') !== false) {
[$key, $value] = explode(':', $header, 2);
$parsed[trim($key)] = trim($value);
}
}
return $parsed;
}

public function testAnthropicBetaHeaderIsSetWithSingleBetaFeature(): void
{
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
$this->assertEquals('POST', $method);
$this->assertEquals('https://api.anthropic.com/v1/messages', $url);

$headers = $this->parseHeaders($options['headers']);

$this->assertArrayHasKey('anthropic-beta', $headers);
$this->assertEquals('feature-1', $headers['anthropic-beta']);

return new MockResponse('{"success": true}');
});

$this->modelClient = new ModelClient($this->httpClient, 'test-api-key');

$options = ['beta_features' => ['feature-1']];
$this->modelClient->request($this->model, ['message' => 'test'], $options);
}

public function testAnthropicBetaHeaderIsSetWithMultipleBetaFeatures(): void
{
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
$headers = $this->parseHeaders($options['headers']);

$this->assertArrayHasKey('anthropic-beta', $headers);
$this->assertEquals('feature-1,feature-2,feature-3', $headers['anthropic-beta']);

return new MockResponse('{"success": true}');
});

$this->modelClient = new ModelClient($this->httpClient, 'test-api-key', '2023-06-01');

$options = ['beta_features' => ['feature-1', 'feature-2', 'feature-3']];
$this->modelClient->request($this->model, ['message' => 'test'], $options);
}

public function testAnthropicBetaHeaderIsNotSetWhenBetaFeaturesIsEmpty(): void
{
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
$headers = $this->parseHeaders($options['headers']);

$this->assertArrayNotHasKey('anthropic-beta', $headers);

return new MockResponse('{"success": true}');
});

$this->modelClient = new ModelClient($this->httpClient, 'test-api-key', '2023-06-01');

$options = ['beta_features' => []];
$this->modelClient->request($this->model, ['message' => 'test'], $options);
}

public function testAnthropicBetaHeaderIsNotSetWhenBetaFeaturesIsNotProvided(): void
{
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
$headers = $this->parseHeaders($options['headers']);

$this->assertArrayNotHasKey('anthropic-beta', $headers);

return new MockResponse('{"success": true}');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use JsonMockResponse

});

$this->modelClient = new ModelClient($this->httpClient, 'test-api-key', '2023-06-01');

$options = ['some_other_option' => 'value'];
$this->modelClient->request($this->model, ['message' => 'test'], $options);
}
}