-
-
Notifications
You must be signed in to change notification settings - Fork 50
[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
base: main
Are you sure you want to change the base?
Changes from all commits
82b33bf
1b96815
e534ebe
3ecf3e1
85ce067
3312380
dabd2b9
7f4a0e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
])); | ||
} | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move 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}'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
There was a problem hiding this comment.
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.