WordPress\AiClient\Providers\OpenAiCompatibleImplementation

AbstractOpenAiCompatibleTextGenerationModel::prepareGenerateTextParamsprotectedWP 0.1.0

Prepares the given prompt and the model configuration into parameters for the API request.

Method of the class: AbstractOpenAiCompatibleTextGenerationModel{}

No Hooks.

Returns

Array. mixed> The parameters for the API request.

Usage

// protected - for code of main (parent) or child class
$result = $this->prepareGenerateTextParams( $prompt ): array;
$prompt(list) (required)
The prompt to generate text for. Either a single message or a list of messages from a chat.

Changelog

Since 0.1.0 Introduced.

AbstractOpenAiCompatibleTextGenerationModel::prepareGenerateTextParams() code WP 7.0

protected function prepareGenerateTextParams(array $prompt): array
{
    $config = $this->getConfig();
    $params = ['model' => $this->metadata()->getId(), 'messages' => $this->prepareMessagesParam($prompt, $config->getSystemInstruction())];
    $outputModalities = $config->getOutputModalities();
    if (is_array($outputModalities)) {
        $this->validateOutputModalities($outputModalities);
        if (count($outputModalities) > 1) {
            $params['modalities'] = $this->prepareOutputModalitiesParam($outputModalities);
        }
    }
    $candidateCount = $config->getCandidateCount();
    if ($candidateCount !== null) {
        $params['n'] = $candidateCount;
    }
    $maxTokens = $config->getMaxTokens();
    if ($maxTokens !== null) {
        $params['max_tokens'] = $maxTokens;
    }
    $temperature = $config->getTemperature();
    if ($temperature !== null) {
        $params['temperature'] = $temperature;
    }
    $topP = $config->getTopP();
    if ($topP !== null) {
        $params['top_p'] = $topP;
    }
    $stopSequences = $config->getStopSequences();
    if (is_array($stopSequences)) {
        $params['stop'] = $stopSequences;
    }
    $presencePenalty = $config->getPresencePenalty();
    if ($presencePenalty !== null) {
        $params['presence_penalty'] = $presencePenalty;
    }
    $frequencyPenalty = $config->getFrequencyPenalty();
    if ($frequencyPenalty !== null) {
        $params['frequency_penalty'] = $frequencyPenalty;
    }
    $logprobs = $config->getLogprobs();
    if ($logprobs !== null) {
        $params['logprobs'] = $logprobs;
    }
    $topLogprobs = $config->getTopLogprobs();
    if ($topLogprobs !== null) {
        $params['top_logprobs'] = $topLogprobs;
    }
    $functionDeclarations = $config->getFunctionDeclarations();
    if (is_array($functionDeclarations)) {
        $params['tools'] = $this->prepareToolsParam($functionDeclarations);
    }
    $outputMimeType = $config->getOutputMimeType();
    if ('application/json' === $outputMimeType) {
        $outputSchema = $config->getOutputSchema();
        $params['response_format'] = $this->prepareResponseFormatParam($outputSchema);
    }
    /*
     * Any custom options are added to the parameters as well.
     * This allows developers to pass other options that may be more niche or not yet supported by the SDK.
     */
    $customOptions = $config->getCustomOptions();
    foreach ($customOptions as $key => $value) {
        if (isset($params[$key])) {
            throw new InvalidArgumentException(sprintf('The custom option "%s" conflicts with an existing parameter.', $key));
        }
        $params[$key] = $value;
    }
    return $params;
}