| 1 | package Langertha::Role::Models; | ||||||||||
| 2 | # ABSTRACT: Role for APIs with several models | ||||||||||
| 3 | 34 | our $VERSION = '0.502'; | |||||||||
| 4 | 34 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 34 | requires qw( | |||||||||
| 7 | default_model | ||||||||||
| 8 | ); | ||||||||||
| 9 | |||||||||||
| 10 | 34 | has models => ( | |||||||||
| 11 | is => 'rw', | ||||||||||
| 12 | isa => 'ArrayRef[Str]', | ||||||||||
| 13 | lazy_build => 1, | ||||||||||
| 14 | ); | ||||||||||
| 15 | sub _build_models { | ||||||||||
| 16 | 0 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
_build_models: not called CC=2 SCAR=17.9
| |||||||||||
| 17 | 0 | ▶ | return $self->list_models() if $self->can('list_models'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 0 | return [ $self->model ]; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 34 | has model => ( | |||||||||
| 23 | is => 'ro', | ||||||||||
| 24 | isa => 'Maybe[Str]', | ||||||||||
| 25 | lazy_build => 1, | ||||||||||
| 26 | ); | ||||||||||
| 27 | sub _build_model { | ||||||||||
| 28 | 13 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
_build_model: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 13 | return $self->default_model; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | # Cache configuration | ||||||||||
| 34 | has models_cache_ttl => ( | ||||||||||
| 35 | is => 'ro', | ||||||||||
| 36 | isa => 'Int', | ||||||||||
| 37 | 245 | ▶ | default => sub { 3600 }, # 1 hour default | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 34 | ); | |||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | has _models_cache => ( | ||||||||||
| 42 | is => 'rw', | ||||||||||
| 43 | isa => 'HashRef', | ||||||||||
| 44 | 247 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 34 | traits => ['Hash'], | |||||||||
| 46 | handles => { | ||||||||||
| 47 | _clear_models_cache => 'clear', | ||||||||||
| 48 | }, | ||||||||||
| 49 | ); | ||||||||||
| 50 | |||||||||||
| 51 | # Public method to clear the cache | ||||||||||
| 52 | sub clear_models_cache { | ||||||||||
| 53 | 3 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
clear_models_cache: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 54 | 3 | $self->_clear_models_cache; | |||||||||
| 55 | 3 | return; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | 34 | 1; | |||||||||
| 61 | |||||||||||
| 62 | __END__ | ||||||||||
| 63 | |||||||||||
| 64 | =pod | ||||||||||
| 65 | |||||||||||
| 66 | =encoding UTF-8 | ||||||||||
| 67 | |||||||||||
| 68 | =head1 NAME | ||||||||||
| 69 | |||||||||||
| 70 | Langertha::Role::Models - Role for APIs with several models | ||||||||||
| 71 | |||||||||||
| 72 | =head1 VERSION | ||||||||||
| 73 | |||||||||||
| 74 | version 0.502 | ||||||||||
| 75 | |||||||||||
| 76 | =head2 models | ||||||||||
| 77 | |||||||||||
| 78 | ArrayRef of available model name strings. Lazily populated by calling | ||||||||||
| 79 | C<list_models> if the engine supports it, otherwise contains only the | ||||||||||
| 80 | currently selected C<model>. | ||||||||||
| 81 | |||||||||||
| 82 | =head2 model | ||||||||||
| 83 | |||||||||||
| 84 | The model name to use for requests. Defaults to the engine's C<default_model>. | ||||||||||
| 85 | Engines that require this role must implement C<default_model>. | ||||||||||
| 86 | |||||||||||
| 87 | =head2 models_cache_ttl | ||||||||||
| 88 | |||||||||||
| 89 | Time-to-live in seconds for the models list cache. Defaults to C<3600> (one hour). | ||||||||||
| 90 | |||||||||||
| 91 | =head2 clear_models_cache | ||||||||||
| 92 | |||||||||||
| 93 | $engine->clear_models_cache; | ||||||||||
| 94 | |||||||||||
| 95 | Clears the internal models list cache, forcing a fresh fetch on the next | ||||||||||
| 96 | access to C<models>. | ||||||||||
| 97 | |||||||||||
| 98 | =head1 SEE ALSO | ||||||||||
| 99 | |||||||||||
| 100 | =over | ||||||||||
| 101 | |||||||||||
| 102 | =item * L<Langertha::Role::OpenAPI> - Typically composed alongside this role | ||||||||||
| 103 | |||||||||||
| 104 | =item * L<Langertha::Role::Chat> - Uses C<model> via C<chat_model> | ||||||||||
| 105 | |||||||||||
| 106 | =item * L<Langertha::Role::Embedding> - Uses C<model> via C<embedding_model> | ||||||||||
| 107 | |||||||||||
| 108 | =item * L<Langertha::Role::Transcription> - Uses C<model> via C<transcription_model> | ||||||||||
| 109 | |||||||||||
| 110 | =back | ||||||||||
| 111 | |||||||||||
| 112 | =head1 SUPPORT | ||||||||||
| 113 | |||||||||||
| 114 | =head2 Issues | ||||||||||
| 115 | |||||||||||
| 116 | Please report bugs and feature requests on GitHub at | ||||||||||
| 117 | L<https://github.com/Getty/langertha/issues>. | ||||||||||
| 118 | |||||||||||
| 119 | =head2 IRC | ||||||||||
| 120 | |||||||||||
| 121 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||
| 122 | |||||||||||
| 123 | =head1 CONTRIBUTING | ||||||||||
| 124 | |||||||||||
| 125 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||
| 126 | |||||||||||
| 127 | =head1 AUTHOR | ||||||||||
| 128 | |||||||||||
| 129 | Torsten Raudssus <getty@cpan.org> | ||||||||||
| 130 | |||||||||||
| 131 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 132 | |||||||||||
| 133 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||
| 134 | |||||||||||
| 135 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 136 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 137 | |||||||||||
| 138 | =cut | ||||||||||