Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Resources/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function create(array $parameters): CreateResponse

$payload = Payload::create('chat/completions', $parameters);

/** @var Response<array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: ?string, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, logprobs: ?array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> $response */
/** @var Response<array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: ?string, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, logprobs: ?array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> $response */
$response = $this->transporter->requestObject($payload);

return CreateResponse::from($response->data(), $response->meta());
Expand Down
2 changes: 1 addition & 1 deletion src/Responses/Chat/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function __construct(
/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{id?: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, logprobs: ?array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null}>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int, prompt_tokens_details?:array{cached_tokens:int}, completion_tokens_details?:array{audio_tokens?:int, reasoning_tokens:int, accepted_prediction_tokens:int, rejected_prediction_tokens:int}}} $attributes
* @param array{id?: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, logprobs: ?array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null}>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int, prompt_tokens_details?:array{cached_tokens:int}, completion_tokens_details?:array{audio_tokens?:int, reasoning_tokens:int, accepted_prediction_tokens:int, rejected_prediction_tokens:int}}} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
Expand Down
2 changes: 1 addition & 1 deletion src/Responses/Chat/CreateResponseChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private function __construct(
) {}

/**
* @param array{index: int, message: array{role: string, content: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>} ,logprobs?: ?array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null} $attributes
* @param array{index: int, message: array{role: string, content: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>} ,logprobs?: ?array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null} $attributes
*/
public static function from(array $attributes): self
{
Expand Down
57 changes: 57 additions & 0 deletions src/Responses/Chat/CreateResponseChoiceImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Chat;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @phpstan-type CreateResponseChoiceImageType array{image_url: array{url: string, detail: string}, index: int, type: string}
*
* @implements ResponseContract<CreateResponseChoiceImageType>
*/
final class CreateResponseChoiceImage implements ResponseContract
{
/**
* @use ArrayAccessible<CreateResponseChoiceImageType>
*/
use ArrayAccessible;

use Fakeable;

/**
* @param array{url: string, detail: string} $imageUrl
*/
private function __construct(
public readonly array $imageUrl,
public readonly int $index,
public readonly string $type,
) {}

/**
* @param CreateResponseChoiceImageType $attributes
*/
public static function from(array $attributes): self
{
return new self(
$attributes['image_url'],
$attributes['index'],
$attributes['type'],
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'image_url' => $this->imageUrl,
'index' => $this->index,
'type' => $this->type,
];
}
}
15 changes: 13 additions & 2 deletions src/Responses/Chat/CreateResponseMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class CreateResponseMessage
/**
* @param array<int, CreateResponseToolCall> $toolCalls
* @param array<int, CreateResponseChoiceAnnotations> $annotations
* @param array<int, CreateResponseChoiceImage>|null $images
*/
private function __construct(
public readonly string $role,
Expand All @@ -20,10 +21,11 @@ private function __construct(
public readonly array $toolCalls,
public readonly ?CreateResponseFunctionCall $functionCall,
public readonly ?CreateResponseChoiceAudio $audio = null,
public readonly ?array $images = null,
) {}

/**
* @param array{role: string, content: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>, audio?: CreateResponseChoiceAudioType} $attributes
* @param array{role: string, content: ?string,annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>,function_call?: array{name: string, arguments: string},tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>,audio?: array{id: string, data: string, expires_at: int, transcript: string},images?: array<int, array{image_url: array{url: string, detail: string}, index: int, type: string}>,} $attributes
*/
public static function from(array $attributes): self
{
Expand All @@ -35,18 +37,23 @@ public static function from(array $attributes): self
$result,
), $attributes['annotations'] ?? []);

$images = isset($attributes['images']) ? array_map(fn (array $result): CreateResponseChoiceImage => CreateResponseChoiceImage::from(
$result
), $attributes['images']) : null;

return new self(
$attributes['role'],
$attributes['content'] ?? null,
$annotations,
$toolCalls,
isset($attributes['function_call']) ? CreateResponseFunctionCall::from($attributes['function_call']) : null,
isset($attributes['audio']) ? CreateResponseChoiceAudio::from($attributes['audio']) : null,
$images,
);
}

/**
* @return array{role: string, content: string|null, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>, audio?: CreateResponseChoiceAudioType}
* @return array{role: string, content: string|null, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>, audio?: CreateResponseChoiceAudioType, images?: array<int, array{image_url: array{url: string, detail: string}, index: int, type: string}>}
*/
public function toArray(): array
{
Expand All @@ -71,6 +78,10 @@ public function toArray(): array
$data['audio'] = $this->audio->toArray();
}

if ($this->images !== null && $this->images !== []) {
$data['images'] = array_map(fn (CreateResponseChoiceImage $image): array => $image->toArray(), $this->images);
}

return $data;
}
}