WordPress\AiClient\Providers\OpenAiCompatibleImplementation
AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceMessageToolCallPart
Parses a tool call part from the API response.
Method of the class: AbstractOpenAiCompatibleTextGenerationModel{}
No Hooks.
Returns
MessagePart|null. The parsed message part for the tool call, or null if not applicable.
Usage
// protected - for code of main (parent) or child class $result = $this->parseResponseChoiceMessageToolCallPart( $toolCallData ): ?MessagePart;
- $toolCallData(ToolCallData) (required)
- The tool call data from the API response.
Changelog
| Since 0.1.0 | Introduced. |
AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceMessageToolCallPart() AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceMessageToolCallPart code WP 7.0
protected function parseResponseChoiceMessageToolCallPart(array $toolCallData): ?MessagePart
{
/*
* For now, only function calls are supported.
*
* Not all OpenAI compatible APIs include a 'type' key, so we only check its value if it is set.
*/
if (isset($toolCallData['type']) && 'function' !== $toolCallData['type'] || !isset($toolCallData['function']) || !is_array($toolCallData['function'])) {
return null;
}
$functionArguments = is_string($toolCallData['function']['arguments']) ? json_decode($toolCallData['function']['arguments'], \true) : $toolCallData['function']['arguments'];
$functionCall = new FunctionCall(isset($toolCallData['id']) && is_string($toolCallData['id']) ? $toolCallData['id'] : null, isset($toolCallData['function']['name']) && is_string($toolCallData['function']['name']) ? $toolCallData['function']['name'] : null, $functionArguments);
return new MessagePart($functionCall);
}