Skip to content

Commit d39c135

Browse files
committed
Deprecate Generation_Config class in favor of new Text_Generation_Config class.
1 parent a3d7676 commit d39c135

14 files changed

+104
-52
lines changed

docs/Accessing-AI-Services-in-JavaScript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Note that not all configuration arguments are supported by every service API. Ho
317317
* `topK` _(integer)_: The maximum number of tokens to consider when sampling.
318318
* Supported by all except `openai`.
319319

320-
Please see the [`Felix_Arntz\AI_Services\Services\API\Types\Generation_Config` class](../includes/Services/API/Types/Generation_Config.php) for all available configuration arguments, and consult the API documentation of the respective provider to see which of them are supported.
320+
Please see the [`Felix_Arntz\AI_Services\Services\API\Types\Text_Generation_Config` class](../includes/Services/API/Types/Text_Generation_Config.php) for all available configuration arguments, and consult the API documentation of the respective provider to see which of them are supported.
321321

322322
### Function calling
323323

docs/Accessing-AI-Services-in-PHP.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,23 @@ It's worth noting that streaming is likely more useful in JavaScript than in PHP
245245

246246
### Customizing the default model configuration
247247

248-
When retrieving a model using the `get_model()` method, it is possible to provide a `generationConfig` argument to customize the model configuration. The `generationConfig` key needs to contain an instance of the [`Felix_Arntz\AI_Services\Services\API\Types\Generation_Config` class](../includes/Services/API/Types/Generation_Config.php), which allows to provide various model configuration arguments in a normalized way that works across the different AI services and their APIs.
248+
When retrieving a model using the `get_model()` method, it is possible to provide a `generationConfig` argument to customize the model configuration. The `generationConfig` key needs to contain an instance of the [`Felix_Arntz\AI_Services\Services\API\Types\Text_Generation_Config` class](../includes/Services/API/Types/Text_Generation_Config.php), which allows to provide various model configuration arguments in a normalized way that works across the different AI services and their APIs.
249249

250250
Additionally to `generationConfig`, you can pass a `systemInstruction` argument if you want to provide a custom instruction for how the model should behave. By setting a system instruction, you give the model additional context to understand its tasks, provide more customized responses, and adhere to specific guidelines over the full user interaction with the model.
251251

252252
Here is a code example using both `generationConfig` and `systemInstruction`:
253253

