Report observed PromptKit invocation usage
This commit is contained in:
83
integrations/ckprompt/usage_report.go
Normal file
83
integrations/ckprompt/usage_report.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package ckprompt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
commandkit "go.maximumdirect.net/commandkit"
|
||||
promptkit "go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
type usageInput struct{ ShowTokens bool }
|
||||
|
||||
type usageSummary struct {
|
||||
Prompts int
|
||||
RepairCalls uint64
|
||||
CoverageComplete bool
|
||||
Usage promptkit.ObservedTokenUsage
|
||||
Cost promptkit.ObservedCost
|
||||
CacheHitRatio *float64
|
||||
Smoke bool
|
||||
}
|
||||
|
||||
func usageFeature() commandkit.InvocationFeature {
|
||||
return commandkit.NewInvocationFeature(commandkit.InvocationFeatureSpec[usageInput]{
|
||||
Flags: []commandkit.Flag[usageInput]{commandkit.BoolFlag(commandkit.FlagMeta{Name: "show-tokens", Usage: "Show token usage and available cost information after execution."}, false, func(i *usageInput, v commandkit.Supplied[bool]) { i.ShowTokens = v.Value })},
|
||||
Start: func(inv commandkit.Invocation, input usageInput) (context.Context, func() commandkit.Report) {
|
||||
if !input.ShowTokens {
|
||||
return inv.Context, nil
|
||||
}
|
||||
group := new(promptkit.ExecutionGroup)
|
||||
ctx := promptkit.WithExecutionObserver(inv.Context, group.Observe)
|
||||
smoke := inv.CanonicalPath() == "promptkit test"
|
||||
return ctx, func() commandkit.Report { return usageReport(group.Snapshot(), smoke) }
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func usageReport(summary promptkit.ExecutionGroupSummary, smoke bool) commandkit.Report {
|
||||
value := usageSummary{Prompts: len(summary.Members), RepairCalls: summary.RepairCalls, CoverageComplete: summary.TerminalCoverageComplete, Usage: summary.Usage, Cost: summary.CostUSD, Smoke: smoke}
|
||||
if ratio, ok := summary.Usage.CacheHitRatio(); ok {
|
||||
value.CacheHitRatio = &ratio
|
||||
}
|
||||
return commandkit.Report{Value: value, TextTemplate: usageTextTemplate}
|
||||
}
|
||||
|
||||
func usageCount(count promptkit.ObservedTokenCount) string {
|
||||
if !count.Known {
|
||||
return "unknown"
|
||||
}
|
||||
if !count.Complete {
|
||||
return fmt.Sprintf("%d (incomplete)", count.Value)
|
||||
}
|
||||
return fmt.Sprint(count.Value)
|
||||
}
|
||||
func usageCost(cost promptkit.ObservedCost) string {
|
||||
if !cost.Known {
|
||||
return "unknown"
|
||||
}
|
||||
result := fmt.Sprintf("USD %.6g", cost.Value)
|
||||
if !cost.Complete {
|
||||
result += " (incomplete subtotal)"
|
||||
}
|
||||
if cost.Source != "" {
|
||||
result += fmt.Sprintf(" [%s]", cost.Source)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
var usageTextTemplate = template.Must(commandkit.ParseTextTemplate("observed usage", `Observed PromptKit execution usage
|
||||
Prompts: {{.Prompts}}
|
||||
{{if .RepairCalls}} Repair calls: {{.RepairCalls}}
|
||||
{{end}}Prompt tokens: {{count .Usage.PromptTokens}}
|
||||
Completion tokens: {{count .Usage.CompletionTokens}}
|
||||
Reasoning tokens: {{count .Usage.ReasoningTokens}}
|
||||
Total tokens: {{count .Usage.TotalTokens}}
|
||||
Cached tokens: {{count .Usage.CachedTokens}}
|
||||
Cache-write tokens: {{count .Usage.CacheWriteTokens}}
|
||||
{{with .CacheHitRatio}}Cache hit ratio: {{printf "%.1f" (percent .)}}%
|
||||
{{end}}Cost: {{cost .Cost}}
|
||||
Terminal coverage: {{if .CoverageComplete}}complete{{else}}incomplete{{end}}
|
||||
{{if .Smoke}}Smoke-test usage is reported separately in the command report.
|
||||
{{end}}`, template.FuncMap{"count": usageCount, "cost": usageCost, "percent": func(value *float64) float64 { return *value * 100 }}))
|
||||
Reference in New Issue
Block a user