Skip to content

Commit c574bae

Browse files
committed
bug #1267 [Platform][Ollama][VertexAi] Disable token extraction for streams (chr-hertel)
This PR was merged into the main branch. Discussion ---------- [Platform][Ollama][VertexAi] Disable token extraction for streams | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no <!-- required for new features --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT For now we need to revert this, since it consumes the stream iterator already and output streaming is broken than. Commits ------- 42dd99d Disable Ollama token extraction for streams
2 parents a6a4530 + 42dd99d commit c574bae

File tree

4 files changed

+6
-55
lines changed

4 files changed

+6
-55
lines changed

src/platform/src/Bridge/Ollama/Tests/TokenUsageExtractorTest.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,8 @@ public function testItExtractsTokenUsageFromStreamResult()
5454
{
5555
$extractor = new TokenUsageExtractor();
5656

57-
$result = new InMemoryRawResult([], [
58-
[
59-
'model' => 'foo',
60-
'response' => 'First chunk',
61-
'done' => false,
62-
],
63-
[
64-
'model' => 'foo',
65-
'response' => 'Hello World!',
66-
'done' => true,
67-
'prompt_eval_count' => 10,
68-
'eval_count' => 10,
69-
],
70-
]);
71-
72-
$tokenUsage = $extractor->extract($result, ['stream' => true]);
57+
$tokenUsage = $extractor->extract(new InMemoryRawResult(), ['stream' => true]);
7358

74-
$this->assertInstanceOf(TokenUsage::class, $tokenUsage);
75-
$this->assertSame(10, $tokenUsage->getPromptTokens());
76-
$this->assertSame(10, $tokenUsage->getCompletionTokens());
59+
$this->assertNull($tokenUsage);
7760
}
7861
}

src/platform/src/Bridge/Ollama/TokenUsageExtractor.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ final class TokenUsageExtractor implements TokenUsageExtractorInterface
2424
public function extract(RawResultInterface $rawResult, array $options = []): ?TokenUsageInterface
2525
{
2626
if ($options['stream'] ?? false) {
27-
foreach ($rawResult->getDataStream() as $chunk) {
28-
if ($chunk['done']) {
29-
return new TokenUsage(
30-
$chunk['prompt_eval_count'],
31-
$chunk['eval_count']
32-
);
33-
}
34-
}
35-
27+
// Streams have to be handled manually as the tokens are part of the streamed chunks
3628
return null;
3729
}
3830

src/platform/src/Bridge/VertexAi/Gemini/TokenUsageExtractor.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,7 @@ final class TokenUsageExtractor implements TokenUsageExtractorInterface
2424
public function extract(RawResultInterface $rawResult, array $options = []): ?TokenUsageInterface
2525
{
2626
if ($options['stream'] ?? false) {
27-
$lastChunk = null;
28-
29-
foreach ($rawResult->getDataStream() as $chunk) {
30-
// Store last event that contains usage metadata
31-
if (isset($chunk['usageMetadata'])) {
32-
$lastChunk = $chunk;
33-
}
34-
}
35-
36-
if ($lastChunk) {
37-
return $this->extractUsageMetadata($lastChunk['usageMetadata']);
38-
}
39-
27+
// Streams have to be handled manually as the tokens are part of the streamed chunks
4028
return null;
4129
}
4230

src/platform/src/Bridge/VertexAi/Tests/TokenUsageExtractorTest.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,9 @@ public function testItHandlesMissingUsageFields()
6868
public function testItHandlesStreamResults()
6969
{
7070
$extractor = new TokenUsageExtractor();
71-
$result = new InMemoryRawResult(dataStream: [
72-
['content' => 'chunk1'],
73-
['content' => 'chunk2', 'usageMetadata' => [
74-
'promptTokenCount' => 15,
75-
'candidatesTokenCount' => 25,
76-
'totalTokenCount' => 40,
77-
]],
78-
]);
7971

80-
$tokenUsage = $extractor->extract($result, ['stream' => true]);
72+
$tokenUsage = $extractor->extract(new InMemoryRawResult(), ['stream' => true]);
8173

82-
$this->assertInstanceOf(TokenUsage::class, $tokenUsage);
83-
$this->assertSame(15, $tokenUsage->getPromptTokens());
84-
$this->assertSame(25, $tokenUsage->getCompletionTokens());
85-
$this->assertNull($tokenUsage->getThinkingTokens());
86-
$this->assertSame(40, $tokenUsage->getTotalTokens());
74+
$this->assertNull($tokenUsage);
8775
}
8876
}

0 commit comments

Comments
 (0)