Disable Ollama token extraction for streams

This commit is contained in:
Christopher Hertel
2025-12-23 11:33:11 +01:00
parent cf536a33f3
commit 0c4cfde3de
2 changed files with 3 additions and 28 deletions

View File

@@ -54,25 +54,8 @@ final class TokenUsageExtractorTest extends TestCase
{
$extractor = new TokenUsageExtractor();
$result = new InMemoryRawResult([], [
[
'model' => 'foo',
'response' => 'First chunk',
'done' => false,
],
[
'model' => 'foo',
'response' => 'Hello World!',
'done' => true,
'prompt_eval_count' => 10,
'eval_count' => 10,
],
]);
$tokenUsage = $extractor->extract(new InMemoryRawResult(), ['stream' => true]);
$tokenUsage = $extractor->extract($result, ['stream' => true]);
$this->assertInstanceOf(TokenUsage::class, $tokenUsage);
$this->assertSame(10, $tokenUsage->getPromptTokens());
$this->assertSame(10, $tokenUsage->getCompletionTokens());
$this->assertNull($tokenUsage);
}
}

View File

@@ -24,15 +24,7 @@ final class TokenUsageExtractor implements TokenUsageExtractorInterface
public function extract(RawResultInterface $rawResult, array $options = []): ?TokenUsageInterface
{
if ($options['stream'] ?? false) {
foreach ($rawResult->getDataStream() as $chunk) {
if ($chunk['done']) {
return new TokenUsage(
$chunk['prompt_eval_count'],
$chunk['eval_count']
);
}
}
// Streams have to be handled manually as the tokens are part of the streamed chunks
return null;
}