Skip to content

Commit fa4ea1c

Browse files
Build/Test Tools: Fix the request limit, the model check and the answer of the live AI provider tests.
The number of requests a model may take now follows the number of abilities, so a model that calls one ability per response can still answer when it is given all six. The model of each result is compared with the requested one, which also catches a listed model that cannot be used for the prompt. Every text part of the answer is read, not only the first one. See #66707.
1 parent 76cb13f commit fa4ea1c

2 files changed

Lines changed: 54 additions & 34 deletions

File tree

‎docker-compose.yml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ services:
5050
GITHUB_EVENT_NAME: ${GITHUB_EVENT_NAME-false}
5151
HOST_PATH: ${PWD-}/${LOCAL_DIR-src}
5252
# Passed through for the live AI provider tests, and only when they are set.
53-
# Only the tests read these names, so the keys are not used by the local site.
5453
WP_TESTS_AI_ANTHROPIC_API_KEY:
5554
WP_TESTS_AI_GOOGLE_API_KEY:
5655
WP_TESTS_AI_OPENAI_API_KEY:

‎tests/phpunit/tests/ai-client/wpAiClientLiveProviders.php‎

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* `WP_TESTS_AI_ANTHROPIC_API_KEY`, `WP_TESTS_AI_GOOGLE_API_KEY` or `WP_TESTS_AI_OPENAI_API_KEY`
77
* environment variable:
88
*
9-
* WP_TESTS_AI_ANTHROPIC_API_KEY=… vendor/bin/phpunit --group external-http --filter Tests_AI_Client_LiveProviders
9+
* WP_TESTS_AI_ANTHROPIC_API_KEY=your-key vendor/bin/phpunit --group external-http --filter Tests_AI_Client_LiveProviders
1010
*
1111
* The provider tools and the Connectors read other names, such as `ANTHROPIC_API_KEY`, so a key
1212
* set for them does not start these tests, and a key set for these tests is not used by a site.
1313
*
14-
* A provider uses its default model, unless a `WP_TESTS_AI_{PROVIDER}_MODEL` environment
15-
* variable, such as `WP_TESTS_AI_GOOGLE_MODEL`, names another one.
14+
* A provider uses its default model, unless its `WP_TESTS_AI_*_MODEL` environment variable,
15+
* such as `WP_TESTS_AI_GOOGLE_MODEL`, names another one.
1616
*
1717
* @group ai-client
1818
* @group abilities-api
@@ -48,19 +48,32 @@ class Tests_AI_Client_LiveProviders extends WP_UnitTestCase {
4848
* For each core ability, a question no other ability answers and the value the answer must contain.
4949
*/
5050
private const QUESTIONS = array(
51-
'core/get-site-info' => array( 'What is the character encoding of this site?', 'UTF-8' ),
52-
'core/get-user-info' => array( 'What is my display name?', 'Barnaby Quillfeather' ),
53-
'core/get-environment-info' => array( 'Which PHP version does this site run?', PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION ),
54-
'core/settings-get' => array( 'What is the timezone setting of this site?', 'Pacific/Chatham' ),
55-
'core/users-query' => array( 'What is the display name of the user with the username "live_provider_editor"?', 'Marigold Featherstone' ),
56-
'core/content-query' => array( 'What is the title of the post with the slug "live-provider-post"?', 'Quokka Lighthouse Notes' ),
51+
'core/get-site-info' => array(
52+
'question' => 'What is the character encoding of this site?',
53+
'answer' => 'UTF-8',
54+
),
55+
'core/get-user-info' => array(
56+
'question' => 'What is my display name?',
57+
'answer' => 'Barnaby Quillfeather',
58+
),
59+
'core/get-environment-info' => array(
60+
'question' => 'Which PHP version does this site run?',
61+
'answer' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
62+
),
63+
'core/settings-get' => array(
64+
'question' => 'What is the timezone setting of this site?',
65+
'answer' => 'Pacific/Chatham',
66+
),
67+
'core/users-query' => array(
68+
'question' => 'What is the display name of the user with the username "live_provider_editor"?',
69+
'answer' => 'Marigold Featherstone',
70+
),
71+
'core/content-query' => array(
72+
'question' => 'What is the title of the post with the slug "live-provider-post"?',
73+
'answer' => 'Quokka Lighthouse Notes',
74+
),
5775
);
5876

59-
/**
60-
* How many requests a model may take to answer.
61-
*/
62-
private const MAX_ROUNDS = 6;
63-
6477
/**
6578
* ID of the administrator who asks the questions.
6679
*
@@ -162,11 +175,11 @@ public function set_up(): void {
162175
public function test_provider_answers_with_core_abilities( string $provider, array $abilities ): void {
163176
$questions = array_intersect_key( self::QUESTIONS, array_flip( $abilities ) );
164177

165-
list( $answer, $called, $model ) = $this->ask( $provider, $abilities, implode( ' ', array_column( $questions, 0 ) ) );
178+
list( $answer, $called, $model ) = $this->ask( $provider, $abilities, implode( ' ', array_column( $questions, 'question' ) ) );
166179

167180
// A model may quote a value as it is in the JSON it received, where slashes are escaped.
168181
$answer = str_replace( '\/', '/', $answer );
169-
$missing = array_filter( array_column( $questions, 1 ), static fn ( $value ) => false === stripos( $answer, $value ) );
182+
$missing = array_filter( array_column( $questions, 'answer' ), static fn ( $value ) => false === stripos( $answer, $value ) );
170183
$context = "Model: $model. Answer: $answer";
171184

172185
$this->assertSameSets( $abilities, $called, "Every ability given to the model should run without an error. $context" );
@@ -229,26 +242,22 @@ private function ask( string $provider, array $abilities, string $question ): ar
229242
* Any other exception, such as a response the provider changed, is an error of the test.
230243
*/
231244
try {
232-
$models = $provider_class::modelMetadataDirectory()->listModelMetadata();
233-
} catch ( NetworkException | ServerException $e ) {
234-
$this->markTestSkipped( 'The provider is unavailable: ' . $e->getMessage() );
235-
} catch ( ClientException $e ) {
236-
if ( 429 === $e->getCode() ) {
237-
$this->markTestSkipped( 'The provider is unavailable: ' . $e->getMessage() );
245+
$provider_class::modelMetadataDirectory()->listModelMetadata();
246+
} catch ( NetworkException | ServerException | ClientException $e ) {
247+
if ( $e instanceof ClientException && 429 !== $e->getCode() ) {
248+
$this->fail( "The model list request failed, check the $key_name: " . $e->getMessage() );
238249
}
239-
$this->fail( "The model list request failed, check the $key_name: " . $e->getMessage() );
240-
}
241-
242-
// A model preference that matches no model would fall back to another model without an error.
243-
if ( $model && ! in_array( $model, array_map( static fn ( $metadata ) => $metadata->getId(), $models ), true ) ) {
244-
$this->fail( "The provider has no \"$model\" model, check the $model_name." );
250+
$this->markTestSkipped( 'The provider is unavailable: ' . $e->getMessage() );
245251
}
246252

247253
$resolver = new WP_AI_Client_Ability_Function_Resolver( ...$abilities );
248254
$messages = array( new UserMessage( array( new MessagePart( $question ) ) ) );
249255
$called = array();
250256

251-
for ( $round = 0; $round < self::MAX_ROUNDS; $round++ ) {
257+
// A model may call one ability per response: a request for each, one to answer and a spare one.
258+
$max_rounds = count( $abilities ) + 2;
259+
260+
for ( $round = 0; $round < $max_rounds; $round++ ) {
252261
$builder = ( new WP_AI_Client_Prompt_Builder( $registry, $messages ) )
253262
->using_provider( $provider )
254263
->using_system_instruction( 'Answer every question with the abilities. Quote the values they return unchanged.' )
@@ -262,20 +271,32 @@ private function ask( string $provider, array $abilities, string $question ): ar
262271

263272
if ( is_wp_error( $result ) ) {
264273
$status = $result->get_error_data()['status'] ?? 0;
265-
$error = "($status): " . $result->get_error_message();
266274

267275
// Only a rate limit, a server error of the provider and a network error skip the test.
268276
if ( 429 === $status || in_array( $result->get_error_code(), array( 'prompt_network_error', 'prompt_upstream_server_error' ), true ) ) {
269-
$this->markTestSkipped( "The provider is unavailable $error" );
277+
$this->markTestSkipped( 'The provider is unavailable: ' . $result->get_error_message() );
270278
}
271-
$this->fail( "The request failed $error" );
279+
$this->fail( 'The request failed: ' . $result->get_error_message() );
280+
}
281+
282+
// A model preference the provider cannot use for the prompt falls back to another model without an error.
283+
if ( $model && $result->getModelMetadata()->getId() !== $model ) {
284+
$this->fail( "The provider has no \"$model\" model for this prompt, check the $model_name." );
272285
}
273286

274287
// OpenAI returns each function call as its own candidate, so keep the message of each one.
275288
$has_calls = false;
289+
$answer = '';
276290
foreach ( $result->toMessages() as $message ) {
277291
$messages[] = $message;
278292

293+
// An answer may be split into several text parts.
294+
foreach ( $message->getParts() as $part ) {
295+
if ( $part->getChannel()->isContent() && null !== $part->getText() ) {
296+
$answer .= $part->getText();
297+
}
298+
}
299+
279300
if ( ! $resolver->has_ability_calls( $message ) ) {
280301
continue;
281302
}
@@ -293,7 +314,7 @@ private function ask( string $provider, array $abilities, string $question ): ar
293314
}
294315

295316
if ( ! $has_calls ) {
296-
return array( implode( "\n", $result->toTexts() ), array_unique( $called ), $result->getModelMetadata()->getId() );
317+
return array( $answer, array_unique( $called ), $result->getModelMetadata()->getId() );
297318
}
298319
}
299320

0 commit comments

Comments
 (0)