Skip to content

Commit

Permalink
assistants v2: Add vector store file batches
Browse files Browse the repository at this point in the history
  • Loading branch information
knash94 committed May 21, 2024
1 parent 9591c6d commit 3e681a9
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Contracts/Resources/VectorStoresContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace OpenAI\Contracts\Resources;

use OpenAI\Resources\VectorStores;
use OpenAI\Resources\VectorStoresFileBatches;
use OpenAI\Responses\VectorStores\VectorStoreDeleteResponse;
use OpenAI\Responses\VectorStores\VectorStoreListResponse;
use OpenAI\Responses\VectorStores\VectorStoreResponse;
Expand Down Expand Up @@ -53,4 +55,11 @@ public function list(): VectorStoreListResponse;
* @see https://platform.openai.com/docs/api-reference/vector-stores-files
*/
public function files(): VectorStoresFilesContract;

/**
* Manage the file batches related to the vector store
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches
*/
public function batches(): VectorStoresFileBatchesContract;
}
45 changes: 45 additions & 0 deletions src/Contracts/Resources/VectorStoresFileBatchesContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace OpenAI\Contracts\Resources;

use OpenAI\Resources\VectorStoresFileBatches;
use OpenAI\Responses\VectorStores\FileBatches\VectorStoreFileBatchResponse;
use OpenAI\Responses\VectorStores\Files\VectorStoreFileDeleteResponse;
use OpenAI\Responses\VectorStores\Files\VectorStoreFileListResponse;
use OpenAI\Responses\VectorStores\Files\VectorStoreFileResponse;
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

interface VectorStoresFileBatchesContract
{

/**
* Retrieves a file batch within a vector store.
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/getBatch
*/
public function retrieve(string $vectorStoreId, string $fileBatchId): VectorStoreFileBatchResponse;

/**
* Cancel a vector store file batch
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/cancelBatch
*/
public function cancel(string $vectorStoreId, string $fileBatchId): VectorStoreFileBatchResponse;

/**
* Create a file batch on a vector store
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/createBatch
*
* @param array<string, mixed> $parameters
*/
public function create(string $vectorStoreId, array $parameters): VectorStoreFileBatchResponse;

/**
* Lists the files within a file batch within a vector store
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/listBatchFiles
*/
public function listFiles(string $vectorStoreId, string $fileBatchId): VectorStoreFileListResponse;
}
13 changes: 13 additions & 0 deletions src/Resources/VectorStores.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenAI\Resources;

use OpenAI\Contracts\Resources\VectorStoresContract;
use OpenAI\Contracts\Resources\VectorStoresFileBatchesContract;
use OpenAI\Contracts\Resources\VectorStoresFilesContract;
use OpenAI\Responses\VectorStores\VectorStoreDeleteResponse;
use OpenAI\Responses\VectorStores\VectorStoreListResponse;
Expand Down Expand Up @@ -104,4 +105,16 @@ public function files(): VectorStoresFilesContract
{
return new VectorStoresFiles($this->transporter);
}

/**
* Manage the file batches related to the vector store
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches
*/
public function batches(): VectorStoresFileBatchesContract
{
return new VectorStoresFileBatches($this->transporter);
}


}
78 changes: 78 additions & 0 deletions src/Resources/VectorStoresFileBatches.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace OpenAI\Resources;