254254
```php
255255
use Felix_Arntz\AI_Services\Services\API\Enums\AI_Capability;
256-
use Felix_Arntz\AI_Services\Services\API\Types\Generation_Config;
256+
use Felix_Arntz\AI_Services\Services\API\Types\Text_Generation_Config;
257257

258258
try {
259259
$model = $service
260260
->get_model(
261261
array(
262262
'feature' => 'my-test-feature',
263263
'capabilities' => array( AI_Capability::TEXT_GENERATION ),
264-
'generationConfig' => Generation_Config::from_array(
264+
'generationConfig' => Text_Generation_Config::from_array(
265265
array(
266266
'maxOutputTokens' => 128,
267267
'temperature' => 0.2,
@@ -290,7 +290,7 @@ Note that not all configuration arguments are supported by every service API. Ho
290290
* `topK` _(integer)_: The maximum number of tokens to consider when sampling.
291291
* Supported by all except `openai`.
292292

293-
Please see the [`Felix_Arntz\AI_Services\Services\API\Types\Generation_Config` class](../includes/Services/API/Types/Generation_Config.php) for all available configuration arguments, and consult the API documentation of the respective provider to see which of them are supported.
293+
Please see the [`Felix_Arntz\AI_Services\Services\API\Types\Text_Generation_Config` class](../includes/Services/API/Types/Text_Generation_Config.php) for all available configuration arguments, and consult the API documentation of the respective provider to see which of them are supported.
294294

295295
### Function calling
296296

includes/Anthropic/Anthropic_AI_Model.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
use Felix_Arntz\AI_Services\Services\API\Types\Candidate;
1414
use Felix_Arntz\AI_Services\Services\API\Types\Candidates;
1515
use Felix_Arntz\AI_Services\Services\API\Types\Content;
16-
use Felix_Arntz\AI_Services\Services\API\Types\Generation_Config;
1716
use Felix_Arntz\AI_Services\Services\API\Types\Parts;
1817
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Function_Call_Part;
1918
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Function_Response_Part;
2019
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Inline_Data_Part;
2120
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Text_Part;
21+
use Felix_Arntz\AI_Services\Services\API\Types\Text_Generation_Config;
2222
use Felix_Arntz\AI_Services\Services\API\Types\Tool_Config;
2323
use Felix_Arntz\AI_Services\Services\API\Types\Tools;
2424
use Felix_Arntz\AI_Services\Services\API\Types\Tools\Function_Declarations_Tool;
@@ -79,7 +79,7 @@ class Anthropic_AI_Model implements Generative_AI_Model, With_Chat_History, With
7979
* The generation configuration.
8080
*
8181
* @since 0.1.0
82-
* @var Generation_Config|null
82+
* @var Text_Generation_Config|null
8383
*/
8484
private $generation_config;
8585

@@ -133,7 +133,7 @@ public function __construct( Anthropic_AI_API_Client $api, string $model, array
133133
array(
134134
'param_key' => 'generationConfig',
135135
'property_name' => 'generation_config',
136-
'class_name' => Generation_Config::class,
136+
'class_name' => Text_Generation_Config::class,
137137
),
138138
);
139139
foreach ( $data_obj_params as $data_obj_param ) {
@@ -547,29 +547,29 @@ private static function get_content_transformers(): array {
547547
*/
548548
private static function get_generation_config_transformers(): array {
549549
return array(
550-
'stop_sequences' => static function ( Generation_Config $config ) {
550+
'stop_sequences' => static function ( Text_Generation_Config $config ) {
551551
return $config->get_stop_sequences();
552552
},
553-
'max_tokens' => static function ( Generation_Config $config ) {
553+
'max_tokens' => static function ( Text_Generation_Config $config ) {
554554
$max_tokens = $config->get_max_output_tokens();
555555
if ( ! $max_tokens ) {
556556
// The 'max_tokens' parameter is required in the Anthropic API, so we need a default.
557557
return 4096;
558558
}
559559
return $max_tokens;
560560
},
561-
'temperature' => static function ( Generation_Config $config ) {
561+
'temperature' => static function ( Text_Generation_Config $config ) {
562562
$temperature = $config->get_temperature();
563563
if ( $temperature > 1.0 ) {
564564
// The Anthropic API only supports a temperature of up to 1.0, so we need to cap it.
565565
return 1.0;
566566
}
567567
return $temperature;
568568
},
569-
'top_p' => static function ( Generation_Config $config ) {
569+
'top_p' => static function ( Text_Generation_Config $config ) {
570570
return $config->get_top_p();
571571
},
572-
'top_k' => static function ( Generation_Config $config ) {
572+
'top_k' => static function ( Text_Generation_Config $config ) {
573573
return $config->get_top_k();
574574
},
575575
);

includes/Anthropic/Anthropic_AI_Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static function ( array $models_data, array $model_data ) use ( $anthropic_legac
170170
* a model slug.
171171
* @type Tools|null $tools The tools to use for the model. Default none.
172172
* @type Tool_Config|null $toolConfig Tool configuration options. Default none.
173-
* @type Generation_Config|null $generationConfig Model generation configuration options. Default none.
173+
* @type Generation_Config|null $generationConfig Model generation configuration options. Default none.
174174
* @type string|Parts|Content $systemInstruction The system instruction for the model. Default none.
175175
* }
176176
* @param array<string, mixed> $request_options Optional. The request options. Default empty array.

includes/Google/Google_AI_Model.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
use Felix_Arntz\AI_Services\Services\API\Types\Candidate;
1414
use Felix_Arntz\AI_Services\Services\API\Types\Candidates;
1515
use Felix_Arntz\AI_Services\Services\API\Types\Content;
16-
use Felix_Arntz\AI_Services\Services\API\Types\Generation_Config;
1716
use Felix_Arntz\AI_Services\Services\API\Types\Parts;
1817
use Felix_Arntz\AI_Services\Services\API\Types\Parts\File_Data_Part;
1918
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Function_Call_Part;
2019
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Function_Response_Part;
2120
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Inline_Data_Part;
2221
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Text_Part;
22+
use Felix_Arntz\AI_Services\Services\API\Types\Text_Generation_Config;
2323
use Felix_Arntz\AI_Services\Services\API\Types\Tool_Config;
2424
use Felix_Arntz\AI_Services\Services\API\Types\Tools;
2525
use Felix_Arntz\AI_Services\Services\API\Types\Tools\Function_Declarations_Tool;
@@ -80,7 +80,7 @@ class Google_AI_Model implements Generative_AI_Model, With_Chat_History, With_Fu
8080
* The generation configuration.
8181
*
8282
* @since 0.1.0
83-
* @var Generation_Config|null
83+
* @var Text_Generation_Config|null
8484
*/
8585
private $generation_config;
8686

@@ -146,7 +146,7 @@ public function __construct( Google_AI_API_Client $api, string $model, array $mo
146146
array(
147147
'param_key' => 'generationConfig',
148148
'property_name' => 'generation_config',
149-
'class_name' => Generation_Config::class,
149+
'class_name' => Text_Generation_Config::class,
150150
),
151151
);
152152
foreach ( $data_obj_params as $data_obj_param ) {
@@ -552,44 +552,44 @@ private static function get_content_transformers(): array {
552552
*/
553553
private static function get_generation_config_transformers(): array {
554554
return array(
555-
'stopSequences' => static function ( Generation_Config $config ) {
555+
'stopSequences' => static function ( Text_Generation_Config $config ) {
556556
return $config->get_stop_sequences();
557557
},
558-
'responseMimeType' => static function ( Generation_Config $config ) {
558+
'responseMimeType' => static function ( Text_Generation_Config $config ) {
559559
return $config->get_response_mime_type();
560560
},
561-
'responseSchema' => static function ( Generation_Config $config ) {
561+
'responseSchema' => static function ( Text_Generation_Config $config ) {
562562
if ( $config->get_response_mime_type() === 'application/json' ) {
563563
return $config->get_response_schema();
564564
}
565565
return array();
566566
},
567-
'candidateCount' => static function ( Generation_Config $config ) {
567+
'candidateCount' => static function ( Text_Generation_Config $config ) {
568568
return $config->get_candidate_count();
569569
},
570-
'maxOutputTokens' => static function ( Generation_Config $config ) {
570+
'maxOutputTokens' => static function ( Text_Generation_Config $config ) {
571571
return $config->get_max_output_tokens();
572572
},
573-
'temperature' => static function ( Generation_Config $config ) {
573+
'temperature' => static function ( Text_Generation_Config $config ) {
574574
// In the Google AI API temperature ranges from 0.0 to 2.0.
575575
return $config->get_temperature() * 2.0;
576576
},
577-
'topP' => static function ( Generation_Config $config ) {
577+
'topP' => static function ( Text_Generation_Config $config ) {
578578
return $config->get_top_p();
579579
},
580-
'topK' => static function ( Generation_Config $config ) {
580+
'topK' => static function ( Text_Generation_Config $config ) {
581581
return $config->get_top_k();
582582
},
583-
'presencePenalty' => static function ( Generation_Config $config ) {
583+
'presencePenalty' => static function ( Text_Generation_Config $config ) {
584584
return $config->get_presence_penalty();
585585
},
586-
'frequencyPenalty' => static function ( Generation_Config $config ) {
586+
'frequencyPenalty' => static function ( Text_Generation_Config $config ) {
587587
return $config->get_frequency_penalty();
588588
},
589-
'responseLogprobs' => static function ( Generation_Config $config ) {
589+
'responseLogprobs' => static function ( Text_Generation_Config $config ) {
590590
return $config->get_response_logprobs();
591591
},
592-
'logprobs' => static function ( Generation_Config $config ) {
592+
'logprobs' => static function ( Text_Generation_Config $config ) {
593593
return $config->get_logprobs();
594594
},
595595
);

includes/Google/Google_AI_Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static function ( array $models_data, array $model_data ) use ( $google_legacy_c
181181
* a model slug.
182182
* @type Tools|null $tools The tools to use for the model. Default none.
183183
* @type Tool_Config|null $toolConfig Tool configuration options. Default none.
184-
* @type Generation_Config|null $generationConfig Model generation configuration options. Default none.
184+
* @type Generation_Config|null $generationConfig Model generation configuration options. Default none.
185185
* @type string|Parts|Content $systemInstruction The system instruction for the model. Default none.
186186
* @type Safety_Setting[] $safetySettings Optional. The safety settings for the model. Default empty
187187
* array.

includes/OpenAI/OpenAI_AI_Model.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
use Felix_Arntz\AI_Services\Services\API\Types\Candidate;
1313
use Felix_Arntz\AI_Services\Services\API\Types\Candidates;
1414
use Felix_Arntz\AI_Services\Services\API\Types\Content;
15-
use Felix_Arntz\AI_Services\Services\API\Types\Generation_Config;
1615
use Felix_Arntz\AI_Services\Services\API\Types\Parts;
1716
use Felix_Arntz\AI_Services\Services\API\Types\Parts\File_Data_Part;
1817
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Function_Call_Part;
1918
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Function_Response_Part;
2019
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Inline_Data_Part;
2120
use Felix_Arntz\AI_Services\Services\API\Types\Parts\Text_Part;
21+
use Felix_Arntz\AI_Services\Services\API\Types\Text_Generation_Config;
2222
use Felix_Arntz\AI_Services\Services\API\Types\Tool_Config;
2323
use Felix_Arntz\AI_Services\Services\API\Types\Tools;
2424
use Felix_Arntz\AI_Services\Services\API\Types\Tools\Function_Declarations_Tool;
@@ -79,7 +79,7 @@ class OpenAI_AI_Model implements Generative_AI_Model, With_Chat_History, With_Fu
7979
* The generation configuration.
8080
*
8181
* @since 0.1.0
82-
* @var Generation_Config|null
82+
* @var Text_Generation_Config|null
8383
*/
8484
private $generation_config;
8585

@@ -133,7 +133,7 @@ public function __construct( OpenAI_AI_API_Client $api, string $model, array $mo
133133
array(
134134
'param_key' => 'generationConfig',
135135
'property_name' => 'generation_config',
136-
'class_name' => Generation_Config::class,
136+
'class_name' => Text_Generation_Config::class,
137137
),
138138
);
139139
foreach ( $data_obj_params as $data_obj_param ) {
@@ -575,10 +575,10 @@ private static function get_content_transformers(): array {
575575
*/
576576
private static function get_generation_config_transformers(): array {
577577
return array(
578-
'stop' => static function ( Generation_Config $config ) {
578+
'stop' => static function ( Text_Generation_Config $config ) {
579579
return $config->get_stop_sequences();
580580
},
581-
'response_format' => static function ( Generation_Config $config ) {
581+
'response_format' => static function ( Text_Generation_Config $config ) {
582582
if ( $config->get_response_mime_type() === 'application/json' ) {
583583
$schema = $config->get_response_schema();
584584
if ( $schema ) {
@@ -591,28 +591,28 @@ private static function get_generation_config_transformers(): array {
591591
}
592592
return array();
593593
},
594-
'n' => static function ( Generation_Config $config ) {
594+
'n' => static function ( Text_Generation_Config $config ) {
595595
return $config->get_candidate_count();
596596
},
597-
'max_completion_tokens' => static function ( Generation_Config $config ) {
597+
'max_completion_tokens' => static function ( Text_Generation_Config $config ) {
598598
return $config->get_max_output_tokens();
599599
},
600-
'temperature' => static function ( Generation_Config $config ) {
600+
'temperature' => static function ( Text_Generation_Config $config ) {
601601
return $config->get_temperature();
602602
},
603-
'top_p' => static function ( Generation_Config $config ) {
603+
'top_p' => static function ( Text_Generation_Config $config ) {
604604
return $config->get_top_p();
605605
},
606-
'presence_penalty' => static function ( Generation_Config $config ) {
606+
'presence_penalty' => static function ( Text_Generation_Config $config ) {
607607
return $config->get_presence_penalty();
608608
},
609-
'frequency_penalty' => static function ( Generation_Config $config ) {
609+
'frequency_penalty' => static function ( Text_Generation_Config $config ) {
610610
return $config->get_frequency_penalty();
611611
},
612-
'logprobs' => static function ( Generation_Config $config ) {
612+
'logprobs' => static function ( Text_Generation_Config $config ) {
613613
return $config->get_response_logprobs();
614614
},
615-
'top_logprobs' => static function ( Generation_Config $config ) {
615+
'top_logprobs' => static function ( Text_Generation_Config $config ) {
616616
return $config->get_logprobs();
617617
},
618618
);

includes/OpenAI/OpenAI_AI_Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static function ( array $models_data, array $model_data ) use ( $gpt_capabilitie
181181
* a model slug.
182182
* @type Tools|null $tools The tools to use for the model. Default none.
183183
* @type Tool_Config|null $toolConfig Tool configuration options. Default none.
184-
* @type Generation_Config|null $generationConfig Model generation configuration options. Default none.
184+
* @type Generation_Config|null $generationConfig Model generation configuration options. Default none.
185185
* @type string|Parts|Content $systemInstruction The system instruction for the model. Default none.
186186
* }
187187
* @param array<string, mixed> $request_options Optional. The request options. Default empty array.

includes/Services/API/Types/Generation_Config.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
* Class representing configuration options for a generative AI model.
1818
*
1919
* @since 0.2.0
20+
* @since n.e.x.t Deprecated in favor of the `Text_Generation_Config` class.
2021
*/
21-
final class Generation_Config implements Arrayable, With_JSON_Schema {
22+
class Generation_Config implements Arrayable, With_JSON_Schema {
2223

2324
/**
2425
* The sanitized configuration arguments.
@@ -67,6 +68,21 @@ final class Generation_Config implements Arrayable, With_JSON_Schema {
6768
* @param array<string, mixed> $args The configuration arguments.
6869
*/
6970
public function __construct( array $args ) {
71+
// Trigger deprecation notice if this class is instantiated directly.
72+
if ( ! $this instanceof Text_Generation_Config ) {
73+
if ( function_exists( '_deprecated_class' ) ) {
74+
_deprecated_class( __CLASS__, 'n.e.x.t', Text_Generation_Config::class );
75+
} elseif ( WP_DEBUG ) {
76+
$message = sprintf(
77+
'Class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
78+
__CLASS__,
79+
'n.e.x.t',
80+
Text_Generation_Config::class
81+
);
82+
wp_trigger_error( '', $message, E_USER_DEPRECATED );
83+
}
84+
}
85+
7086
$args = $this->sanitize_args( $args );
7187

7288
$this->sanitized_args = $args['sanitized'];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Class Felix_Arntz\AI_Services\Services\API\Types\Text_Generation_Config
4+
*
5+
* @since n.e.x.t
6+
* @package ai-services
7+
*/
8+
9+
namespace Felix_Arntz\AI_Services\Services\API\Types;
10+
11+
use InvalidArgumentException;
12+
13+
/**
14+
* Class representing text configuration options for a generative AI model.
15+
*
16+
* @since n.e.x.t Replacing `Generation_Config`.
17+
*/
18+
class Text_Generation_Config extends Generation_Config {
19+
20+
/**
21+
* Creates a Generation_Config instance from an array of content data.
22+
*
23+
* @since 0.2.0
24+
*
25+
* @param array<string, mixed> $data The content data.
26+
* @return Generation_Config Generation_Config instance.
27+
*
28+
* @phpstan-return Text_Generation_Config
29+
*
30+
* @throws InvalidArgumentException Thrown if the data is missing required fields.
31+
*/
32+
public static function from_array( array $data ): Generation_Config {
33+
return new Text_Generation_Config( $data );
34+
}
35+
}

0 commit comments

Comments
 (0)