Skip to content

Commit 7367980

Browse files
authored
Merge pull request #65 from WordPress/prompt-builder-using-config
Adds usingModelConfig method to PromptBuilder
2 parents 16ca404 + bef5e8e commit 7367980

File tree

4 files changed

+161
-6
lines changed

4 files changed

+161
-6
lines changed

src/Builders/PromptBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,32 @@ public function usingModel(ModelInterface $model): self
203203
return $this;
204204
}
205205

206+
/**
207+
* Sets the model configuration.
208+
*
209+
* Merges the provided configuration with the builder's configuration,
210+
* with builder configuration taking precedence.
211+
*
212+
* @since n.e.x.t
213+
*
214+
* @param ModelConfig $config The model configuration to merge.
215+
* @return self
216+
*/
217+
public function usingModelConfig(ModelConfig $config): self
218+
{
219+
// Convert both configs to arrays
220+
$builderConfigArray = $this->modelConfig->toArray();
221+
$providedConfigArray = $config->toArray();
222+
223+
// Merge arrays with builder config taking precedence
224+
$mergedArray = array_merge($providedConfigArray, $builderConfigArray);
225+
226+
// Create new config from merged array
227+
$this->modelConfig = ModelConfig::fromArray($mergedArray);
228+
229+
return $this;
230+
}
231+
206232
/**
207233
* Sets the provider to use for generation.
208234
*

src/Providers/Models/DTO/ModelConfig.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,9 @@ static function (FunctionDeclaration $function_declaration): array {
909909
$data[self::KEY_OUTPUT_MEDIA_ASPECT_RATIO] = $this->outputMediaAspectRatio;
910910
}
911911

912-
$data[self::KEY_CUSTOM_OPTIONS] = $this->customOptions;
912+
if (!empty($this->customOptions)) {
913+
$data[self::KEY_CUSTOM_OPTIONS] = $this->customOptions;
914+
}
913915

914916
return $data;
915917
}

tests/unit/Builders/PromptBuilderTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,99 @@ public function testUsingModel(): void
642642
$this->assertSame($model, $actualModel);
643643
}
644644

645+
/**
646+
* Tests usingModelConfig method.
647+
*
648+
* @return void
649+
*/
650+
public function testUsingModelConfig(): void
651+
{
652+
$builder = new PromptBuilder($this->registry);
653+
654+
// Set some initial config values on the builder
655+
$builder->usingSystemInstruction('Builder instruction')
656+
->usingMaxTokens(500)
657+
->usingTemperature(0.5);
658+
659+
// Create a config to merge
660+
$config = new ModelConfig();
661+
$config->setSystemInstruction('Config instruction');
662+
$config->setMaxTokens(1000);
663+
$config->setTopP(0.9);
664+
$config->setTopK(40);
665+
666+
$result = $builder->usingModelConfig($config);
667+
668+
// Assert fluent interface
669+
$this->assertSame($builder, $result);
670+
671+
// Get the merged config
672+
$reflection = new \ReflectionClass($builder);
673+
$configProperty = $reflection->getProperty('modelConfig');
674+
$configProperty->setAccessible(true);
675+
676+
/** @var ModelConfig $mergedConfig */
677+
$mergedConfig = $configProperty->getValue($builder);
678+
679+
// Assert builder values take precedence
680+
$this->assertEquals('Builder instruction', $mergedConfig->getSystemInstruction());
681+
$this->assertEquals(500, $mergedConfig->getMaxTokens());
682+
$this->assertEquals(0.5, $mergedConfig->getTemperature());
683+
684+
// Assert config values are used when builder doesn't have them
685+
$this->assertEquals(0.9, $mergedConfig->getTopP());
686+
$this->assertEquals(40, $mergedConfig->getTopK());
687+
}
688+
689+
/**
690+
* Tests usingModelConfig with custom options.
691+
*
692+
* @return void
693+
*/
694+
public function testUsingModelConfigWithCustomOptions(): void
695+
{
696+
$builder = new PromptBuilder($this->registry);
697+
698+
// Create a config with custom options
699+
$config = new ModelConfig();
700+
$config->setCustomOption('stopSequences', ['CONFIG_STOP']);
701+
$config->setCustomOption('otherOption', 'value');
702+
703+
$builder->usingModelConfig($config);
704+
705+
// Get the merged config
706+
$reflection = new \ReflectionClass($builder);
707+
$configProperty = $reflection->getProperty('modelConfig');
708+
$configProperty->setAccessible(true);
709+
710+
/** @var ModelConfig $mergedConfig */
711+
$mergedConfig = $configProperty->getValue($builder);
712+
$customOptions = $mergedConfig->getCustomOptions();
713+
714+
// Assert config custom options are preserved
715+
$this->assertArrayHasKey('stopSequences', $customOptions);
716+
$this->assertIsArray($customOptions['stopSequences']);
717+
$this->assertEquals(['CONFIG_STOP'], $customOptions['stopSequences']);
718+
$this->assertArrayHasKey('otherOption', $customOptions);
719+
$this->assertEquals('value', $customOptions['otherOption']);
720+
721+
// Now set a builder value that overrides one of the custom options
722+
$builder->usingStopSequences('STOP');
723+
724+
// Get the config again
725+
$mergedConfig = $configProperty->getValue($builder);
726+
$customOptions = $mergedConfig->getCustomOptions();
727+
728+
// Assert builder's stop sequences override the config's
729+
$this->assertArrayHasKey('stopSequences', $customOptions);
730+
$this->assertIsArray($customOptions['stopSequences']);
731+
$this->assertEquals(['STOP'], $customOptions['stopSequences']);
732+
733+
// Assert other custom options are still preserved
734+
$this->assertArrayHasKey('otherOption', $customOptions);
735+
$this->assertEquals('value', $customOptions['otherOption']);
736+
}
737+
645738
/**
646739
* Tests usingProvider method.
647740
*

tests/unit/Providers/Models/DTO/ModelConfigTest.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,8 @@ public function testToArrayNoProperties(): void
304304
$array = $config->toArray();
305305

306306
$this->assertIsArray($array);
307-
$this->assertCount(1, $array);
308-
$this->assertArrayHasKey(ModelConfig::KEY_CUSTOM_OPTIONS, $array);
309-
$this->assertEquals([], $array[ModelConfig::KEY_CUSTOM_OPTIONS]);
307+
$this->assertCount(0, $array);
308+
$this->assertArrayNotHasKey(ModelConfig::KEY_CUSTOM_OPTIONS, $array);
310309
}
311310

312311
/**
@@ -323,14 +322,49 @@ public function testToArrayPartialProperties(): void
323322
$array = $config->toArray();
324323

325324
$this->assertIsArray($array);
326-
$this->assertCount(3, $array);
325+
$this->assertCount(2, $array);
327326
$this->assertEquals(0.5, $array[ModelConfig::KEY_TEMPERATURE]);
328327
$this->assertEquals(100, $array[ModelConfig::KEY_MAX_TOKENS]);
329-
$this->assertEquals([], $array[ModelConfig::KEY_CUSTOM_OPTIONS]);
328+
$this->assertArrayNotHasKey(ModelConfig::KEY_CUSTOM_OPTIONS, $array);
330329
$this->assertArrayNotHasKey(ModelConfig::KEY_SYSTEM_INSTRUCTION, $array);
331330
$this->assertArrayNotHasKey(ModelConfig::KEY_TOP_P, $array);
332331
}
333332

333+
/**
334+
* Tests custom options are only included when not empty.
335+
*
336+
* @return void
337+
*/
338+
public function testToArrayCustomOptionsOnlyIncludedWhenNotEmpty(): void
339+
{
340+
// Test with empty custom options (default)
341+
$config = new ModelConfig();
342+
$config->setTemperature(0.7);
343+
344+
$array = $config->toArray();
345+
$this->assertArrayNotHasKey(ModelConfig::KEY_CUSTOM_OPTIONS, $array);
346+
347+
// Test with non-empty custom options
348+
$config->setCustomOption('key1', 'value1');
349+
$array = $config->toArray();
350+
$this->assertArrayHasKey(ModelConfig::KEY_CUSTOM_OPTIONS, $array);
351+
$this->assertEquals(['key1' => 'value1'], $array[ModelConfig::KEY_CUSTOM_OPTIONS]);
352+
353+
// Test with multiple custom options
354+
$config->setCustomOption('key2', ['nested' => 'value']);
355+
$array = $config->toArray();
356+
$this->assertArrayHasKey(ModelConfig::KEY_CUSTOM_OPTIONS, $array);
357+
$this->assertEquals([
358+
'key1' => 'value1',
359+
'key2' => ['nested' => 'value']
360+
], $array[ModelConfig::KEY_CUSTOM_OPTIONS]);
361+
362+
// Test resetting custom options to empty
363+
$config->setCustomOptions([]);
364+
$array = $config->toArray();
365+
$this->assertArrayNotHasKey(ModelConfig::KEY_CUSTOM_OPTIONS, $array);
366+
}
367+
334368
/**
335369
* Tests creating from array with all properties.
336370
*

0 commit comments

Comments
 (0)