Skip to content

Commit

Permalink
WIP: Threads Messages Files endpoints added
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 9, 2023
1 parent 2ad27c4 commit b36c5a4
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Contracts/Resources/ThreadsMessagesContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ public function delete(string $threadId, string $messageId): ThreadMessageDelete
* @param array<string, mixed> $parameters
*/
public function list(string $threadId, array $parameters = []): ThreadMessageListResponse;

/**
* Manage files attached to a thred message.
*
* @see https://platform.openai.com/docs/api-reference/messages/file-object
*/
public function files(): ThreadsMessagesFilesContract;
}
28 changes: 28 additions & 0 deletions src/Contracts/Resources/ThreadsMessagesFilesContract.php
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;
}
11 changes: 11 additions & 0 deletions src/Resources/ThreadsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use OpenAI\Contracts\Resources\ListAssistantsResponse;
use OpenAI\Contracts\Resources\ThreadsContract;
use OpenAI\Contracts\Resources\ThreadsMessagesContract;
use OpenAI\Contracts\Resources\ThreadsMessagesFilesContract;
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
Expand Down Expand Up @@ -100,4 +101,14 @@ public function list(string $threadId, array $parameters = []): ThreadMessageLis

return ThreadMessageListResponse::from($response->data(), $response->meta());
}

/**
* Manage files attached to a thred message.
*
* @see https://platform.openai.com/docs/api-reference/messages/file-object
*/
public function files(): ThreadsMessagesFilesContract
{
return new ThreadsMessagesFiles($this->transporter);
}
}
54 changes: 54 additions & 0 deletions src/Resources/ThreadsMessagesFiles.php
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());
}
}
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 src/Responses/Threads/Messages/Files/ThreadMessageFileResponse.php
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,
];
}
}

0 comments on commit b36c5a4

Please sign in to comment.