| 1 | package Langertha::Role::ResponseSize; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Role for an engine where you can specify the response size (in tokens) | ||||||||||||||||||||||||||||
| 3 | 178 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||
| 4 | 178 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 178 | has response_size => ( | |||||||||||||||||||||||||||
| 7 | isa => 'Int', | ||||||||||||||||||||||||||||
| 8 | is => 'ro', | ||||||||||||||||||||||||||||
| 9 | predicate => 'has_response_size', | ||||||||||||||||||||||||||||
| 10 | ); | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3770 | ▶ | sub get_response_size { | ||||||||||||||||||||||||||
|
Subroutine
get_response_size: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 3770 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 15 | 3770 | ▶ | return $self->response_size if $self->has_response_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 3611 | my $model_default = $self->_model_response_size_default; | |||||||||||||||||||||||||||
| 17 | 3611 | ▶ | return $model_default if defined $model_default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 3150 | ▶ | return $self->default_response_size if $self->can('default_response_size'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 1900 | return; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Default: no per-model defaults. Engines override with an ordered list of | ||||||||||||||||||||||||||||
| 24 | # ( $matcher => $tokens ) pairs, matched against chat_model like | ||||||||||||||||||||||||||||
| 25 | # Role::Capabilities' model_capability_corrections (ADR 0019 k225 Update). | ||||||||||||||||||||||||||||
| 26 | 3100 | ▶ | sub model_response_size_defaults { return () } | ||||||||||||||||||||||||||
|
Subroutine
model_response_size_defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3611 | ▶ | sub _model_response_size_default { | ||||||||||||||||||||||||||
|
Subroutine
_model_response_size_default: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 29 | 3611 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 30 | 3611 | my @defaults = $self->model_response_size_defaults; | |||||||||||||||||||||||||||
| 31 | 3611 | ▶ | return unless @defaults && $self->can('chat_model'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@defaults and $self->can("chat_model")
MC/DC50%1/2
@defaults and $self->can("chat_model")
@defaults
$self->can("chat_model")
Truth table66%2/3
A and B
A= @defaultsB= $self->can("chat_model")
| |||||||||||||||||||||||||||||
| 32 | 511 | ▶ | my $model = $self->chat_model // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->chat_model // ""
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
| |||||||||||||||||||||||||||||
| 33 | 511 | my $size; | |||||||||||||||||||||||||||
| 34 | 511 | ▶ | while ( @defaults >= 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 1022 | my ( $matcher, $tokens ) = splice @defaults, 0, 2; | |||||||||||||||||||||||||||
| 36 | 1022 | ▶ | my $hit = ref $matcher eq 'Regexp' ? ( $model =~ $matcher ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | : ( $model eq $matcher ); | ||||||||||||||||||||||||||||
| 38 | 1022 | ▶ | $size = $tokens if $hit; # later matching entries win | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 511 | return $size; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 178 | 1; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | __END__ | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =pod | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 NAME | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Langertha::Role::ResponseSize - Role for an engine where you can specify the response size (in tokens) | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 VERSION | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | version 0.503 | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head2 response_size | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Maximum number of tokens to generate in the response. Optional. When not set, | ||||||||||||||||||||||||||||
| 64 | the engine uses its own C<default_response_size> if available, or omits the | ||||||||||||||||||||||||||||
| 65 | parameter from the request. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 get_response_size | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | my $size = $engine->get_response_size; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Returns the effective response size: the explicit C<response_size> if set, | ||||||||||||||||||||||||||||
| 72 | otherwise the per-model default from L</model_response_size_defaults> for the | ||||||||||||||||||||||||||||
| 73 | current C<chat_model>, otherwise the engine's C<default_response_size>, | ||||||||||||||||||||||||||||
| 74 | otherwise C<undef>. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 model_response_size_defaults | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub model_response_size_defaults { | ||||||||||||||||||||||||||||
| 79 | return ( qr/\Akimi-k3(?!\d)/ => 16000 ); | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Per-model default response size, used only when the caller set no | ||||||||||||||||||||||||||||
| 83 | C<response_size>. Returns an B<ordered> list of C<< ( $matcher => $tokens ) >> | ||||||||||||||||||||||||||||
| 84 | pairs; C<$matcher> is an exact model id (C<eq>) or a C<qr//> matched against | ||||||||||||||||||||||||||||
| 85 | C<chat_model>, and later matching entries win. A matching entry replaces the | ||||||||||||||||||||||||||||
| 86 | engine's C<default_response_size> for that model; it never overrides an | ||||||||||||||||||||||||||||
| 87 | explicit C<response_size> or a per-request C<max_tokens>. The default returns | ||||||||||||||||||||||||||||
| 88 | an empty list. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =over | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =item * L<Langertha::Role::ContextSize> - Limit total context tokens | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =item * L<Langertha::Role::Temperature> - Sampling temperature | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =back | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 Issues | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 105 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head2 IRC | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 124 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||