Skip to content

Commit ebbc0e6

Browse files
committed
Add chunking strategy on VectorStoreFileResponse
1 parent 728a944 commit ebbc0e6

File tree

8 files changed

+136
-9
lines changed

8 files changed

+136
-9
lines changed

src/Resources/VectorStoresFileBatches.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function listFiles(string $vectorStoreId, string $fileBatchId): VectorSto
5555
{
5656
$payload = Payload::list("vector_stores/$vectorStoreId/file_batches/$fileBatchId/files");
5757

58-
/** @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 */
58+
/** @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 */
5959
$response = $this->transporter->requestObject($payload);
6060

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

src/Resources/VectorStoresFiles.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function create(string $vectorStoreId, array $parameters): VectorStoreFil
2626
{
2727
$payload = Payload::create("vector_stores/$vectorStoreId/files", $parameters);
2828

29-
/** @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 */
29+
/** @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 */
3030
$response = $this->transporter->requestObject($payload);
3131

3232
return VectorStoreFileResponse::from($response->data(), $response->meta());
@@ -41,7 +41,7 @@ public function list(string $vectorStoreId): VectorStoreFileListResponse
4141
{
4242
$payload = Payload::list("vector_stores/$vectorStoreId/files");
4343

44-
/** @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 */
44+
/** @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 */
4545
$response = $this->transporter->requestObject($payload);
4646

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

59-
/** @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 */
59+
/** @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 */
6060
$response = $this->transporter->requestObject($payload);
6161

6262
return VectorStoreFileResponse::from($response->data(), $response->meta());

src/Responses/VectorStores/Files/VectorStoreFileListResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private function __construct(
4040
/**
4141
* Acts as static factory, and returns a new Response instance.
4242
*
43-
* @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
43+
* @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
4444
*/
4545
public static function from(array $attributes, MetaInformation $meta): self
4646
{

src/Responses/VectorStores/Files/VectorStoreFileResponse.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @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}}>
15+
* @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'}}>
1616
*/
1717
final class VectorStoreFileResponse implements ResponseContract, ResponseHasMetaInformationContract
1818
{
1919
/**
20-
* @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}}>
20+
* @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'}}>
2121
*/
2222
use ArrayAccessible;
2323

@@ -32,14 +32,15 @@ private function __construct(
3232
public readonly string $vectorStoreId,
3333
public readonly string $status,
3434
public readonly ?VectorStoreFileResponseLastError $lastError,
35+
public readonly VectorStoreFileResponseChunkingStrategyStatic|VectorStoreFileResponseChunkingStrategyOther $chunkingStrategy,
3536
private readonly MetaInformation $meta,
3637
) {
3738
}
3839

3940
/**
4041
* Acts as static factory, and returns a new Response instance.
4142
*
42-
* @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
43+
* @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
4344
*/
4445
public static function from(array $attributes, MetaInformation $meta): self
4546
{
@@ -51,6 +52,7 @@ public static function from(array $attributes, MetaInformation $meta): self
5152
$attributes['vector_store_id'],
5253
$attributes['status'],
5354
isset($attributes['last_error']) ? VectorStoreFileResponseLastError::from($attributes['last_error']) : null,
55+
$attributes['chunking_strategy']['type'] === 'static' ? VectorStoreFileResponseChunkingStrategyStatic::from($attributes['chunking_strategy']) : VectorStoreFileResponseChunkingStrategyOther::from($attributes['chunking_strategy']),
5456
$meta,
5557
);
5658
}
@@ -68,6 +70,7 @@ public function toArray(): array
6870
'vector_store_id' => $this->vectorStoreId,
6971
'status' => $this->status,
7072
'last_error' => $this->lastError instanceof VectorStoreFileResponseLastError ? $this->lastError->toArray() : null,
73+
'chunking_strategy' => $this->chunkingStrategy->toArray(),
7174
];
7275
}
7376
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\VectorStores\Files;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Responses\Concerns\ArrayAccessible;
9+
use OpenAI\Testing\Responses\Concerns\Fakeable;
10+
11+
/**
12+
* @implements ResponseContract<array{type: 'other'}>
13+
*/
14+
final class VectorStoreFileResponseChunkingStrategyOther implements ResponseContract
15+
{
16+
/**
17+
* @use ArrayAccessible<array{type: 'other'}>
18+
*/
19+
use ArrayAccessible;
20+
21+
use Fakeable;
22+
23+
/**
24+
* @param 'other' $type
25+
*/
26+
private function __construct(
27+
public readonly string $type,
28+
) {
29+
}
30+
31+
/**
32+
* Acts as static factory, and returns a new Response instance.
33+
*
34+
* @param array{type: 'other'} $attributes
35+
*/
36+
public static function from(array $attributes): self
37+
{
38+
return new self(
39+
$attributes['type'],
40+
);
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function toArray(): array
47+
{
48+
return [
49+
'type' => $this->type,
50+
];
51+
}
52+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\VectorStores\Files;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Responses\Concerns\ArrayAccessible;
9+
use OpenAI\Testing\Responses\Concerns\Fakeable;
10+
11+
/**
12+
* @implements ResponseContract<array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}>
13+
*/
14+
final class VectorStoreFileResponseChunkingStrategyStatic implements ResponseContract
15+
{
16+
/**
17+
* @use ArrayAccessible<array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}}>
18+
*/
19+
use ArrayAccessible;
20+
21+
use Fakeable;
22+
23+
/**
24+
* @param 'static' $type
25+
*/
26+
private function __construct(
27+
public readonly string $type,
28+
public readonly int $maxChunkSizeTokens,
29+
public readonly int $chunkOverlapTokens,
30+
) {
31+
}
32+
33+
/**
34+
* Acts as static factory, and returns a new Response instance.
35+
*
36+
* @param array{type: 'static', static: array{max_chunk_size_tokens: int, chunk_overlap_tokens: int}} $attributes
37+
*/
38+
public static function from(array $attributes): self
39+
{
40+
return new self(
41+
$attributes['type'],
42+
$attributes['static']['max_chunk_size_tokens'],
43+
$attributes['static']['chunk_overlap_tokens'],
44+
);
45+
}
46+
47+
/**
48+
* {@inheritDoc}
49+
*/
50+
public function toArray(): array
51+
{
52+
return [
53+
'type' => $this->type,
54+
'static' => [
55+
'max_chunk_size_tokens' => $this->maxChunkSizeTokens,
56+
'chunk_overlap_tokens' => $this->chunkOverlapTokens,
57+
],
58+
];
59+
}
60+
}

tests/Fixtures/VectorStoreFile.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ function vectorStoreFileResource(): array
1313
'vector_store_id' => 'vs_xds05V7ep0QMGI5JmYnWsJwb',
1414
'status' => 'completed',
1515
'last_error' => null,
16+
'chunking_strategy' => [
17+
'type' => 'static',
18+
'static' => [
19+
'max_chunk_size_tokens' => 800,
20+
'chunk_overlap_tokens' => 400,
21+
],
22+
],
1623
];
1724
}
1825

tests/Responses/VectorStores/Files/VectorStoreFileResponse.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use OpenAI\Responses\VectorStores\Files\VectorStoreFileResponse;
4+
use OpenAI\Responses\VectorStores\Files\VectorStoreFileResponseChunkingStrategyStatic;
45

56
test('from', function () {
67
$result = VectorStoreFileResponse::from(vectorStoreFileResource(), meta());
@@ -12,7 +13,11 @@
1213
->createdAt->toBe(1715956697)
1314
->vectorStoreId->toBe('vs_xds05V7ep0QMGI5JmYnWsJwb')
1415
->status->toBe('completed')
15-
->lastError->toBeNull();
16+
->lastError->toBeNull()
17+
->chunkingStrategy->toBeInstanceOf(VectorStoreFileResponseChunkingStrategyStatic::class)
18+
->chunkingStrategy->type->toBe('static')
19+
->chunkingStrategy->maxChunkSizeTokens->toBe(800)
20+
->chunkingStrategy->chunkOverlapTokens->toBe(400);
1621
});
1722

1823
test('as array accessible', function () {

0 commit comments

Comments
 (0)