blib/lib/Langertha/Pricing.pm

filter: Statementstmt 100% 28 / 28 Branchbran 100% 4 / 4 Conditioncond 86.3% 19 / 22 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 89.3% 67 / 75 CC 12 SCAR 19.0
CC
12
Cov
94%
cost_for
10.1
rule_for
3.0
BEGIN
1.0
CRAP 6.7
« blib/lib/Langertha/RunContext.pm ↑ Summary blib/lib/Langertha/Runtime/Metrics.pm »
1 package Langertha::Pricing;
2 # ABSTRACT: Model-to-price catalog producing Langertha::Cost from Langertha::Usage
3 9 our $VERSION = '0.505';
4 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Langertha::Cost;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Map of model id → { input_per_million => N, output_per_million => N,
8 # cached_input_per_million => N, cache_write_per_million => N }. The last two
9 # are optional. All keys are USD per 1,000,000 tokens.
10 has rules => (
11 is => 'ro',
12 isa => 'HashRef',
13 22 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 9 );
15  
16 # Optional fallback rule applied when a model id is unknown.
17 has default_rule => (
18 is => 'ro',
19 isa => 'Maybe[HashRef]',
20 8 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 9 );
22  
23 46 ▶sub rule_for {
Subroutine rule_for: called CC=3 SCAR=11.0
Pod documented
24 46 my ($self, $model) = @_;
25 46 ▶ return $self->rules->{$model} if defined $model && exists $self->rules->{$model};
Branch: 1 branch
BranchTrueFalseTotal
if defined $model and exists $self->rules->{$model} 7 39 46
Condition100%3/3
defined $model and exists $self->rules->{$model}
!ll&&!rl&&r
1387
MC/DC100%2/2
defined $model and exists $self->rules->{$model}
defined $model exists $self->rules->{$model}
Truth table100%3/3
A and B
A= defined $modelB= exists $self->rules->{$model}
ABresult
0X0
100
111
26 39 return $self->default_rule;
27 }
28  
29 41 ▶sub cost_for {
Subroutine cost_for: called CC=10 SCAR=23.1
Pod documented
30 41 my ($self, $usage, $model) = @_;
31 41 ▶ my $rule = $self->rule_for($model) || {};
Condition100%2/2
$self->rule_for($model) || {}
l!l
383
MC/DC100%1/1
$self->rule_for($model) || {}
$self->rule_for($model)
Truth table100%2/2
A
A= $self->rule_for($model)
Aresult
11
00
32 41 ▶ my $ipm = 0 + ( $rule->{input_per_million} // 0 );
Condition100%2/2
$$rule{"input_per_million"} // 0
l!l
383
MC/DC100%1/1
$$rule{"input_per_million"} // 0
$$rule{"input_per_million"}
Truth table100%2/2
A
A= $$rule{"input_per_million"}
Aresult
11
00
33 41 ▶ my $opm = 0 + ( $rule->{output_per_million} // 0 );
Condition100%2/2
$$rule{"output_per_million"} // 0
l!l
374
MC/DC100%1/1
$$rule{"output_per_million"} // 0
$$rule{"output_per_million"}
Truth table100%2/2
A
A= $$rule{"output_per_million"}
Aresult
11
00
34 41 my $output_usd = ( $usage->output_tokens / 1_000_000 ) * $opm;
35  
36 # A rule without a cache rate prices input_tokens as a whole, as it always
37 # did. A rule with one splits the cache counts out of input_tokens (or adds
38 # them, where the wire counts them beside it — Usage->input_includes_cache)
39 # so no token is priced twice. A missing rate falls back to the input rate:
40 # no discount the rule did not state. (k263, ADR 0031)
41 41 my $cached_rate = $rule->{cached_input_per_million};
42 41 my $write_rate = $rule->{cache_write_per_million};
43 41 ▶ unless ( defined $cached_rate || defined $write_rate ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cached_rate or defined $write_rate) 17 24 41
Condition100%3/3
defined $cached_rate or defined $write_rate
l!l&&r!l&&!r
23117
MC/DC100%2/2
defined $cached_rate or defined $write_rate
defined $cached_rate defined $write_rate
Truth table100%3/3
A or B
A= defined $cached_rateB= defined $write_rate
ABresult
1X1
011
000
44 17 return Langertha::Cost->new(
45 input_usd => ( $usage->input_tokens / 1_000_000 ) * $ipm,
46 output_usd => $output_usd,
47 );
48 }
49 24 ▶ my $cached = $usage->cached_tokens // 0;
Condition50%1/2
$usage->cached_tokens // 0
l!l
240
MC/DC0%0/1
$usage->cached_tokens // 0
$usage->cached_tokens
Truth table50%1/2
A
A= $usage->cached_tokens
Aresult
11
00
50 24 ▶ my $write = $usage->cache_write_tokens // 0;
Condition100%2/2
$usage->cache_write_tokens // 0
l!l
1212
MC/DC100%1/1
$usage->cache_write_tokens // 0
$usage->cache_write_tokens
Truth table100%2/2
A
A= $usage->cache_write_tokens
Aresult
11
00
51 24 ▶ return Langertha::Cost->new(
Condition66%4/6
$cached_rate // $ipm
l!l&&r!l&&!r
2310
$write_rate // $ipm
l!l&&r!l&&!r
1680
MC/DC0%0/2
$cached_rate // $ipm
$cached_rate $ipm
MC/DC0%0/2
$write_rate // $ipm
$write_rate $ipm
Truth table66%2/3
A // B
A= $cached_rateB= $ipm
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $write_rateB= $ipm
ABresult
1X1
011
000
52 input_usd => ( $usage->uncached_input_tokens / 1_000_000 ) * $ipm,
53 output_usd => $output_usd,
54 cache_read_usd => ( $cached / 1_000_000 ) * ( 0 + ( $cached_rate // $ipm ) ),
55 cache_write_usd => ( $write / 1_000_000 ) * ( 0 + ( $write_rate // $ipm ) ),
56 );
57 }
58  
59  
60 9 __PACKAGE__->meta->make_immutable;
61 9 1;
62  
63 __END__
« blib/lib/Langertha/RunContext.pm ↑ Summary blib/lib/Langertha/Runtime/Metrics.pm »