wppaste
WordPress

AbstractOpenAiCompatibleTextGenerationModel

Since
0.1.0
Source
wp-includes/php-ai-client/src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleTextGenerationModel.php:64
Base class for a text generation model for providers that implement OpenAI's API format.

Description

This abstract class is designed to work with any AI provider that offers an OpenAI-compatible API endpoint, including but not limited to Anthropic, Google, and other providers that have adopted OpenAI's API specification as a standard interface.

Compatibility

WordPress
since 0.1.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 0.1.0.

Methods · 17

Source code

abstract class AbstractOpenAiCompatibleTextGenerationModel extends AbstractApiBasedModel implements TextGenerationModelInterface{    /**     * {@inheritDoc}     *     * @since 0.1.0     */    final public function generateTextResult(array $prompt): GenerativeAiResult    {        $httpTransporter = $this->getHttpTransporter();        $params = $this->prepareGenerateTextParams($prompt);        $request = $this->createRequest(HttpMethodEnum::POST(), 'chat/completions', ['Content-Type' => 'application/json'], $params);        // Add authentication credentials to the request.        $request = $this->getRequestAuthentication()->authenticateRequest($request);        // Send and process the request.        $response = $httpTransporter->send($request);        $this->throwIfNotSuccessful($response);        return $this->parseResponseToGenerativeAiResult($response);    }    /**     * Prepares the given prompt and the model configuration into parameters for the API request.     *     * @since 0.1.0     *     * @param list<Message> $prompt The prompt to generate text for. Either a single message or a list of messages     *                              from a chat.     * @return array<string, mixed> The parameters for the API request.     */    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();

Changelog

Introduced in 0.1.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/php-ai-client/src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleTextGenerationModel.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.