Skip to content

Commit cb49b9f

Browse files
math625fMathias Rasmussen
andauthored
fix(OpenAI): Add usage support to streamed completions (#708)
* Feat: Add usage to streamed completions * run lint --------- Co-authored-by: Mathias Rasmussen <mra@ordbogen.com>
1 parent d9d0e7a commit cb49b9f

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

src/Responses/Completions/CreateStreamedResponse.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private function __construct(
2828
public readonly string $object,
2929
public readonly int $created,
3030
public readonly string $model,
31+
public readonly ?CreateResponseUsage $usage,
3132
public readonly array $choices,
3233
) {}
3334

@@ -47,6 +48,7 @@ public static function from(array $attributes): self
4748
$attributes['object'],
4849
$attributes['created'],
4950
$attributes['model'],
51+
isset($attributes['usage']) ? CreateResponseUsage::from($attributes['usage']) : null,
5052
$choices,
5153
);
5254
}
@@ -65,6 +67,7 @@ public function toArray(): array
6567
static fn (CreateResponseChoice $result): array => $result->toArray(),
6668
$this->choices,
6769
),
70+
'usage' => $this->usage?->toArray(),
6871
];
6972
}
7073
}

tests/Fixtures/Streams/CompletionCreate.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion",
77
data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " wanted", "index": 0, "logprobs": null, "finish_reason": null}], "model": "gpt-3.5-turbo-instruct"}
88
data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " to", "index": 0, "logprobs": null, "finish_reason": null}], "model": "gpt-3.5-turbo-instruct"}
99
data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " check", "index": 0, "logprobs": null, "finish_reason": null}], "model": "gpt-3.5-turbo-instruct"}
10-
data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " in", "index": 0, "logprobs": null, "finish_reason": "length"}], "model": "gpt-3.5-turbo-instruct"}
10+
data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " in", "index": 0, "logprobs": null, "finish_reason": "length"}], "model": "gpt-3.5-turbo-instruct", "usage": {"prompt_tokens": 1, "completion_tokens": 16, "total_tokens": 17}}
1111
data: [DONE]

tests/Resources/Completions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@
9494
->logprobs->toBe(null)
9595
->finishReason->toBeNull();
9696

97+
$iterator = iterator_to_array($result->getIterator());
98+
$last = end($iterator);
99+
expect($last->usage)
100+
->toBeInstanceOf(CreateResponseUsage::class);
101+
97102
expect($result->meta())
98103
->toBeInstanceOf(MetaInformation::class);
99104
});

0 commit comments

Comments
 (0)