Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chunking strategy on VectorStoreFileResponse #419

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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/VectorStoresFileBatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function listFiles(string $vectorStoreId, string $fileBatchId): VectorSto
{
$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 */
/** @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}, chunking_strategy: array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}|array{type: 'other'}}>, first_id: ?string, last_id: ?string, has_more: bool}> $response */
$response = $this->transporter->requestObject($payload);

return VectorStoreFileListResponse::from($response->data(), $response->meta());
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/VectorStoresFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function create(string $vectorStoreId, array $parameters): VectorStoreFil
{
$payload = Payload::create("vector_stores/$vectorStoreId/files", $parameters);

/** @var Response<array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}}> $response */
/** @var Response<array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}, chunking_strategy: array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}|array{type: 'other'}}> $response */
$response = $this->transporter->requestObject($payload);

return VectorStoreFileResponse::from($response->data(), $response->meta());
Expand All @@ -41,7 +41,7 @@ public function list(string $vectorStoreId): VectorStoreFileListResponse
{
$payload = Payload::list("vector_stores/$vectorStoreId/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 */
/** @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}, chunking_strategy: array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}|array{type: 'other'}}>, first_id: ?string, last_id: ?string, has_more: bool}> $response */
$response = $this->transporter->requestObject($payload);

return VectorStoreFileListResponse::from($response->data(), $response->meta());
Expand All @@ -56,7 +56,7 @@ public function retrieve(string $vectorStoreId, string $fileId): VectorStoreFile
{
$payload = Payload::retrieve("vector_stores/$vectorStoreId/files", $fileId);

/** @var Response<array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}}> $response */
/** @var Response<array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}, chunking_strategy: array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}|array{type: 'other'}}> $response */
$response = $this->transporter->requestObject($payload);

return VectorStoreFileResponse::from($response->data(), $response->meta());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private function __construct(
/**
* Acts as static factory, and returns a new Response instance.
*
* @param 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} $attributes
* @param 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}, chunking_strategy: array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}|array{type: 'other'}}>, first_id: ?string, last_id: ?string, has_more: bool} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
Expand Down
9 changes: 6 additions & 3 deletions src/Responses/VectorStores/Files/VectorStoreFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @implements ResponseContract<array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}}>
* @implements ResponseContract<array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}, chunking_strategy: array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}|array{type: 'other'}}>
*/
final class VectorStoreFileResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}}>
* @use ArrayAccessible<array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}, chunking_strategy: array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}|array{type: 'other'}}>
*/
use ArrayAccessible;

Expand All @@ -32,14 +32,15 @@ private function __construct(
public readonly string $vectorStoreId,
public readonly string $status,
public readonly ?VectorStoreFileResponseLastError $lastError,
public readonly VectorStoreFileResponseChunkingStrategyStatic|VectorStoreFileResponseChunkingStrategyOther $chunkingStrategy,
private readonly MetaInformation $meta,
) {
}

/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}} $attributes
* @param array{id: string, object: string, usage_bytes: int, created_at: int, vector_store_id: string, status: string, last_error: ?array{code: string, message: string}, chunking_strategy: array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}|array{type: 'other'}} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
Expand All @@ -51,6 +52,7 @@ public static function from(array $attributes, MetaInformation $meta): self
$attributes['vector_store_id'],
$attributes['status'],
isset($attributes['last_error']) ? VectorStoreFileResponseLastError::from($attributes['last_error']) : null,
$attributes['chunking_strategy']['type'] === 'static' ? VectorStoreFileResponseChunkingStrategyStatic::from($attributes['chunking_strategy']) : VectorStoreFileResponseChunkingStrategyOther::from($attributes['chunking_strategy']),
$meta,
);
}
Expand All @@ -68,6 +70,7 @@ public function toArray(): array
'vector_store_id' => $this->vectorStoreId,
'status' => $this->status,
'last_error' => $this->lastError instanceof VectorStoreFileResponseLastError ? $this->lastError->toArray() : null,
'chunking_strategy' => $this->chunkingStrategy->toArray(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\VectorStores\Files;

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

/**
* @implements ResponseContract<array{type: 'other'}>
*/
final class VectorStoreFileResponseChunkingStrategyOther implements ResponseContract
{
/**
* @use ArrayAccessible<array{type: 'other'}>
*/
use ArrayAccessible;

use Fakeable;

/**
* @param 'other' $type
*/
private function __construct(
public readonly string $type,
) {
}

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

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

declare(strict_types=1);

namespace OpenAI\Responses\VectorStores\Files;

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

/**
* @implements ResponseContract<array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}>
*/
final class VectorStoreFileResponseChunkingStrategyStatic implements ResponseContract
{
/**
* @use ArrayAccessible<array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}>
*/
use ArrayAccessible;

use Fakeable;

/**
* @param 'static' $type
*/
private function __construct(
public readonly string $type,
public readonly int $maxChunkSizeTokens,
public readonly int $chunkOverlapTokens,
) {
}

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

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'type' => $this->type,
'static' => [
'max_chunk_size_tokens' => $this->maxChunkSizeTokens,
'chunk_overlap_tokens' => $this->chunkOverlapTokens,
],
];
}
}
7 changes: 7 additions & 0 deletions tests/Fixtures/VectorStoreFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ function vectorStoreFileResource(): array
'vector_store_id' => 'vs_xds05V7ep0QMGI5JmYnWsJwb',
'status' => 'completed',
'last_error' => null,
'chunking_strategy' => [
'type' => 'static',
'static' => [
'max_chunk_size_tokens' => 800,
'chunk_overlap_tokens' => 400,
],
],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use OpenAI\Responses\VectorStores\Files\VectorStoreFileResponse;
use OpenAI\Responses\VectorStores\Files\VectorStoreFileResponseChunkingStrategyStatic;

test('from', function () {
$result = VectorStoreFileResponse::from(vectorStoreFileResource(), meta());
Expand All @@ -12,7 +13,11 @@
->createdAt->toBe(1715956697)
->vectorStoreId->toBe('vs_xds05V7ep0QMGI5JmYnWsJwb')
->status->toBe('completed')
->lastError->toBeNull();
->lastError->toBeNull()
->chunkingStrategy->toBeInstanceOf(VectorStoreFileResponseChunkingStrategyStatic::class)
->chunkingStrategy->type->toBe('static')
->chunkingStrategy->maxChunkSizeTokens->toBe(800)
->chunkingStrategy->chunkOverlapTokens->toBe(400);
});

test('as array accessible', function () {
Expand Down
Loading