| 1 | package Langertha::Knarr::Router; | ||||||||||||||||||||||||||||
| 2 | 4 | our $VERSION = '1.100'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Model name to Langertha engine routing with caching | ||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Log::Any qw( $log ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Langertha (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | has config => ( | |||||||||||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||||||||||
| 12 | required => 1, | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has _engine_cache => ( | ||||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||
| 18 | 10 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | has _discovered_models => ( | ||||||||||||||||||||||||||||
| 22 | is => 'rw', | ||||||||||||||||||||||||||||
| 23 | 10 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 4 | ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | has _discovery_done => ( | |||||||||||||||||||||||||||
| 27 | is => 'rw', | ||||||||||||||||||||||||||||
| 28 | default => 0, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub resolve { | ||||||||||||||||||||||||||||
| 33 | 13 | ▶ | my ($self, $model_name, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 13 | ▶ | croak "No model specified" unless defined $model_name && length $model_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $model_name and length $model_name
MC/DC100%2/2
defined $model_name and length $model_name
defined $model_name
length $model_name
Truth table100%3/3
A and B
A= defined $model_nameB= length $model_name
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Check explicit config first | ||||||||||||||||||||||||||||
| 37 | 11 | my $models = $self->config->models; | |||||||||||||||||||||||||||
| 38 | 11 | my $def = $models->{$model_name}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Check discovered models | ||||||||||||||||||||||||||||
| 41 | 11 | ▶ | unless ($def) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 7 | ▶ | $self->_discover_models unless $self->_discovery_done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 7 | $def = $self->_discovered_models->{$model_name}; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Fall back to default engine (skip_default allows caller to try passthrough first) | ||||||||||||||||||||||||||||
| 47 | 11 | ▶ | unless ($def) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 7 | ▶ | return () if $opts{skip_default}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 2 | $def = $self->config->default_engine; | |||||||||||||||||||||||||||
| 50 | 2 | ▶ | if ($def) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1 | $def = { %$def, model => $model_name }; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 6 | ▶ | croak "Model '$model_name' not configured and no default engine" unless $def; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 5 | my $engine = $self->_get_engine($def, $model_name); | |||||||||||||||||||||||||||
| 58 | 5 | ▶ | my $resolved_model = $def->{model} // $model_name; | ||||||||||||||||||||||||||
|
Condition33%1/3
$def->{"model"} // $model_name
MC/DC0%0/2
$def->{"model"} // $model_name
$def->{"model"}
$model_name
Truth table33%1/3
A // B
A= $def->{"model"}B= $model_name
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 5 | return ($engine, $resolved_model); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub _get_engine { | ||||||||||||||||||||||||||||
| 64 | 5 | ▶ | my ($self, $def, $model_name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_engine: called CC=15 SCAR=34.8
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 5 | my $engine_class = $def->{engine}; | |||||||||||||||||||||||||||
| 67 | 5 | my $cache_key = $self->_engine_cache_key($def); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | ▶ | if (my $cached = $self->_engine_cache->{$cache_key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 1 | return $cached; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | my $full_class = Langertha->resolve_engine_class($engine_class); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 4 | my %args; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # API key from env var if specified | ||||||||||||||||||||||||||||
| 78 | 4 | ▶ | if ($def->{api_key_env}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 79 | $args{api_key} = $ENV{$def->{api_key_env}} | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | // croak "Environment variable $def->{api_key_env} not set for engine $engine_class"; | ||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{$def->{"api_key_env"}} // croak(qq[Environment variable $def->{"api_key_env"} not set for engine $engine_class])
MC/DC0%0/2
$ENV{$def->{"api_key_env"}} // croak(qq[Environment variable $def->{"api_key_env"} not set for engine $engine_class])
$ENV{$def->{"api_key_env"}}
croak(qq[Environment variable $def->{"api_key_env"} not set for engine $engine_class])
Truth table0%0/3
A // B
A= $ENV{$def->{"api_key_env"}}B= croak(qq[Environment variable $def->{"api_key_env"} not set for engine $engine_class])
| |||||||||||||||||||||||||||||
| 81 | } elsif ($def->{api_key}) { | ||||||||||||||||||||||||||||
| 82 | 0 | $args{api_key} = $def->{api_key}; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 4 | ▶ | $args{url} = $def->{url} if $def->{url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 4 | ▶ | $args{model} = $def->{model} if $def->{model}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 4 | ▶ | $args{system_prompt} = $def->{system_prompt} if $def->{system_prompt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | $args{temperature} = $def->{temperature} if defined $def->{temperature}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 4 | ▶ | $args{response_size} = $def->{response_size} if defined $def->{response_size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Langfuse config from global config | ||||||||||||||||||||||||||||
| 92 | 4 | my $langfuse = $self->config->langfuse; | |||||||||||||||||||||||||||
| 93 | 4 | ▶ | if ($langfuse && %$langfuse) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$langfuse and %$langfuse
MC/DC0%0/2
$langfuse and %$langfuse
$langfuse
%$langfuse
Truth table33%1/3
A and B
A= $langfuseB= %$langfuse
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $args{langfuse_public_key} = $langfuse->{public_key} if $langfuse->{public_key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $args{langfuse_secret_key} = $langfuse->{secret_key} if $langfuse->{secret_key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $args{langfuse_url} = $langfuse->{url} if $langfuse->{url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | $log->debugf("Creating engine %s for model %s", $full_class, $model_name); | |||||||||||||||||||||||||||
| 100 | 4 | my $engine = $full_class->new(%args); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | $self->_engine_cache->{$cache_key} = $engine; | |||||||||||||||||||||||||||
| 103 | 4 | return $engine; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub _engine_cache_key { | ||||||||||||||||||||||||||||
| 107 | 5 | ▶ | my ($self, $def) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_engine_cache_key: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 108 | 5 | ▶ | my $engine = $def->{engine} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$def->{"engine"} // ""
MC/DC0%0/1
$def->{"engine"} // ""
$def->{"engine"}
Truth table50%1/2
A
A= $def->{"engine"}
| |||||||||||||||||||||||||||||
| 109 | 5 | ▶ | my $url = $def->{url} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$def->{"url"} // ""
MC/DC100%1/1
$def->{"url"} // ""
$def->{"url"}
Truth table100%2/2
A
A= $def->{"url"}
| |||||||||||||||||||||||||||||
| 110 | 5 | ▶ | my $key_env = $def->{api_key_env} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$def->{"api_key_env"} // ""
MC/DC0%0/1
$def->{"api_key_env"} // ""
$def->{"api_key_env"}
Truth table50%1/2
A
A= $def->{"api_key_env"}
| |||||||||||||||||||||||||||||
| 111 | 5 | return join('|', $engine, $url, $key_env); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub _discover_models { | ||||||||||||||||||||||||||||
| 115 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_discover_models: called CC=10 SCAR=42.2
| |||||||||||||||||||||||||||||
| 116 | 6 | ▶ | return if $self->_discovery_done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 6 | $self->_discovery_done(1); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 6 | ▶ | return unless $self->config->auto_discover; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my $models = $self->config->models; | |||||||||||||||||||||||||||
| 122 | 0 | my %seen_engines; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | for my $name (keys %$models) { | |||||||||||||||||||||||||||
| 125 | 0 | my $def = $models->{$name}; | |||||||||||||||||||||||||||
| 126 | 0 | my $engine_class = $def->{engine}; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | next if $seen_engines{$engine_class}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | eval { | |||||||||||||||||||||||||||
| 130 | 0 | my $engine = $self->_get_engine($def, $name); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($engine->can('list_models')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | $log->debugf("Discovering models from %s", $engine_class); | |||||||||||||||||||||||||||
| 133 | 0 | my $model_ids = $engine->list_models; | |||||||||||||||||||||||||||
| 134 | 0 | for my $id (@$model_ids) { | |||||||||||||||||||||||||||
| 135 | 0 | ▶ | next if $models->{$id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | next if $self->_discovered_models->{$id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | $self->_discovered_models->{$id} = { | |||||||||||||||||||||||||||
| 138 | %$def, | ||||||||||||||||||||||||||||
| 139 | model => $id, | ||||||||||||||||||||||||||||
| 140 | discovered => 1, | ||||||||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||||||
| 142 | 0 | $log->debugf("Discovered model: %s (via %s)", $id, $engine_class); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | $log->debugf("Model discovery skipped for %s: %s", $engine_class, $@); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub list_models { | ||||||||||||||||||||||||||||
| 154 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
list_models: called CC=7 SCAR=23.8
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 3 | ▶ | $self->_discover_models unless $self->_discovery_done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 3 | my $configured = $self->config->models; | |||||||||||||||||||||||||||
| 159 | 3 | my $discovered = $self->_discovered_models; | |||||||||||||||||||||||||||
| 160 | 3 | my @models; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 3 | for my $name (sort keys %$configured) { | |||||||||||||||||||||||||||
| 163 | push @models, { | ||||||||||||||||||||||||||||
| 164 | id => $name, | ||||||||||||||||||||||||||||
| 165 | engine => $configured->{$name}{engine}, | ||||||||||||||||||||||||||||
| 166 | 4 | ▶ | model => $configured->{$name}{model} // $name, | ||||||||||||||||||||||||||
|
Condition66%2/3
$configured->{$name}{"model"} // $name
MC/DC0%0/2
$configured->{$name}{"model"} // $name
$configured->{$name}{"model"}
$name
Truth table66%2/3
A // B
A= $configured->{$name}{"model"}B= $name
| |||||||||||||||||||||||||||||
| 167 | source => 'configured', | ||||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 3 | for my $name (sort keys %$discovered) { | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | next if $configured->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | push @models, { | ||||||||||||||||||||||||||||
| 174 | id => $name, | ||||||||||||||||||||||||||||
| 175 | engine => $discovered->{$name}{engine}, | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | model => $discovered->{$name}{model} // $name, | ||||||||||||||||||||||||||
|
Condition0%0/3
$discovered->{$name}{"model"} // $name
MC/DC0%0/2
$discovered->{$name}{"model"} // $name
$discovered->{$name}{"model"}
$name
Truth table0%0/3
A // B
A= $discovered->{$name}{"model"}B= $name
| |||||||||||||||||||||||||||||
| 177 | source => 'discovered', | ||||||||||||||||||||||||||||
| 178 | }; | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 3 | return \@models; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | sub is_passthrough_model { | ||||||||||||||||||||||||||||
| 185 | 5 | ▶ | my ($self, $model) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_passthrough_model: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 186 | 5 | ▶ | return 0 unless defined $model && length $model; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $model and length $model
MC/DC0%0/2
defined $model and length $model
defined $model
length $model
Truth table33%1/3
A and B
A= defined $modelB= length $model
| |||||||||||||||||||||||||||||
| 187 | 5 | my @r = eval { $self->resolve($model, skip_default => 1) }; | |||||||||||||||||||||||||||
| 188 | 5 | ▶ | return @r ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 4 | 1; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =pod | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head1 NAME | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Langertha::Knarr::Router - Model name to Langertha engine routing with caching | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head1 VERSION | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | version 1.100 | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | use Langertha::Knarr::Router; | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | my $router = Langertha::Knarr::Router->new(config => $config); | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | my ($engine, $model) = $router->resolve('gpt-4o'); | ||||||||||||||||||||||||||||
| 215 | my $result = $engine->simple_chat(@messages); | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | my $models = $router->list_models; | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Resolves a model name to a Langertha engine instance and canonical model | ||||||||||||||||||||||||||||
| 222 | identifier. Engine instances are cached and reused across requests to avoid | ||||||||||||||||||||||||||||
| 223 | repeated construction overhead. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Engine classes are resolved from both C<Langertha::Engine::*> and | ||||||||||||||||||||||||||||
| 226 | C<LangerthaX::Engine::*>. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | When C<auto_discover> is enabled in the config, the router queries each | ||||||||||||||||||||||||||||
| 229 | configured engine for its full model list on first use, making all discovered | ||||||||||||||||||||||||||||
| 230 | models available as routing targets. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head2 config | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | The L<Langertha::Knarr::Config> object. Required. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head2 resolve | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | my ($engine, $model) = $router->resolve($model_name, %opts); | ||||||||||||||||||||||||||||
| 239 | my ($engine, $model) = $router->resolve($model_name, skip_default => 1); | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Resolves C<$model_name> to a Langertha engine instance and the canonical model | ||||||||||||||||||||||||||||
| 242 | string to use with that engine. The resolution order is: | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =over | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =item 1. Explicit model config in L<Langertha::Knarr::Config/models> | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =item 2. Auto-discovered models (if C<auto_discover> is enabled) | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =item 3. The default engine from L<Langertha::Knarr::Config/default_engine> | ||||||||||||||||||||||||||||
| 251 | (skipped when C<skip_default =E<gt> 1> is passed) | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =back | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | Croaks if the model cannot be resolved. Pass C<skip_default =E<gt> 1> to allow | ||||||||||||||||||||||||||||
| 256 | the caller to try passthrough before falling back to the default engine. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =head2 list_models | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | my $models = $router->list_models; | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | Returns an ArrayRef of model hashrefs, each with keys C<id>, C<engine>, | ||||||||||||||||||||||||||||
| 263 | C<model>, and C<source> (either C<configured> or C<discovered>). Triggers | ||||||||||||||||||||||||||||
| 264 | auto-discovery if not already done. Used to build the model list responses | ||||||||||||||||||||||||||||
| 265 | for C<GET /v1/models> and C<GET /api/tags>. | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =over | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =item * L<Langertha::Knarr> — Main documentation and routing priority description | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =item * L<Langertha::Knarr::Config> — Provides model and engine configuration | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =back | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head2 Issues | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 282 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =head2 IRC | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 301 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =cut | ||||||||||||||||||||||||||||