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