Langertha/Role/HermesTools.pm

filter: Statementstmt 100% 60 / 60 Branchbran 100% 6 / 6 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 95.9% 94 / 98 CC 9 SCAR 8.6
CC
9
Cov
97%
hermes_extract_content
4.0
format_tool_results
3.0
response_tool_calls
3.0
CRAP 2.4
« Langertha/Engine/MiniMaxAnthropic.pm ↑ Summary Langertha/Result.pm »
1 package Langertha::Role::HermesTools;
2 # ABSTRACT: Hermes-style tool calling via XML tags
3 10 our $VERSION = '0.502';
4 10 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 10 has hermes_call_tag => (
9 is => 'ro',
10 isa => 'Str',
11 default => 'tool_call',
12 );
13  
14  
15 10 has hermes_response_tag => (
16 is => 'ro',
17 isa => 'Str',
18 default => 'tool_response',
19 );
20  
21  
22 10 has hermes_tool_instructions => (
23 is => 'ro',
24 isa => 'Str',
25 lazy => 1,
26 builder => '_build_hermes_tool_instructions',
27 );
28  
29 sub _build_hermes_tool_instructions {
30 3 ▶ return "You are a function calling AI model. You may call one or more"
Subroutine _build_hermes_tool_instructions: called CC=1 SCAR=0.0
31 . " functions to assist with the user query. Don't make assumptions"
32 . " about what values to plug into functions.";
33 }
34  
35  
36 10 has hermes_tool_prompt => (
37 is => 'ro',
38 isa => 'Str',
39 lazy => 1,
40 builder => '_build_hermes_tool_prompt',
41 );
42  
43 sub _build_hermes_tool_prompt {
44 4 ▶ my ( $self ) = @_;
Subroutine _build_hermes_tool_prompt: called CC=1 SCAR=0.0
45 4 my $call_tag = $self->hermes_call_tag;
46 4 my $instructions = $self->hermes_tool_instructions;
47 4 return <<"PROMPT";
48 ${instructions}
49  
50 You are provided with function signatures within <tools></tools> XML tags:
51 <tools>
52 %s
53 </tools>
54  
55 For each function call, return a JSON object with function name and arguments within <${call_tag}></${call_tag}> XML tags:
56 <${call_tag}>
57 {"name": "function_name", "arguments": {"arg1": "value1"}}
58 </${call_tag}>
59 PROMPT
60 }
61  
62  
63 sub hermes_extract_content {
64 22 ▶ my ( $self, $data ) = @_;
Subroutine hermes_extract_content: called CC=4 SCAR=13.9
Pod documented
65 22 ▶ return undef unless $data && $data->{choices} && @{$data->{choices}};
Branch: 1 branch
BranchTrueFalseTotal
unless $data and $$data{"choices"} and @{$$data{"choices"};} 4 17 21
Condition100%6/6
$data and $$data{"choices"} and @{$$data{"choices"};}
!ll&&!rl&&r
3217
$data and $$data{"choices"}
!ll&&!rl&&r
1219
MC/DC100%3/3
$data and $$data{"choices"} and @{$$data{"choices"};}
$data $$data{"choices"} @{$$data{"choices"};}
Truth table100%4/4
A and B and C
A= $dataB= $$data{"choices"}C= @{$$data{"choices"};}
ABCresult
0XX0
10X0
1100
1111
66 17 return $data->{choices}[0]{message}{content};
67 }
68  
69  
70 # --- Role::Tools method implementations ---
71  
72 sub format_tools {
73 1 ▶ my ( $self, $tools ) = @_;
Subroutine format_tools: called CC=1 SCAR=0.0
Pod documented
74 1 return $tools;
75 }
76  
77  
78 around build_tool_chat_request => sub {
79 2 ▶ my ( $orig, $self, $conversation, $formatted_tools, %extra ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
80 2 my $tools_json = $self->json->encode($formatted_tools);
81 2 my $tool_prompt = sprintf($self->hermes_tool_prompt, $tools_json);
82 2 my $system_msg = { role => 'system', content => $tool_prompt };
83 2 my @conv = ( $system_msg, @$conversation );
84 2 return $self->chat_request(\@conv, %extra);
85 10 };
86  
87  
88 sub response_tool_calls {
89 11 ▶ my ( $self, $data ) = @_;
Subroutine response_tool_calls: called CC=3 SCAR=11.0
Pod documented
90 11 my $content = $self->hermes_extract_content($data);
91 11 ▶ return [] unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 2 9 11
92  
93 9 my $tag = $self->hermes_call_tag;
94 9 my @tool_calls;
95 9 ▶ while ($content =~ m{<\Q$tag\E>\s*(.*?)\s*</\Q$tag\E>}sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<\Q$tag\E>\s*(.*?)\s*</\Q$tag\E>]gs) 8 9 17
96 8 my $json_str = $1;
97 8 eval {
98 8 my $tc = $self->decode_json_text($json_str);
99 7 push @tool_calls, $tc;
100 };
101 }
102 9 return \@tool_calls;
103 }
104  
105  
106 sub extract_tool_call {
107 1 ▶ my ( $self, $tc ) = @_;
Subroutine extract_tool_call: called CC=1 SCAR=0.0
Pod documented
108 1 return ( $tc->{name}, $tc->{arguments} );
109 }
110  
111  
112 sub response_text_content {
113 4 ▶ my ( $self, $data ) = @_;
Subroutine response_text_content: called CC=2 SCAR=7.0
Pod documented
114 4 ▶ my $content = $self->hermes_extract_content($data) // '';
Condition50%1/2
$self->hermes_extract_content($data) // ""
l!l
40
MC/DC0%0/1
$self->hermes_extract_content($data) // ""
$self->hermes_extract_content($data)
Truth table50%1/2
A
A= $self->hermes_extract_content($data)
Aresult
11
00
115 4 my $tag = $self->hermes_call_tag;
116 4 $content =~ s{<\Q$tag\E>.*?</\Q$tag\E>}{}sg;
117 4 $content =~ s/^\s+|\s+$//g;
118 4 return $content;
119 }
120  
121  
122 sub format_tool_results {
123 3 ▶ my ( $self, $data, $results ) = @_;
Subroutine format_tool_results: called CC=3 SCAR=11.0
Pod documented
124 3 my $content = $self->hermes_extract_content($data);
125 3 my $res_tag = $self->hermes_response_tag;
126  
127 3 my @messages;
128 3 push @messages, { role => 'assistant', content => $content };
129  
130 3 for my $r (@$results) {
131 3 ▶ my $tool_content = join('', map { $_->{text} // '' } @{$r->{result}{content}});
Condition50%1/2
$$_{"text"} // ""
l!l
30
MC/DC0%0/1
$$_{"text"} // ""
$$_{"text"}
Truth table50%1/2
A
A= $$_{"text"}
Aresult
11
00
132 push @messages, {
133 role => 'tool',
134 content => "<${res_tag}>\n"
135 3 . $self->json->encode({ name => $r->{tool_call}{name}, content => $tool_content })
136 . "\n</${res_tag}>",
137 };
138 }
139  
140 3 return @messages;
141 }
142  
143  
144  
145 10 1;
146  
147 __END__
148  
149 =pod
150  
151 =encoding UTF-8
152  
153 =head1 NAME
154  
155 Langertha::Role::HermesTools - Hermes-style tool calling via XML tags
156  
157 =head1 VERSION
158  
159 version 0.502
160  
161 =head1 SYNOPSIS
162  
163 package Langertha::Engine::MyEngine;
164 use Moose;
165 extends 'Langertha::Engine::Remote';
166  
167 with 'Langertha::Role::Tools';
168 with 'Langertha::Role::HermesTools';
169  
170 =head1 DESCRIPTION
171  
172 This role implements tool calling via Hermes-style XML tags. Instead of using
173 an API's native C<tools> parameter, tool definitions are injected into the
174 system prompt as C<E<lt>toolsE<gt>> XML and the model responds with
175 C<E<lt>tool_callE<gt>> XML tags containing JSON. This works with any chat
176 model regardless of native tool API support.
177  
178 Engines composing this role get implementations of the five methods required
179 by L<Langertha::Role::Tools>: L</format_tools>, L</response_tool_calls>,
180 L</extract_tool_call>, L</format_tool_results>, and L</response_text_content>.
181 It also provides L</build_tool_chat_request> to inject tools into the system
182 prompt instead of passing them as an API parameter.
183  
184 =head2 hermes_call_tag
185  
186 hermes_call_tag => 'function_call'
187  
188 The XML tag name used for tool calls in the model's output. Both the prompt
189 template and the response parser use this tag. Defaults to C<tool_call>.
190  
191 =head2 hermes_response_tag
192  
193 hermes_response_tag => 'function_response'
194  
195 The XML tag name used when sending tool results back to the model. Defaults to
196 C<tool_response>.
197  
198 =head2 hermes_tool_instructions
199  
200 hermes_tool_instructions => 'You are a helpful assistant that can call functions.'
201  
202 The instruction text prepended to the Hermes tool system prompt. Customize this
203 to change the model's behavior without altering the structural XML template. The
204 default instructs the model to call functions without making assumptions about
205 argument values.
206  
207 =head2 hermes_tool_prompt
208  
209 The full system prompt template used for Hermes tool calling. Must contain a
210 C<%s> placeholder where the tools JSON will be inserted. Built automatically
211 from L</hermes_tool_instructions> and L</hermes_call_tag>. Override this only
212 if you need full control over the prompt structure.
213  
214 =head2 hermes_extract_content
215  
216 my $content = $self->hermes_extract_content($data);
217  
218 Extracts raw text content from a parsed LLM response for Hermes tool call
219 parsing. Defaults to OpenAI response format (C<choices[0].message.content>).
220 Override this method in engines with non-OpenAI response structures.
221  
222 =head2 format_tools
223  
224 Returns the MCP tool definitions as-is for JSON encoding into the Hermes
225 system prompt.
226  
227 =head2 build_tool_chat_request
228  
229 Builds a chat request with tool definitions injected into the system prompt
230 as XML, rather than passing them as an API parameter.
231  
232 =head2 response_tool_calls
233  
234 Parses C<E<lt>tool_callE<gt>> XML tags from the model's text output and
235 returns an ArrayRef of tool call HashRefs with C<name> and C<arguments>.
236  
237 =head2 extract_tool_call
238  
239 Extracts tool name and arguments from a Hermes tool call HashRef.
240  
241 =head2 response_text_content
242  
243 Extracts the final text content from the response, stripping any
244 C<E<lt>tool_callE<gt>> XML tags.
245  
246 =head2 format_tool_results
247  
248 Formats tool execution results as C<E<lt>tool_responseE<gt>> XML messages
249 for the next conversation turn.
250  
251 =head1 SEE ALSO
252  
253 =over
254  
255 =item * L<Langertha::Role::Tools> - Base tool calling role
256  
257 =item * L<Langertha::Engine::NousResearch> - Hermes model engine
258  
259 =item * L<Langertha::Engine::AKI> - AKI.IO engine using Hermes tools
260  
261 =back
262  
263 =head1 SUPPORT
264  
265 =head2 Issues
266  
267 Please report bugs and feature requests on GitHub at
268 L<https://github.com/Getty/langertha/issues>.
269  
270 =head2 IRC
271  
272 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
273  
274 =head1 CONTRIBUTING
275  
276 Contributions are welcome! Please fork the repository and submit a pull request.
277  
278 =head1 AUTHOR
279  
280 Torsten Raudssus <getty@cpan.org>
281  
282 =head1 COPYRIGHT AND LICENSE
283  
284 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
285  
286 This is free software; you can redistribute it and/or modify it under
287 the same terms as the Perl 5 programming language system itself.
288  
289 =cut
« Langertha/Engine/MiniMaxAnthropic.pm ↑ Summary Langertha/Result.pm »