Skip to content

Commit e790121

Browse files
authored
fix(OpenAI): Add model field to CreateResponse for embeddings (#634)
* Add model field to CreateResponse for embeddings * remove comments * Add model field to CreateResponse for embeddings remove comments * add missing 'model' offset to type definition * make model embedding property nullable
1 parent 2f593ce commit e790121

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ $response->object; // 'list'
697697

698698
foreach ($response->embeddings as $embedding) {
699699
$embedding->object; // 'embedding'
700+
$embedding->model; // 'text-similarity-babbage-001'
700701
$embedding->embedding; // [0.018990106880664825, -0.0073809814639389515, ...]
701702
$embedding->index; // 0
702703
}

src/Resources/Embeddings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function create(array $parameters): CreateResponse
2424
{
2525
$payload = Payload::create('embeddings', $parameters);
2626

27-
/** @var Response<array{object: string, data: array<int, array{object: string, embedding: array<int, float>, index: int}>, usage: array{prompt_tokens: int, total_tokens: int}}> $response */
27+
/** @var Response<array{object: string, model: string, data: array<int, array{object: string, embedding: array<int, float>, index: int}>, usage: array{prompt_tokens: int, total_tokens: int}}> $response */
2828
$response = $this->transporter->requestObject($payload);
2929

3030
return CreateResponse::from($response->data(), $response->meta());

src/Responses/Embeddings/CreateResponse.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{object: string, data: array<int, array{object: string, embedding: array<int, float>, index?: int}>, usage?: array{prompt_tokens: int, total_tokens: int}}>
15+
* @implements ResponseContract<array{object: string, model?: string|null, data: array<int, array{object: string, embedding: array<int, float>, index?: int}>, usage?: array{prompt_tokens: int, total_tokens: int}}>
1616
*/
1717
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
1818
{
1919
/**
20-
* @use ArrayAccessible<array{object: string, data: array<int, array{object: string, embedding: array<int, float>, index?: int}>, usage?: array{prompt_tokens: int, total_tokens: int}}>
20+
* @use ArrayAccessible<array{object: string, model?: string|null, data: array<int, array{object: string, embedding: array<int, float>, index?: int}>, usage?: array{prompt_tokens: int, total_tokens: int}}>
2121
*/
2222
use ArrayAccessible;
2323

@@ -29,6 +29,7 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati
2929
*/
3030
private function __construct(
3131
public readonly string $object,
32+
public readonly ?string $model,
3233
public readonly array $embeddings,
3334
public readonly ?CreateResponseUsage $usage,
3435
private readonly MetaInformation $meta,
@@ -37,7 +38,7 @@ private function __construct(
3738
/**
3839
* Acts as static factory, and returns a new Response instance.
3940
*
40-
* @param array{object: string, data: array<int, array{object: string, embedding: array<int, float>, index?: int}>, usage?: array{prompt_tokens: int, total_tokens: int}} $attributes
41+
* @param array{object: string, model?: string|null, data: array<int, array{object: string, embedding: array<int, float>, index?: int}>, usage?: array{prompt_tokens: int, total_tokens: int}} $attributes
4142
*/
4243
public static function from(array $attributes, MetaInformation $meta): self
4344
{
@@ -47,6 +48,7 @@ public static function from(array $attributes, MetaInformation $meta): self
4748

4849
return new self(
4950
$attributes['object'],
51+
$attributes['model'] ?? null,
5052
$embeddings,
5153
isset($attributes['usage']) ? CreateResponseUsage::from($attributes['usage']) : null,
5254
$meta,
@@ -60,6 +62,7 @@ public function toArray(): array
6062
{
6163
return array_filter([
6264
'object' => $this->object,
65+
'model' => $this->model ?? null,
6366
'data' => array_map(
6467
static fn (CreateResponseEmbedding $result): array => $result->toArray(),
6568
$this->embeddings,

src/Testing/Responses/Fixtures/Embeddings/CreateResponseFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ final class CreateResponseFixture
66
{
77
public const ATTRIBUTES = [
88
'object' => 'list',
9+
'model' => 'text-embedding-3-small',
910
'data' => [
1011
[
1112
'object' => 'embedding',

tests/Fixtures/Embedding.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function embeddingList(): array
3838
{
3939
return [
4040
'object' => 'list',
41+
'model' => 'text-embedding-3-small',
4142
'data' => [
4243
embedding(),
4344
embedding(),
@@ -56,9 +57,28 @@ function embeddingListWithoutUsage(): array
5657
{
5758
return [
5859
'object' => 'list',
60+
'model' => 'text-embedding-3-small',
5961
'data' => [
6062
embedding(),
6163
embedding(),
6264
],
6365
];
6466
}
67+
68+
/**
69+
* @return array<string, mixed>
70+
*/
71+
function embeddingListWithoutModel(): array
72+
{
73+
return [
74+
'object' => 'list',
75+
'data' => [
76+
embedding(),
77+
embedding(),
78+
],
79+
'usage' => [
80+
'prompt_tokens' => 8,
81+
'total_tokens' => 8,
82+
],
83+
];
84+
}

tests/Responses/Embeddings/CreateResponse.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
expect($response)
1212
->toBeInstanceOf(CreateResponse::class)
1313
->object->toBe('list')
14+
->model->toBe('text-embedding-3-small')
1415
->embeddings->toBeArray()->toHaveCount(2)
1516
->embeddings->each->toBeInstanceOf(CreateResponseEmbedding::class)
1617
->usage->toBeInstanceOf(CreateResponseUsage::class)
@@ -23,12 +24,26 @@
2324
expect($response)
2425
->toBeInstanceOf(CreateResponse::class)
2526
->object->toBe('list')
27+
->model->toBe('text-embedding-3-small')
2628
->embeddings->toBeArray()->toHaveCount(2)
2729
->embeddings->each->toBeInstanceOf(CreateResponseEmbedding::class)
2830
->usage->toBeNull()
2931
->meta()->toBeInstanceOf(MetaInformation::class);
3032
});
3133

34+
test('from without model', function () {
35+
$response = CreateResponse::from(embeddingListWithoutModel(), meta());
36+
37+
expect($response)
38+
->toBeInstanceOf(CreateResponse::class)
39+
->object->toBe('list')
40+
->model->toBeNull()
41+
->embeddings->toBeArray()->toHaveCount(2)
42+
->embeddings->each->toBeInstanceOf(CreateResponseEmbedding::class)
43+
->usage->toBeInstanceOf(CreateResponseUsage::class)
44+
->meta()->toBeInstanceOf(MetaInformation::class);
45+
});
46+
3247
test('as array accessible', function () {
3348
$response = CreateResponse::from(embeddingList(), meta());
3449

@@ -41,6 +56,12 @@
4156
expect($response->toArray())->toBeArray()->toBe(embeddingList());
4257
});
4358

59+
test('to array without model', function () {
60+
$response = CreateResponse::from(embeddingListWithoutModel(), meta());
61+
62+
expect($response->toArray())->toBeArray()->toBe(embeddingListWithoutModel());
63+
});
64+
4465
test('fake', function () {
4566
$response = CreateResponse::fake();
4667

0 commit comments

Comments
 (0)