use OpenAI\Contracts\Resources\VectorStoresFileBatchesContract;
use OpenAI\Responses\VectorStores\FileBatches\VectorStoreFileBatchResponse;
use OpenAI\Responses\VectorStores\Files\VectorStoreFileListResponse;
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class VectorStoresFileBatches implements VectorStoresFileBatchesContract
{
use Concerns\Transportable;

/**
* Create a file batch on a vector store
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/createBatch
*
* @param array<string, mixed> $parameters
*/
public function create(string $vectorStoreId, array $parameters): VectorStoreFileBatchResponse
{
$payload = Payload::create("vector_stores/$vectorStoreId/file_batches", $parameters);

/** @var Response<array{id: string, object: string, created_at: int, vector_store_id: string, status: string, file_counts: array{in_progress: int, completed: int, failed: int, cancelled: int, total: int}}> $response */
$response = $this->transporter->requestObject($payload);

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

/**
* Retrieves a file batch within a vector store.
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/getBatch
*/
public function retrieve(string $vectorStoreId, string $fileBatchId): VectorStoreFileBatchResponse
{
$payload = Payload::retrieve("vector_stores/$vectorStoreId/file_batches", $fileBatchId);

/** @var Response<array{id: string, object: string, created_at: int, vector_store_id: string, status: string, file_counts: array{in_progress: int, completed: int, failed: int, cancelled: int, total: int}}> $response */
$response = $this->transporter->requestObject($payload);

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

/**
* Lists the files within a file batch within a vector store
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/listBatchFiles
*/
public function listFiles(string $vectorStoreId, string $fileBatchId): VectorStoreFileListResponse
{
$payload = Payload::list("vector_stores/$vectorStoreId/file_batches/$fileBatchId/files");

/** @var Response<array{object: string, data: array<int, array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}}>, first_id: ?string, last_id: ?string, has_more: bool}> $response */
$response = $this->transporter->requestObject($payload);

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

/**
* Cancel a vector store file batch
*
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/cancelBatch
*/
public function cancel(string $vectorStoreId, string $fileBatchId): VectorStoreFileBatchResponse
{
$payload = Payload::delete("vector_stores/$vectorStoreId/file_batches", $fileBatchId);

/** @var Response<array{id: string, object: string, created_at: int, vector_store_id: string, status: string, file_counts: array{in_progress: int, completed: int, failed: int, cancelled: int, total: int}}> $response */
$response = $this->transporter->requestObject($payload);

return VectorStoreFileBatchResponse::from($response->data(), $response->meta());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\VectorStores\FileBatches;

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

/**
* @implements ResponseContract<array{in_progress: int, completed: int, failed: int, cancelled: int, total: int}>
*/
final class VectorStoreFileBatchFileCountsResponse implements ResponseContract
{
/**
* @use ArrayAccessible<array{in_progress: int, completed: int, failed: int, cancelled: int, total: int}>
*/
use ArrayAccessible;

use Fakeable;

private function __construct(
public readonly int $inProgress,
public readonly int $completed,
public readonly int $failed,
public readonly int $cancelled,
public readonly int $total,
) {
}

/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{in_progress: int, completed: int, failed: int, cancelled: int, total: int} $attributes
*/
public static function from(array $attributes): self
{
return new self(
$attributes['in_progress'],
$attributes['completed'],
$attributes['cancelled'],
$attributes['failed'],
$attributes['total'],
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'in_progress' => $this->inProgress,
'completed' => $this->completed,
'failed' => $this->failed,
'cancelled' => $this->cancelled,
'total' => $this->total,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\VectorStores\FileBatches;

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\Responses\VectorStores\Files\VectorStoreFileLastErrorResponse;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @implements ResponseContract<array{id: string, object: string, created_at: int, vector_store_id: string, status: string, file_counts: array{in_progress: int, completed: int, failed: int, cancelled: int, total: int}}>
*/
final class VectorStoreFileBatchResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, object: string, created_at: int, vector_store_id: string, status: string, file_counts: array{in_progress: int, completed: int, failed: int, cancelled: int, total: int}}>
*/
use ArrayAccessible;

use Fakeable;
use HasMetaInformation;

private function __construct(
public readonly string $id,
public readonly string $object,
public readonly int $createdAt,
public readonly string $vectorStoreId,
public readonly string $status,
public readonly VectorStoreFileBatchFileCountsResponse $fileCounts,
private readonly MetaInformation $meta,
) {
}

/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{id: string, object: string, created_at: int, vector_store_id: string, status: string, file_counts: array{in_progress: int, completed: int, failed: int, cancelled: int, total: int}} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
return new self(
$attributes['id'],
$attributes['object'],
$attributes['created_at'],
$attributes['vector_store_id'],
$attributes['status'],
VectorStoreFileBatchFileCountsResponse::from($attributes['file_counts']),
$meta,
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'id' => $this->id,
'object' => $this->object,
'created_at' => $this->createdAt,
'vector_store_id' => $this->vectorStoreId,
'status' => $this->status,
'file_counts' => $this->fileCounts->toArray(),
];
}
}
39 changes: 39 additions & 0 deletions src/Testing/Resources/VectorStoresFileBatchesTestResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace OpenAI\Testing\Resources;

use OpenAI\Contracts\Resources\VectorStoresFileBatchesContract;
use OpenAI\Resources\VectorStoresFileBatches;
use OpenAI\Responses\VectorStores\FileBatches\VectorStoreFileBatchResponse;
use OpenAI\Responses\VectorStores\Files\VectorStoreFileListResponse;
use OpenAI\Testing\Resources\Concerns\Testable;

final class VectorStoresFileBatchesTestResource implements VectorStoresFileBatchesContract
{
use Testable;

public function resource(): string
{
return VectorStoresFileBatches::class;
}

public function retrieve(string $vectorStoreId, string $fileBatchId): VectorStoreFileBatchResponse
{
return $this->record(__FUNCTION__, func_get_args());
}

public function cancel(string $vectorStoreId, string $fileBatchId): VectorStoreFileBatchResponse
{
return $this->record(__FUNCTION__, func_get_args());
}

public function create(string $vectorStoreId, array $parameters): VectorStoreFileBatchResponse
{
return $this->record(__FUNCTION__, func_get_args());
}

public function listFiles(string $vectorStoreId, string $fileBatchId): VectorStoreFileListResponse
{
return $this->record(__FUNCTION__, func_get_args());
}
}
9 changes: 8 additions & 1 deletion src/Testing/Resources/VectorStoresTestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace OpenAI\Testing\Resources;

use OpenAI\Contracts\Resources\VectorStoresContract;
use OpenAI\Contracts\Resources\VectorStoresFileBatchesContract;
use OpenAI\Contracts\Resources\VectorStoresFilesContract;
use OpenAI\Resources\VectorStores;
use OpenAI\Resources\VectorStoresFileBatches;
use OpenAI\Resources\VectorStoresFiles;
use OpenAI\Responses\VectorStores\VectorStoreDeleteResponse;
use OpenAI\Responses\VectorStores\VectorStoreListResponse;
Expand Down Expand Up @@ -47,6 +49,11 @@ public function list(): VectorStoreListResponse

public function files(): VectorStoresFilesContract
{
return new VectorStoresFiles($this);
return new VectorStoresFilesTestResource($this);
}

public function batches(): VectorStoresFileBatchesContract
{
return new VectorStoresFileBatchesTestResource($this);
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/VectorStoreFileBatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @return array<string, mixed>
*/
function vectorStoreFileBatchResource(): array
{
return [
'id' => 'vsfb_abc123',
'object' => 'vector_store.file_batch',
'created_at' => 1699061776,
'vector_store_id' => 'vs_abc123',
'status' => 'cancelling',
'file_counts' => [
'in_progress' => 12,
'completed' => 3,
'failed' => 0,
'cancelled' => 0,
'total' => 15,
]
];
}
Loading

0 comments on commit 3e681a9

Please sign in to comment.