feat(ollama): add generateWithUsage() and export Usage/GenerateResult types#9
Closed
pyramation wants to merge 3 commits into
Closed
feat(ollama): add generateWithUsage() and export Usage/GenerateResult types#9pyramation wants to merge 3 commits into
pyramation wants to merge 3 commits into
Conversation
… types Adds generateWithUsage() method to OllamaClient that returns token usage metadata (prompt_tokens, completion_tokens, total_tokens) alongside content. The existing generate() method delegates to generateWithUsage() internally, maintaining full backward compatibility. Also exports the Usage and GenerateResult interfaces so consumers can type their metering/billing integrations.
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Three smoke tests covering: - Batch mode: content + non-zero usage (input, output, totalTokens) - Streaming mode: chunks received + usage returned after completion - Multi-turn chat: token counts for conversation context
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
generateWithUsage()toOllamaClientthat returns token usage metadata alongside content. The Ollama API returnsprompt_eval_countandeval_countin responses, which theOllamaAdapteralready captures internally — but the legacygenerate()API discarded them and returned only the text string.Changes:
generateWithUsage(input, onChunk?)method returnsGenerateResult { content, usage, model, stopReason }generate()now delegates togenerateWithUsage()internally — fully backward compatibleUsageandGenerateResultinterfaces for consumer type safetydone: truechunk) and non-streaming modesMotivation: graphile-llm needs actual provider token counts for billing metering (check_billing_quota → LLM call → record_usage with real token counts). Previously had to estimate from text length (~4 chars/token).
Review & Testing Checklist for Human
generate()still returnsstring(non-streaming) andvoid(streaming) — backward compatgenerateWithUsage()with a local Ollama instance: confirmusage.inputandusage.outputare populated from the provider responseonChunkcallback still delivers text deltas AND returns usage in theGenerateResultNotes
OllamaAdapter.stream()was already capturingprompt_eval_count/eval_countfrom thedone: truechunk (lines 464-466). This change just surfaces that data through the public API.Usagewas changed frominterfacetoexport interface.GenerateResult.stopReasonincludes'toolUse'to match the fullAssistantMessagestopReason union.Link to Devin session: https://app.devin.ai/sessions/2b5a29d83d3f478e8d3d972653b4879c
Requested by: @pyramation