|
| 1 | +Feature: Generate AI content |
| 2 | + |
| 3 | + Background: |
| 4 | + Given a WP install |
| 5 | + And a wp-content/mu-plugins/mock-provider.php file: |
| 6 | + """ |
| 7 | + <?php |
| 8 | +
|
| 9 | + use WordPress\AiClient\AiClient; |
| 10 | + use WordPress\AiClient\Messages\DTO\MessagePart; |
| 11 | + use WordPress\AiClient\Messages\DTO\ModelMessage; |
| 12 | + use WordPress\AiClient\Messages\Enums\ModalityEnum; |
| 13 | + use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface; |
| 14 | + use WordPress\AiClient\Providers\Contracts\ProviderAvailabilityInterface; |
| 15 | + use WordPress\AiClient\Providers\Contracts\ProviderInterface; |
| 16 | + use WordPress\AiClient\Providers\DTO\ProviderMetadata; |
| 17 | + use WordPress\AiClient\Providers\Enums\ProviderTypeEnum; |
| 18 | + use WordPress\AiClient\Providers\Models\Contracts\ModelInterface; |
| 19 | + use WordPress\AiClient\Providers\Models\TextGeneration\Contracts\TextGenerationModelInterface; |
| 20 | + use WordPress\AiClient\Providers\Models\DTO\ModelConfig; |
| 21 | + use WordPress\AiClient\Providers\Models\DTO\ModelMetadata; |
| 22 | + use WordPress\AiClient\Providers\Models\DTO\SupportedOption; |
| 23 | + use WordPress\AiClient\Providers\Models\Enums\CapabilityEnum; |
| 24 | + use WordPress\AiClient\Providers\Models\Enums\OptionEnum; |
| 25 | + use WordPress\AiClient\Results\DTO\Candidate; |
| 26 | + use WordPress\AiClient\Results\DTO\GenerativeAiResult; |
| 27 | + use WordPress\AiClient\Results\DTO\TokenUsage; |
| 28 | + use WordPress\AiClient\Results\Enums\FinishReasonEnum; |
| 29 | + use WordPress\AI_Client\API_Credentials\API_Credentials_Manager; |
| 30 | +
|
| 31 | + class WP_CLI_Mock_Model implements ModelInterface, TextGenerationModelInterface { |
| 32 | + private $id; |
| 33 | + private $config; |
| 34 | +
|
| 35 | + public function __construct( $id ) { |
| 36 | + $this->id = $id; |
| 37 | + $this->config = new ModelConfig(); |
| 38 | + } |
| 39 | +
|
| 40 | + public function metadata(): ModelMetadata { |
| 41 | + return new ModelMetadata( |
| 42 | + $this->id, |
| 43 | + 'WP-CLI Mock Model', |
| 44 | + // Supported capabilities. |
| 45 | + [ |
| 46 | + CapabilityEnum::textGeneration(), |
| 47 | + CapabilityEnum::imageGeneration(), |
| 48 | + ], |
| 49 | + // Supported options. |
| 50 | + [ |
| 51 | + new SupportedOption( |
| 52 | + OptionEnum::inputModalities(), |
| 53 | + [ |
| 54 | + [ModalityEnum::text()] |
| 55 | + ] |
| 56 | + ), |
| 57 | + new SupportedOption( |
| 58 | + OptionEnum::outputModalities(), |
| 59 | + [ |
| 60 | + [ModalityEnum::text()], |
| 61 | + [ModalityEnum::text(), ModalityEnum::image()], |
| 62 | + ] |
| 63 | + ), |
| 64 | + ] |
| 65 | + ); |
| 66 | + } |
| 67 | +
|
| 68 | + public function providerMetadata(): ProviderMetadata { |
| 69 | + return WP_CLI_Mock_Provider::metadata(); |
| 70 | + } |
| 71 | +
|
| 72 | + public function setConfig( ModelConfig $config ): void { |
| 73 | + $this->config = $config; |
| 74 | + } |
| 75 | +
|
| 76 | + public function getConfig(): ModelConfig { |
| 77 | + return $this->config; |
| 78 | + } |
| 79 | +
|
| 80 | + public function generateTextResult(array $prompt): GenerativeAiResult { |
| 81 | + // throw new RuntimeException('No candidates were generated'); |
| 82 | +
|
| 83 | + $modelMessage = new ModelMessage([ |
| 84 | + new MessagePart('Generated content') |
| 85 | + ]); |
| 86 | + $candidate = new Candidate( |
| 87 | + $modelMessage, |
| 88 | + FinishReasonEnum::stop(), |
| 89 | + 42 |
| 90 | + ); |
| 91 | + $tokenUsage = new TokenUsage(10, 42, 52); |
| 92 | + return new GenerativeAiResult( |
| 93 | + 'result_123', |
| 94 | + [ $candidate ], |
| 95 | + $tokenUsage, |
| 96 | + $this->providerMetadata(), |
| 97 | + $this->metadata(), |
| 98 | + [ 'provider' => 'wp-cli-mock-provider' ] |
| 99 | + ); |
| 100 | + } |
| 101 | +
|
| 102 | + public function streamGenerateTextResult(array $prompt): Generator { |
| 103 | + yield from []; |
| 104 | + } |
| 105 | + } |
| 106 | +
|
| 107 | + class WP_CLI_Mock_Provider implements ProviderInterface { |
| 108 | + public static function metadata(): ProviderMetadata { |
| 109 | + return new ProviderMetadata( 'wp-cli-mock-provider', 'WP-CLI Mock Provider', ProviderTypeEnum::cloud() ); |
| 110 | + } |
| 111 | +
|
| 112 | + public static function model( string $modelId, ?ModelConfig $modelConfig = null ): ModelInterface { |
| 113 | + return new WP_CLI_Mock_Model( $modelId ); |
| 114 | + } |
| 115 | +
|
| 116 | + public static function availability(): ProviderAvailabilityInterface { |
| 117 | + return new class() implements ProviderAvailabilityInterface { |
| 118 | + public function isConfigured(): bool { |
| 119 | + return true; |
| 120 | + } |
| 121 | + }; |
| 122 | + } |
| 123 | +
|
| 124 | + public static function modelMetadataDirectory(): ModelMetadataDirectoryInterface { |
| 125 | + return new class() implements ModelMetadataDirectoryInterface { |
| 126 | + public function listModelMetadata(): array { |
| 127 | + return [ |
| 128 | + ( new WP_CLI_Mock_Model( 'wp-cli-mock-model' ) )->metadata() |
| 129 | + ]; |
| 130 | + } |
| 131 | +
|
| 132 | + public function hasModelMetadata( string $modelId ): bool { |
| 133 | + return true; |
| 134 | + } |
| 135 | +
|
| 136 | + public function getModelMetadata( string $modelId ): ModelMetadata { |
| 137 | + return self::model()->metadata(); |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | + } |
| 142 | +
|
| 143 | + WP_CLI::add_hook( |
| 144 | + 'ai_client_init', |
| 145 | + static function () { |
| 146 | + AiClient::defaultRegistry()->registerProvider( WP_CLI_Mock_Provider::class ); |
| 147 | +
|
| 148 | + ( new API_Credentials_Manager() )->initialize(); |
| 149 | + } |
| 150 | + ); |
| 151 | + """ |
| 152 | + |
| 153 | + Scenario: Generate command validates model format |
| 154 | + When I try `wp ai generate text "Test prompt" --model=invalidformat` |
| 155 | + Then the return code should be 1 |
| 156 | + |
| 157 | + Scenario: Generate command validates max-tokens |
| 158 | + When I try `wp ai generate text "Test prompt" --max-tokens=-5` |
| 159 | + Then the return code should be 1 |
| 160 | + And STDERR should contain: |
| 161 | + """ |
| 162 | + Max tokens must be a positive integer |
| 163 | + """ |
| 164 | + |
| 165 | + Scenario: Generate command validates top-p range |
| 166 | + When I try `wp ai generate text "Test prompt" --top-p=1.5` |
| 167 | + Then the return code should be 1 |
| 168 | + And STDERR should contain: |
| 169 | + """ |
| 170 | + Top-p must be between 0.0 and 1.0 |
| 171 | + """ |
| 172 | + |
| 173 | + Scenario: Generate command validates top-k positive |
| 174 | + When I try `wp ai generate text "Test prompt" --top-k=-10` |
| 175 | + Then the return code should be 1 |
| 176 | + And STDERR should contain: |
| 177 | + """ |
| 178 | + Top-k must be a positive integer |
| 179 | + """ |
0 commit comments