| Server IP : 5.144.175.180 / Your IP : 216.73.217.25 Web Server : Apache/2.4.63 (Unix) OpenSSL/1.1.1 System : Linux vpsgpz1.itsimplycom.it 4.15.0-20-generic #21-Ubuntu SMP Tue Apr 24 06:16:15 UTC 2018 x86_64 User : edilchimica ( ) PHP Version : 7.4.33 Disable Function : getmyuid,passthru,leak,listen,diskfreespace,tmpfile,link,shell_exec,dl,exec,system,highlight_file,source,show_source,fpassthru,virtual,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,proc_open,proc_close,proc_nice,proc_terminate,escapeshellcmd,ini_alter,popen,pcntl_exec,socket_accept,socket_bind,socket_clear_error,socket_close,socket_connect,symlink,posix_geteuid,ini_alter,socket_listen,socket_create_listen,socket_read,socket_create_pair,stream_socket_server MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home/edilchimica/webapps/edilchimica/wp-includes/php-ai-client/src/Results/DTO/ |
Upload File : |
<?php
declare (strict_types=1);
namespace WordPress\AiClient\Results\DTO;
use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Messages\DTO\Message;
use WordPress\AiClient\Results\Enums\FinishReasonEnum;
/**
* Represents a candidate response from an AI model.
*
* When generating content, AI models can produce multiple candidates.
* Each candidate contains a message and metadata about why generation stopped.
*
* @since 0.1.0
*
* @phpstan-import-type MessageArrayShape from Message
*
* @phpstan-type CandidateArrayShape array{message: MessageArrayShape, finishReason: string}
*
* @extends AbstractDataTransferObject<CandidateArrayShape>
*/
class Candidate extends AbstractDataTransferObject
{
public const KEY_MESSAGE = 'message';
public const KEY_FINISH_REASON = 'finishReason';
/**
* @var Message The generated message.
*/
private Message $message;
/**
* @var FinishReasonEnum The reason generation stopped.
*/
private FinishReasonEnum $finishReason;
/**
* Constructor.
*
* @since 0.1.0
*
* @param Message $message The generated message.
* @param FinishReasonEnum $finishReason The reason generation stopped.
*/
public function __construct(Message $message, FinishReasonEnum $finishReason)
{
if (!$message->getRole()->isModel()) {
throw new InvalidArgumentException('Message must be a model message.');
}
$this->message = $message;
$this->finishReason = $finishReason;
}
/**
* Gets the generated message.
*
* @since 0.1.0
*
* @return Message The message.
*/
public function getMessage(): Message
{
return $this->message;
}
/**
* Gets the finish reason.
*
* @since 0.1.0
*
* @return FinishReasonEnum The finish reason.
*/
public function getFinishReason(): FinishReasonEnum
{
return $this->finishReason;
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*/
public static function getJsonSchema(): array
{
return ['type' => 'object', 'properties' => [self::KEY_MESSAGE => Message::getJsonSchema(), self::KEY_FINISH_REASON => ['type' => 'string', 'enum' => FinishReasonEnum::getValues(), 'description' => 'The reason generation stopped.']], 'required' => [self::KEY_MESSAGE, self::KEY_FINISH_REASON]];
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*
* @return CandidateArrayShape
*/
public function toArray(): array
{
return [self::KEY_MESSAGE => $this->message->toArray(), self::KEY_FINISH_REASON => $this->finishReason->value];
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*/
public static function fromArray(array $array): self
{
static::validateFromArrayData($array, [self::KEY_MESSAGE, self::KEY_FINISH_REASON]);
$messageData = $array[self::KEY_MESSAGE];
return new self(Message::fromArray($messageData), FinishReasonEnum::from($array[self::KEY_FINISH_REASON]));
}
/**
* Performs a deep clone of the candidate.
*
* This method ensures that the message object is cloned to prevent
* modifications to the cloned candidate from affecting the original.
*
* @since 0.4.2
*/
public function __clone()
{
$this->message = clone $this->message;
}
}