@@ -48,6 +48,36 @@ type DownloadMediaOptions struct {
4848 Filter func (part * Part ) bool // Filter to apply to parts that are media URLs.
4949}
5050
51+ func CalculateInputOutputUsage (req * ModelRequest , resp * ModelResponse ) {
52+ if resp .Usage == nil {
53+ resp .Usage = & GenerationUsage {}
54+ }
55+ if resp .Usage .InputCharacters == 0 {
56+ resp .Usage .InputCharacters = countInputCharacters (req )
57+ }
58+ if resp .Usage .OutputCharacters == 0 {
59+ resp .Usage .OutputCharacters = countOutputCharacters (resp )
60+ }
61+ if resp .Usage .InputImages == 0 {
62+ resp .Usage .InputImages = countInputParts (req , func (part * Part ) bool { return part .IsImage () })
63+ }
64+ if resp .Usage .OutputImages == 0 {
65+ resp .Usage .OutputImages = countOutputParts (resp , func (part * Part ) bool { return part .IsImage () })
66+ }
67+ if resp .Usage .InputVideos == 0 {
68+ resp .Usage .InputVideos = countInputParts (req , func (part * Part ) bool { return part .IsVideo () })
69+ }
70+ if resp .Usage .OutputVideos == 0 {
71+ resp .Usage .OutputVideos = countOutputParts (resp , func (part * Part ) bool { return part .IsVideo () })
72+ }
73+ if resp .Usage .InputAudioFiles == 0 {
74+ resp .Usage .InputAudioFiles = countInputParts (req , func (part * Part ) bool { return part .IsAudio () })
75+ }
76+ if resp .Usage .OutputAudioFiles == 0 {
77+ resp .Usage .OutputAudioFiles = countOutputParts (resp , func (part * Part ) bool { return part .IsAudio () })
78+ }
79+ }
80+
5181// addAutomaticTelemetry creates middleware that automatically measures latency and calculates character and media counts.
5282func addAutomaticTelemetry () ModelMiddleware {
5383 return func (fn ModelFunc ) ModelFunc {
@@ -66,33 +96,7 @@ func addAutomaticTelemetry() ModelMiddleware {
6696 resp .LatencyMs = latencyMs
6797 }
6898
69- if resp .Usage == nil {
70- resp .Usage = & GenerationUsage {}
71- }
72- if resp .Usage .InputCharacters == 0 {
73- resp .Usage .InputCharacters = countInputCharacters (req )
74- }
75- if resp .Usage .OutputCharacters == 0 {
76- resp .Usage .OutputCharacters = countOutputCharacters (resp )
77- }
78- if resp .Usage .InputImages == 0 {
79- resp .Usage .InputImages = countInputParts (req , func (part * Part ) bool { return part .IsImage () })
80- }
81- if resp .Usage .OutputImages == 0 {
82- resp .Usage .OutputImages = countOutputParts (resp , func (part * Part ) bool { return part .IsImage () })
83- }
84- if resp .Usage .InputVideos == 0 {
85- resp .Usage .InputVideos = countInputParts (req , func (part * Part ) bool { return part .IsVideo () })
86- }
87- if resp .Usage .OutputVideos == 0 {
88- resp .Usage .OutputVideos = countOutputParts (resp , func (part * Part ) bool { return part .IsVideo () })
89- }
90- if resp .Usage .InputAudioFiles == 0 {
91- resp .Usage .InputAudioFiles = countInputParts (req , func (part * Part ) bool { return part .IsAudio () })
92- }
93- if resp .Usage .OutputAudioFiles == 0 {
94- resp .Usage .OutputAudioFiles = countOutputParts (resp , func (part * Part ) bool { return part .IsAudio () })
95- }
99+ CalculateInputOutputUsage (req , resp )
96100
97101 return resp , nil
98102 }
0 commit comments