| 1 | package Langertha::Role::ThinkTag; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Configurable think tag filtering for reasoning models | ||||||||||||||||||||||||||||
| 3 | 33 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||
| 4 | 33 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 33 | has think_tag => ( | |||||||||||||||||||||||||||
| 8 | is => 'ro', | ||||||||||||||||||||||||||||
| 9 | isa => 'Str', | ||||||||||||||||||||||||||||
| 10 | default => 'think', | ||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 33 | has think_tag_filter => ( | |||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 17 | default => 1, | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub filter_think_content { | ||||||||||||||||||||||||||||
| 22 | 41 | ▶ | my ( $self, $text ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
filter_think_content: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 41 | ▶ | return ($text, undef) unless $self->think_tag_filter && defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->think_tag_filter and defined $text
MC/DC100%2/2
$self->think_tag_filter and defined $text
$self->think_tag_filter
defined $text
Truth table100%3/3
A and B
A= $self->think_tag_filterB= defined $text
| |||||||||||||||||||||||||||||
| 24 | 39 | my $tag = $self->think_tag; | |||||||||||||||||||||||||||
| 25 | 39 | my @thinking; | |||||||||||||||||||||||||||
| 26 | # Matched pairs: <think>...</think> | ||||||||||||||||||||||||||||
| 27 | 39 | $text =~ s{<\Q$tag\E>(.*?)</\Q$tag\E>}{push @thinking, $1; ''}esg; | |||||||||||||||||||||||||||
| 28 | # Unclosed tag: <think>... (rest of text) — model stopped mid-thought | ||||||||||||||||||||||||||||
| 29 | 39 | ▶ | if ($text =~ s{<\Q$tag\E>(.*)$}{}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 3 | ▶ | push @thinking, $1 if length $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 39 | $text =~ s/^\s+//; | |||||||||||||||||||||||||||
| 33 | 39 | $text =~ s/\s+$//; | |||||||||||||||||||||||||||
| 34 | 39 | ▶ | my $thinking = @thinking ? join("\n", @thinking) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 39 | return ($text, $thinking); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | around 'chat_response' => sub { | ||||||||||||||||||||||||||||
| 40 | 28 | ▶ | my ( $orig, $self, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 41 | 28 | my $response = $self->$orig(@args); | |||||||||||||||||||||||||||
| 42 | 27 | ▶ | return $response unless $self->think_tag_filter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 26 | my ($filtered, $thinking) = $self->filter_think_content($response->content); | |||||||||||||||||||||||||||
| 44 | 26 | ▶ | return $response->clone_with( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | content => $filtered, | ||||||||||||||||||||||||||||
| 46 | defined $thinking ? (thinking => $thinking) : (), | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | 33 | }; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | around 'simple_chat_stream' => sub { | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my ( $orig, $self, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 52 | 0 | my $content = $self->$orig(@args); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | return $content unless $self->think_tag_filter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | my ($filtered, $thinking) = $self->filter_think_content($content); | |||||||||||||||||||||||||||
| 55 | 0 | return $filtered; | |||||||||||||||||||||||||||
| 56 | 33 | }; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | around 'simple_chat_stream_realtime_f' => sub { | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my ( $orig, $self, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 60 | return $self->$orig(@args)->then(sub { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my ($content, $chunks) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return Future->done($content, $chunks) unless $self->think_tag_filter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($filtered, $thinking) = $self->filter_think_content($content); | |||||||||||||||||||||||||||
| 64 | 0 | return Future->done($filtered, $chunks); | |||||||||||||||||||||||||||
| 65 | 0 | }); | |||||||||||||||||||||||||||
| 66 | 33 | }; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 33 | 1; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | __END__ | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =pod | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head1 NAME | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Langertha::Role::ThinkTag - Configurable think tag filtering for reasoning models | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 VERSION | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | version 0.502 | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Think tag filter is enabled by default on all engines. | ||||||||||||||||||||||||||||
| 88 | # <think> tags are automatically stripped and thinking preserved: | ||||||||||||||||||||||||||||
| 89 | my $response = $engine->simple_chat('Explain quantum computing'); | ||||||||||||||||||||||||||||
| 90 | say $response; # clean answer text | ||||||||||||||||||||||||||||
| 91 | say $response->thinking; # chain-of-thought (if any) | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # For APIs with native reasoning (DeepSeek, Anthropic, Gemini), | ||||||||||||||||||||||||||||
| 94 | # thinking is extracted from the API response automatically — | ||||||||||||||||||||||||||||
| 95 | # no tag filtering needed. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Custom tag name (e.g. for models using <reasoning> tags): | ||||||||||||||||||||||||||||
| 98 | my $engine = Langertha::Engine::vLLM->new( | ||||||||||||||||||||||||||||
| 99 | url => $vllm_url, | ||||||||||||||||||||||||||||
| 100 | model => 'my-reasoning-model', | ||||||||||||||||||||||||||||
| 101 | think_tag => 'reasoning', | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Disable filtering if you want raw output: | ||||||||||||||||||||||||||||
| 105 | my $engine = Langertha::Engine::OpenAI->new( | ||||||||||||||||||||||||||||
| 106 | api_key => $key, | ||||||||||||||||||||||||||||
| 107 | think_tag_filter => 0, | ||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | This role provides automatic filtering of C<E<lt>thinkE<gt>> tags from LLM | ||||||||||||||||||||||||||||
| 113 | responses. Many reasoning models (DeepSeek R1, QwQ, Hermes with reasoning | ||||||||||||||||||||||||||||
| 114 | enabled) emit chain-of-thought reasoning wrapped in C<E<lt>thinkE<gt>> tags | ||||||||||||||||||||||||||||
| 115 | inline with their response text. This role strips those tags and preserves | ||||||||||||||||||||||||||||
| 116 | the thinking content on the L<Langertha::Response/thinking> attribute. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Composed into L<Langertha::Role::Chat>, so every engine gets it automatically. | ||||||||||||||||||||||||||||
| 119 | The filter handles both closed pairs (C<E<lt>thinkE<gt>...E<lt>/thinkE<gt>>) | ||||||||||||||||||||||||||||
| 120 | and unclosed tags where the model stopped mid-thought. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | For APIs that provide reasoning content natively (DeepSeek C<reasoning_content>, | ||||||||||||||||||||||||||||
| 123 | Anthropic C<thinking> blocks, Gemini C<thought> parts), the thinking is | ||||||||||||||||||||||||||||
| 124 | extracted directly from the API response — no tag filtering needed. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 think_tag | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | The XML tag name used for thinking content. Defaults to C<think>. | ||||||||||||||||||||||||||||
| 129 | Some models may use different tag names (e.g. C<reasoning>). | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head2 think_tag_filter | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | When true, C<E<lt>thinkE<gt>...E<lt>/thinkE<gt>> blocks are stripped from | ||||||||||||||||||||||||||||
| 134 | response text. The thinking content is preserved on the | ||||||||||||||||||||||||||||
| 135 | L<Langertha::Response/thinking> attribute for inspection. Defaults to C<1> | ||||||||||||||||||||||||||||
| 136 | (enabled). Set to C<0> to pass think tags through unmodified. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 filter_think_content | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | my ($filtered_text, $thinking) = $engine->filter_think_content($text); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Strips C<E<lt>thinkE<gt>...E<lt>/thinkE<gt>> blocks from C<$text>. Handles | ||||||||||||||||||||||||||||
| 143 | both closed pairs and unclosed tags (where the model stopped mid-thought). | ||||||||||||||||||||||||||||
| 144 | Returns the filtered text and the extracted thinking content (or C<undef> if | ||||||||||||||||||||||||||||
| 145 | none). Returns the original text unchanged when L</think_tag_filter> is false. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =over | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item * L<Langertha::Response> - Response object with C<thinking> attribute | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =item * L<Langertha::Role::Chat> - Chat role that composes this role | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =item * L<Langertha::Engine::NousResearch> - Engine with C<reasoning> attribute for Hermes models | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =back | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 Issues | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 164 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 IRC | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 183 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||