WordPress\AiClient\Providers\OpenAiCompatibleImplementation
AbstractOpenAiCompatibleTextGenerationModel::getMessagePartContentData
Returns the OpenAI API specific content data for a message part.
Method of the class: AbstractOpenAiCompatibleTextGenerationModel{}
No Hooks.
Returns
?Array
Usage
// protected - for code of main (parent) or child class $result = $this->getMessagePartContentData( $part ): ?array;
- $part(MessagePart) (required)
- The message part to get the data for.
Changelog
| Since 0.1.0 | Introduced. |
AbstractOpenAiCompatibleTextGenerationModel::getMessagePartContentData() AbstractOpenAiCompatibleTextGenerationModel::getMessagePartContentData code WP 7.0
protected function getMessagePartContentData(MessagePart $part): ?array
{
$type = $part->getType();
if ($type->isText()) {
/*
* The OpenAI Chat Completions API spec does not support annotating thought parts as input,
* so we instead skip them.
*/
if ($part->getChannel()->isThought()) {
return null;
}
return ['type' => 'text', 'text' => $part->getText()];
}
if ($type->isFile()) {
$file = $part->getFile();
if (!$file) {
// This should be impossible due to class internals, but still needs to be checked.
throw new RuntimeException('The file typed message part must contain a file.');
}
if ($file->isRemote()) {
if ($file->isImage()) {
return ['type' => 'image_url', 'image_url' => ['url' => $file->getUrl()]];
}
throw new InvalidArgumentException(sprintf('Unsupported MIME type "%s" for remote file message part.', $file->getMimeType()));
}
// Else, it is an inline file.
if ($file->isImage()) {
return ['type' => 'image_url', 'image_url' => ['url' => $file->getDataUri()]];
}
if ($file->isAudio()) {
return ['type' => 'input_audio', 'input_audio' => ['data' => $file->getBase64Data(), 'format' => $file->getMimeTypeObject()->toExtension()]];
}
throw new InvalidArgumentException(sprintf('Unsupported MIME type "%s" for inline file message part.', $file->getMimeType()));
}
if ($type->isFunctionCall()) {
// Skip, as this is separately included. See `getMessagePartToolCallData()`.
return null;
}
if ($type->isFunctionResponse()) {
// Special case: Function response.
throw new InvalidArgumentException('The API only allows a single function response, as the only content of the message.');
}
throw new InvalidArgumentException(sprintf('Unsupported message part type "%s".', $type));
}