Skip to content

Commit dff064e

Browse files
committed
fix mistral usage capture
1 parent e1a80d1 commit dff064e

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/Providers/Mistral/HandleStream.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use function array_reduce;
2727
use function array_unshift;
2828
use function is_array;
29+
use function array_key_exists;
30+
use function array_merge;
2931

3032
trait HandleStream
3133
{
@@ -49,7 +51,7 @@ public function stream(Message ...$messages): Generator
4951
'stream' => true,
5052
'model' => $this->model,
5153
'messages' => $this->messageMapper()->map($messages),
52-
...$this->parameters,
54+
...array_merge($this->parameters, ['stream_options' => ['include_usage' => true]]),
5355
];
5456

5557
// Attach tools
@@ -65,17 +67,17 @@ public function stream(Message ...$messages): Generator
6567
);
6668

6769
$this->streamState = new StreamState();
70+
$lastFinishReason = null;
6871

6972
while (! $stream->eof()) {
7073
if (($line = SSEParser::parseNextSSEEvent($stream)) === null) {
7174
continue;
7275
}
7376

7477
// Capture usage information
75-
if (empty($line['choices']) && !empty($line['usage'])) {
78+
if (!empty($line['usage'])) {
7679
$this->streamState->addInputTokens($line['usage']['prompt_tokens'] ?? 0);
7780
$this->streamState->addOutputTokens($line['usage']['completion_tokens'] ?? 0);
78-
continue;
7981
}
8082

8183
if (empty($line['choices'])) {
@@ -134,11 +136,19 @@ public function stream(Message ...$messages): Generator
134136
if ($chunk !== null) {
135137
yield $chunk;
136138
}
139+
140+
if (array_key_exists('finish_reason', $choice)) {
141+
$lastFinishReason = $choice['finish_reason'];
142+
}
137143
}
138144

139145
$message = new AssistantMessage($this->streamState->getContentBlocks());
140146
$message->setUsage($this->streamState->getUsage());
141147

148+
if ($lastFinishReason !== null) {
149+
$message->setStopReason($lastFinishReason);
150+
}
151+
142152
return $message;
143153
}
144154

0 commit comments

Comments
 (0)