Skip to content

Commit 1e92933

Browse files
committed
Fix for Ollama
1 parent 61a251c commit 1e92933

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/api/providers/ollama.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class OllamaHandler extends BaseProvider implements SingleCompletionHandl
3737
messages: openAiMessages,
3838
temperature: this.options.modelTemperature ?? 0,
3939
stream: true,
40+
stream_options: { include_usage: true },
4041
})
4142
const matcher = new XmlMatcher(
4243
"think",
@@ -46,18 +47,30 @@ export class OllamaHandler extends BaseProvider implements SingleCompletionHandl
4647
text: chunk.data,
4748
}) as const,
4849
)
50+
let lastUsage: any | undefined
4951
for await (const chunk of stream) {
5052
const delta = chunk.choices[0]?.delta
5153

5254
if (delta?.content) {
53-
for (const chunk of matcher.update(delta.content)) {
54-
yield chunk
55+
for (const matcherChunk of matcher.update(delta.content)) {
56+
yield matcherChunk
5557
}
5658
}
59+
if (chunk.usage) {
60+
lastUsage = chunk.usage
61+
}
5762
}
5863
for (const chunk of matcher.final()) {
5964
yield chunk
6065
}
66+
67+
if (lastUsage) {
68+
yield {
69+
type: "usage",
70+
inputTokens: lastUsage.prompt_tokens || 0,
71+
outputTokens: lastUsage.completion_tokens || 0,
72+
}
73+
}
6174
}
6275

6376
override getModel(): { id: string; info: ModelInfo } {

0 commit comments

Comments
 (0)