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
- generateTextResult(){@inheritDoc}
- prepareGenerateTextParams()Prepares the given prompt and the model configuration into parameters for the API request.
- prepareMessagesParam()Prepares the messages parameter for the API request.
- getMessageRoleString()Returns the OpenAI API specific role string for the given message role.
- getMessagePartContentData()Returns the OpenAI API specific content data for a message part.
- getMessagePartToolCallData()Returns the OpenAI API specific tool calls data for a message part.
- validateOutputModalities()Validates that the given output modalities to ensure that at least one output modality is text.
- prepareOutputModalitiesParam()Prepares the output modalities parameter for the API request.
- prepareToolsParam()Prepares the tools parameter for the API request.
- prepareResponseFormatParam()Prepares the response format parameter for the API request.
- createRequest()Creates a request object for the provider's API.
- throwIfNotSuccessful()Throws an exception if the response is not successful.
- parseResponseToGenerativeAiResult()Parses the response from the API endpoint to a generative AI result.
- parseResponseChoiceToCandidate()Parses a single choice from the API response into a Candidate object.
- parseResponseChoiceMessage()Parses the message from a choice in the API response.
- parseResponseChoiceMessageParts()Parses the message parts from a choice in the API response.
- parseResponseChoiceMessageToolCallPart()Parses a tool call part from the API response.
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.
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.