WordPress\AiClient\Builders

PromptBuilder::getConfiguredModelprivateWP 0.1.0

Gets the model to use for generation.

If a model has been explicitly set, validates it meets requirements and returns it. Otherwise, finds a suitable model based on the prompt requirements.

Method of the class: PromptBuilder{}

No Hooks.

Returns

ModelInterface. The model to use.

Usage

// private - for code of main (parent) class only
$result = $this->getConfiguredModel( $capability ): ModelInterface;
$capability(CapabilityEnum) (required)
The capability the model will be using.

Changelog

Since 0.1.0 Introduced.

PromptBuilder::getConfiguredModel() code WP 7.0

private function getConfiguredModel(CapabilityEnum $capability): ModelInterface
{
    $requirements = ModelRequirements::fromPromptData($capability, $this->messages, $this->modelConfig);
    if ($this->model !== null) {
        // Explicit model was provided via usingModel(); just update config and bind dependencies.
        $model = $this->model;
        $model->setConfig($this->modelConfig);
        $this->registry->bindModelDependencies($model);
        $this->bindModelRequestOptions($model);
        return $model;
    }
    // Retrieve the candidate models map which satisfies the requirements.
    $candidateMap = $this->getCandidateModelsMap($requirements);
    if (empty($candidateMap)) {
        $message = sprintf('No models found that support %s for this prompt.', $capability->value);
        if ($this->providerIdOrClassName !== null) {
            $message = sprintf('No models found for provider "%s" that support %s for this prompt.', $this->providerIdOrClassName, $capability->value);
        }
        throw new InvalidArgumentException($message);
    }
    // Check if any preferred models match the candidates, in priority order.
    if (!empty($this->modelPreferenceKeys)) {
        // Find preferences that match available candidates, preserving preference order.
        $matchingPreferences = array_intersect_key(array_flip($this->modelPreferenceKeys), $candidateMap);
        if (!empty($matchingPreferences)) {
            // Get the first matching preference key
            $firstMatchKey = key($matchingPreferences);
            [$providerId, $modelId] = $candidateMap[$firstMatchKey];
            $model = $this->registry->getProviderModel($providerId, $modelId, $this->modelConfig);
            $this->bindModelRequestOptions($model);
            return $model;
        }
    }
    // No preference matched; fall back to the first candidate discovered.
    [$providerId, $modelId] = reset($candidateMap);
    $model = $this->registry->getProviderModel($providerId, $modelId, $this->modelConfig);
    $this->bindModelRequestOptions($model);
    return $model;
}