Skip to content

Commit 840999d

Browse files
committed
[Platform][OpenAI] Enhance token usage handling and streamline streaming results integration
1 parent f9da9c0 commit 840999d

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
use Symfony\AI\Platform\Exception\ContentFilterException;
1818
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1919
use Symfony\AI\Platform\Exception\RuntimeException;
20+
use Symfony\AI\Platform\Metadata\TokenUsage;
2021
use Symfony\AI\Platform\Model;
2122
use Symfony\AI\Platform\Result\ChoiceResult;
2223
use Symfony\AI\Platform\Result\RawHttpResult;
2324
use Symfony\AI\Platform\Result\RawResultInterface;
2425
use Symfony\AI\Platform\Result\ResultInterface;
26+
use Symfony\AI\Platform\Result\StreamChunk;
2527
use Symfony\AI\Platform\Result\StreamResult;
2628
use Symfony\AI\Platform\Result\TextResult;
2729
use Symfony\AI\Platform\Result\ToolCall;
@@ -88,20 +90,49 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
8890
private function convertStream(RawResultInterface|RawHttpResult $result): \Generator
8991
{
9092
$toolCalls = [];
93+
/** @var ToolCallResult|null $toolCallResult */
94+
$toolCallResult = null;
9195
foreach ($result->getDataStream() as $data) {
9296
if ($this->streamIsToolCall($data)) {
9397
$toolCalls = $this->convertStreamToToolCalls($toolCalls, $data);
9498
}
9599

96100
if ([] !== $toolCalls && $this->isToolCallsStreamFinished($data)) {
97-
yield new ToolCallResult(...array_map($this->convertToolCall(...), $toolCalls));
101+
$toolCallResult = new ToolCallResult(...array_map($this->convertToolCall(...), $toolCalls));
102+
// postpone yielding the tool call result until the usage is available
103+
continue;
104+
}
105+
106+
// Usage arrives after the tool calls are finished.
107+
if ($usage = $data['usage'] ?? null) {
108+
if ($toolCallResult) {
109+
$toolCallResult->getMetadata()->add('usage', $usage);
110+
yield $toolCallResult;
111+
$toolCallResult = null;
112+
} else {
113+
yield new TokenUsage(
114+
promptTokens: $usage['prompt_tokens'] ?? null,
115+
completionTokens: $usage['completion_tokens'] ?? null,
116+
thinkingTokens: $usage['completion_tokens_details']['reasoning_tokens'] ?? null,
117+
cachedTokens: $usage['prompt_tokens_details']['cached_tokens'] ?? null,
118+
totalTokens: $usage['total_tokens'] ?? null,
119+
);
120+
}
98121
}
99122

100123
if (!isset($data['choices'][0]['delta']['content'])) {
101124
continue;
102125
}
103126

104-
yield $data['choices'][0]['delta']['content'];
127+
$chunk = new StreamChunk($data['choices'][0]['delta']['content']);
128+
$chunk->setRawResult($result);
129+
130+
yield $chunk;
131+
}
132+
133+
// Yield the last tool call result if any.
134+
if ($toolCallResult) {
135+
yield $toolCallResult;
105136
}
106137
}
107138

0 commit comments

Comments
 (0)