-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Threads Messages Files endpoints added
- Loading branch information
1 parent
2ad27c4
commit b36c5a4
Showing
6 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace OpenAI\Contracts\Resources; | ||
|
||
use OpenAI\Responses\Threads\Messages\Files\ThreadMessageFileListResponse; | ||
use OpenAI\Responses\Threads\Messages\Files\ThreadMessageFileResponse; | ||
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse; | ||
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse; | ||
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse; | ||
|
||
interface ThreadsMessagesFilesContract | ||
{ | ||
/** | ||
* Retrieves a message file. | ||
* | ||
* @see https://platform.openai.com/docs/api-reference/messages/getMessageFile | ||
*/ | ||
public function retrieve(string $threadId, string $messageId, string $fileId): ThreadMessageFileResponse; | ||
|
||
/** | ||
* Returns a list of message files. | ||
* | ||
* @see https://platform.openai.com/docs/api-reference/messages/listMessageFiles | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public function list(string $threadId, string $messageId, array $parameters = []): ThreadMessageFileListResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Resources; | ||
|
||
use OpenAI\Contracts\Resources\ListAssistantsResponse; | ||
use OpenAI\Contracts\Resources\ThreadsMessagesContract; | ||
use OpenAI\Contracts\Resources\ThreadsMessagesFilesContract; | ||
use OpenAI\Responses\Threads\Messages\Files\ThreadMessageFileListResponse; | ||
use OpenAI\Responses\Threads\Messages\Files\ThreadMessageFileResponse; | ||
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse; | ||
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse; | ||
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse; | ||
use OpenAI\Responses\Threads\ThreadDeleteResponse; | ||
use OpenAI\ValueObjects\Transporter\Payload; | ||
use OpenAI\ValueObjects\Transporter\Response; | ||
|
||
final class ThreadsMessagesFiles implements ThreadsMessagesFilesContract | ||
{ | ||
use Concerns\Transportable; | ||
|
||
/** | ||
* Retrieves a message file. | ||
* | ||
* @see https://platform.openai.com/docs/api-reference/messages/getMessageFile | ||
*/ | ||
public function retrieve(string $threadId, string $messageId, string $fileId): ThreadMessageFileResponse | ||
{ | ||
$payload = Payload::retrieve("threads/$threadId/messages/$messageId/files", $fileId); | ||
|
||
/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */ | ||
$response = $this->transporter->requestObject($payload); | ||
|
||
return ThreadMessageFileResponse::from($response->data(), $response->meta()); | ||
} | ||
|
||
/** | ||
* Returns a list of message files. | ||
* | ||
* @see https://platform.openai.com/docs/api-reference/messages/listMessageFiles | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public function list(string $threadId, string $messageId, array $parameters = []): ThreadMessageFileListResponse | ||
{ | ||
$payload = Payload::list("threads/$threadId/messages/$messageId/files", $parameters); | ||
|
||
/** @var Response<array{data: array<int, array{id: string, object: string, created: int, data: array<int, array{url?: string, b64_json?: string}>}>}> $response */ | ||
$response = $this->transporter->requestObject($payload); | ||
|
||
return ThreadMessageFileListResponse::from($response->data(), $response->meta()); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/Responses/Threads/Messages/Files/ThreadMessageFileListResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Responses\Threads\Messages\Files; | ||
|
||
use OpenAI\Contracts\ResponseContract; | ||
use OpenAI\Contracts\ResponseHasMetaInformationContract; | ||
use OpenAI\Responses\Concerns\ArrayAccessible; | ||
use OpenAI\Responses\Concerns\HasMetaInformation; | ||
use OpenAI\Responses\Meta\MetaInformation; | ||
use OpenAI\Testing\Responses\Concerns\Fakeable; | ||
|
||
/** | ||
* @implements ResponseContract<array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int|string}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool}> | ||
*/ | ||
final class ThreadMessageFileListResponse implements ResponseContract, ResponseHasMetaInformationContract | ||
{ | ||
/** | ||
* @use ArrayAccessible<array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int|string}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool}> | ||
*/ | ||
use ArrayAccessible; | ||
|
||
use Fakeable; | ||
use HasMetaInformation; | ||
|
||
/** | ||
* @param array<int, ThreadMessageFileResponse> $data | ||
*/ | ||
private function __construct( | ||
public readonly string $object, | ||
public readonly array $data, | ||
public readonly ?string $firstId, | ||
public readonly ?string $lastId, | ||
public readonly bool $hasMore, | ||
private readonly MetaInformation $meta, | ||
) { | ||
} | ||
|
||
/** | ||
* Acts as static factory, and returns a new Response instance. | ||
* | ||
* @param array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int|string}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool} $attributes | ||
*/ | ||
public static function from(array $attributes, MetaInformation $meta): self | ||
{ | ||
$data = array_map(fn (array $result): ThreadMessageFileResponse => ThreadMessageFileResponse::from( | ||
$result, | ||
$meta, | ||
), $attributes['data']); | ||
|
||
return new self( | ||
$attributes['object'], | ||
$data, | ||
$attributes['first_id'], | ||
$attributes['last_id'], | ||
$attributes['has_more'], | ||
$meta, | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'object' => $this->object, | ||
'data' => array_map( | ||
static fn (ThreadMessageFileResponse $response): array => $response->toArray(), | ||
$this->data, | ||
), | ||
'first_id' => $this->firstId, | ||
'last_id' => $this->lastId, | ||
'has_more' => $this->hasMore, | ||
]; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Responses/Threads/Messages/Files/ThreadMessageFileResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Responses\Threads\Messages\Files; | ||
|
||
use OpenAI\Contracts\ResponseContract; | ||
use OpenAI\Contracts\ResponseHasMetaInformationContract; | ||
use OpenAI\Responses\Concerns\ArrayAccessible; | ||
use OpenAI\Responses\Concerns\HasMetaInformation; | ||
use OpenAI\Responses\Meta\MetaInformation; | ||
use OpenAI\Testing\Responses\Concerns\Fakeable; | ||
|
||
/** | ||
* @implements ResponseContract<array{task: ?string, language: ?string, duration: ?float, segments: array<int, array{id: int, seek: int, start: float, end: float, text: string, tokens: array<int, int>, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}> | ||
*/ | ||
final class ThreadMessageFileResponse implements ResponseContract, ResponseHasMetaInformationContract | ||
{ | ||
/** | ||
* @use ArrayAccessible<array{task: ?string, language: ?string, duration: ?float, segments: array<int, array{id: int, seek: int, start: float, end: float, text: string, tokens: array<int, int>, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}> | ||
*/ | ||
use ArrayAccessible; | ||
|
||
use Fakeable; | ||
use HasMetaInformation; | ||
|
||
/** | ||
* @param array<int, ThreadMessageResponseContentTextObject|ThreadMessageResponseContentImageFileObject> $content | ||
*/ | ||
private function __construct( | ||
public string $id, | ||
public string $object, | ||
public int $createdAt, | ||
public string $messageId, | ||
private readonly MetaInformation $meta, | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* Acts as static factory, and returns a new Response instance. | ||
* | ||
* @param array{task: ?string, language: ?string, duration: ?float, segments: array<int, array{id: int, seek: int, start: float, end: float, text: string, tokens: array<int, int>, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}|string $attributes | ||
*/ | ||
public static function from(array|string $attributes, MetaInformation $meta): self | ||
{ | ||
return new self( | ||
$attributes['id'], | ||
$attributes['object'], | ||
$attributes['created_at'], | ||
$attributes['message_id'], | ||
$meta, | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'object' => $this->object, | ||
'created_at' => $this->createdAt, | ||
'message_id' => $this->messageId, | ||
]; | ||
} | ||
} |