« blib/lib/Langertha/Knarr/Handler/Tracing.pm
↑ Summary
blib/lib/Langertha/Knarr/Protocol/Anthropic.pm »
| 1 | package Langertha::Knarr::Reasoning; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Map a face's native reasoning controls onto the normalized reasoning_effort level | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | our $VERSION = '1.103'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | # The normalized ascending reasoning vocabulary (Langertha::Reasoning::Level). | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | my %LEVEL = map { $_ => 1 } qw( none minimal low medium high xhigh max ); | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | has default_level => ( | |||||||||||||||||||||||||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 14 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 15 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 16 | builder => '_build_default_level', | ||||||||||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | sub _build_default_level { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_default_level: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 6 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | ▶ | return 'medium' unless $self->budget_available; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | my $attr = Langertha::Reasoning::BudgetPolicy->meta->find_attribute_by_name('default_bool_level'); | |||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | my $level = $attr && $attr->default; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$attr && $attr->default
MC/DC0%0/2
$attr && $attr->default
$attr
$attr->default
Truth table33%1/3
A && B
A= $attrB= $attr->default
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | ▶ | return defined $level && $LEVEL{$level} ? $level : 'medium'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $level and $LEVEL{$level}
MC/DC0%0/2
defined $level and $LEVEL{$level}
defined $level
$LEVEL{$level}
Truth table33%1/3
A and B
A= defined $levelB= $LEVEL{$level}
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | has budget_points => ( | |||||||||||||||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | isa => 'HashRef[Int]', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | predicate => 'has_budget_points', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 39 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 39 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | croak __PACKAGE__."->new: unknown default_level '".$args->{default_level}."'" | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 39 | ▶ | if exists $args->{default_level} && !$LEVEL{ $args->{default_level} // '' }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
exists $args->{"default_level"} and not $LEVEL{$args->{"default_level"} // ""}
$args->{"default_level"} // ""
MC/DC100%2/2
exists $args->{"default_level"} and not $LEVEL{$args->{"default_level"} // ""}
exists $args->{"default_level"}
not $LEVEL{$args->{"default_level"} // ""}
MC/DC0%0/1
$args->{"default_level"} // ""
$args->{"default_level"}
Truth table100%3/3
A and not(B)
A= exists $args->{"default_level"}B= not $LEVEL{$args->{"default_level"} // ""}
Truth table50%1/2
A
A= $args->{"default_level"}
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 38 | ▶ | if ( $self->has_budget_points ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | for my $level ( sort keys %{ $self->budget_points } ) { | |||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | ▶ | croak __PACKAGE__."->new: unknown level '".$level."' in budget_points" unless $LEVEL{$level}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 37 | return; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 7 | has budget_available => ( | |||||||||||||||||||||||||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | isa => 'Bool', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | builder => '_build_budget_available', | ||||||||||||||||||||||||||||||||||||||||||
| 52 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 14 | ▶ | sub _build_budget_available { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_budget_available: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | # Optional core feature (Langertha > 0.503): probed, not a hard dependency. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 14 | ▶ | return eval { require Langertha::Reasoning::BudgetPolicy; 1 } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 6 | ▶ | sub fallback_budget_points { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fallback_budget_points: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 6 | return { low => 2048, medium => 8192, high => 24576 }; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 11 | ▶ | sub budget_policy { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
budget_policy: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 11 | my ( $self, $model ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 11 | ▶ | return unless $self->budget_available; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 10 | my $policy = eval { | |||||||||||||||||||||||||||||||||||||||||
| 70 | 10 | my $profile = Langertha::Reasoning::Profile->for_model($model); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | my %opts = $self->has_budget_points | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$profile->has_budget_min and $profile->has_budget_max
MC/DC50%1/2
$profile->has_budget_min and $profile->has_budget_max
$profile->has_budget_min
$profile->has_budget_max
Truth table66%2/3
A and B
A= $profile->has_budget_minB= $profile->has_budget_max
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | ? ( points => $self->budget_points ) | ||||||||||||||||||||||||||||||||||||||||||
| 73 | : $profile->has_budget_min && $profile->has_budget_max | ||||||||||||||||||||||||||||||||||||||||||
| 74 | ? () | ||||||||||||||||||||||||||||||||||||||||||
| 75 | : ( points => $self->fallback_budget_points ); | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | Langertha::Reasoning::BudgetPolicy->new( profile => $profile, %opts ); | |||||||||||||||||||||||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 10 | return $policy; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 12 | ▶ | sub level_for_budget { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
level_for_budget: called CC=7 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 12 | my ( $self, $budget, $model ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 12 | ▶ | return unless defined $budget && !ref $budget && $budget =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $budget and not ref $budget and $budget =~ /\A[0-9]+\z/
defined $budget and not ref $budget
MC/DC33%1/3
defined $budget and not ref $budget and $budget =~ /\A[0-9]+\z/
defined $budget
not ref $budget
$budget =~ /\A[0-9]+\z/
Truth table50%2/4
A and not(B) and C
A= defined $budgetB= not ref $budgetC= $budget =~ /\A[0-9]+\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 11 | ▶ | my $policy = $self->budget_policy($model) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 10 | my $level = eval { $policy->level_for( $budget + 0 ) }; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 10 | ▶ | return defined $level && $LEVEL{$level} ? $level : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $level and $LEVEL{$level}
MC/DC0%0/2
defined $level and $LEVEL{$level}
defined $level
$LEVEL{$level}
Truth table33%1/3
A and B
A= defined $levelB= $LEVEL{$level}
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 5 | ▶ | sub level_for_bool { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
level_for_bool: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 5 | my ( $self, $on ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 5 | ▶ | return $on ? $self->default_level : 'none'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 95 | ▶ | sub _explicit_level { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_explicit_level: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 95 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 95 | ▶ | return defined $value && !ref $value && length $value ? $value : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $value and not ref $value and length $value
defined $value and not ref $value
MC/DC33%1/3
defined $value and not ref $value and length $value
defined $value
not ref $value
length $value
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 34 | ▶ | sub from_anthropic { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_anthropic: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 34 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 34 | ▶ | return unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 34 | ▶ | my $output_config = ref $data->{output_config} eq 'HASH' ? $data->{output_config} : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 34 | for my $explicit ( $data->{reasoning_effort}, $output_config->{effort} ) { | |||||||||||||||||||||||||||||||||||||||||
| 107 | 67 | my ($level) = $self->_explicit_level($explicit); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 67 | ▶ | return $level if defined $level; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 31 | my $thinking = $data->{thinking}; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 31 | ▶ | return unless ref $thinking eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 20 | ▶ | my $type = $thinking->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$thinking->{"type"} // ""
MC/DC0%0/1
$thinking->{"type"} // ""
$thinking->{"type"}
Truth table50%1/2
A
A= $thinking->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 20 | ▶ | return 'none' if $type eq 'disabled'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 18 | ▶ | return $self->default_level if $type eq 'adaptive'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 14 | ▶ | return unless $type eq 'enabled'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 13 | ▶ | return $self->default_level unless defined $thinking->{budget_tokens}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 12 | return $self->level_for_budget( $thinking->{budget_tokens}, $data->{model} ); | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 28 | ▶ | sub from_ollama { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_ollama: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 28 | my ( $self, $think, $explicit ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 28 | my ($level) = $self->_explicit_level($explicit); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 28 | ▶ | return $level if defined $level; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 27 | ▶ | return unless defined $think; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 11 | ▶ | return $self->level_for_bool($think) if JSON::MaybeXS::is_bool($think); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 6 | ▶ | return if ref $think; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 6 | ▶ | return $LEVEL{$think} ? $think : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||||||||||||||||