#[non_exhaustive]pub struct Completion {
pub text: String,
pub json: Option<Value>,
pub model: String,
pub input_tokens: u64,
pub output_tokens: u64,
pub cached_input_tokens: Option<u64>,
}Expand description
A completed completion: the text, the model that answered (a provider identifier, for the log line), and the token usage.
The two token counts have a fixed relationship: Completion::input_tokens
is the total prompt tokens the provider processed, and
Completion::cached_input_tokens — where the provider reports one —
is the subset of that total served from the provider’s own prompt
cache. A caller costing a completion never subtracts; the total already
includes the cached part.
#[non_exhaustive] for the same reason Prompt is: what a provider
reports back grows, and it should not break every caller.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.text: String§json: Option<Value>The parsed answer, when the prompt carried a
Prompt::json_schema. None when it did not, or the provider’s
answer could not be parsed — in which case text still holds what
came back.
model: String§input_tokens: u64The total prompt tokens the provider processed, cached tokens
included — a provider that reports caching reports them alongside
the uncached remainder, not inside it. The discount question is
Completion::cached_input_tokens, never a smaller total here.
output_tokens: u64§cached_input_tokens: Option<u64>The subset of Completion::input_tokens the provider served from
its own prompt cache, where the provider reports one. None when it
does not: an absent report and a reported zero mean different things
(a vendor that does not do caching versus a cache miss), so the
option is never collapsed.
Implementations§
Source§impl Completion
impl Completion
Sourcepub fn new(text: impl Into<String>, model: impl Into<String>) -> Self
pub fn new(text: impl Into<String>, model: impl Into<String>) -> Self
A completion with just the text and the model that wrote it; the usage and parsed JSON are builder methods.
Sourcepub fn json(self, json: Value) -> Self
pub fn json(self, json: Value) -> Self
The parsed structured answer, for a prompt that asked for one.
Sourcepub fn usage(self, input_tokens: u64, output_tokens: u64) -> Self
pub fn usage(self, input_tokens: u64, output_tokens: u64) -> Self
The token counts the provider reported.
Sourcepub fn cached_input_tokens(self, cached_input_tokens: u64) -> Self
pub fn cached_input_tokens(self, cached_input_tokens: u64) -> Self
The subset of the input tokens the provider served from its own
prompt cache, where the provider reports one.
Completion::input_tokens stays the total, cached included.