blib/lib/Langertha/Role/HermesTools.pm

filter: Statementstmt 100% 35 / 35 Branchbran 100% 6 / 6 Conditioncond 82.3% 14 / 17 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 91.8% 68 / 74 CC 10 SCAR 13.0
CC
10
Cov
95%
_hermes_schema_messages
6.2
hermes_extract_content
4.0
__ANON__
2.0
CRAP 3.7
« blib/lib/Langertha/Role/Embedding.pm ↑ Summary blib/lib/Langertha/Role/KeepAlive.pm »
1 package Langertha::Role::HermesTools;
2 # ABSTRACT: Hermes-style tool calling via XML tags
3 33 our $VERSION = '0.503';
4 33 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 33 has hermes_call_tag => (
9 is => 'ro',
10 isa => 'Str',
11 default => 'tool_call',
12 );
13  
14  
15 33 has hermes_response_tag => (
16 is => 'ro',
17 isa => 'Str',
18 default => 'tool_response',
19 );
20  
21  
22 33 has hermes_tool_instructions => (
23 is => 'ro',
24 isa => 'Str',
25 lazy => 1,
26 builder => '_build_hermes_tool_instructions',
27 );
28  
29 174 ▶sub _build_hermes_tool_instructions {
Subroutine _build_hermes_tool_instructions: called CC=1 SCAR=0.0
30 174 return "You are a function calling AI model. You may call one or more"
31 . " functions to assist with the user query. Don't make assumptions"
32 . " about what values to plug into functions.";
33 }
34  
35  
36 33 has hermes_tool_prompt => (
37 is => 'ro',
38 isa => 'Str',
39 lazy => 1,
40 builder => '_build_hermes_tool_prompt',
41 );
42  
43 175 ▶sub _build_hermes_tool_prompt {
Subroutine _build_hermes_tool_prompt: called CC=1 SCAR=0.0
44 175 my ( $self ) = @_;
45 175 my $call_tag = $self->hermes_call_tag;
46 175 my $instructions = $self->hermes_tool_instructions;
47 175 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 19 ▶sub hermes_extract_content {
Subroutine hermes_extract_content: called CC=4 SCAR=13.9
Pod documented
64 19 my ( $self, $data ) = @_;
65 19 ▶ return undef unless $data && $data->{choices} && @{$data->{choices}};
Branch: 1 branch
BranchTrueFalseTotal
unless $data and $data->{"choices"} and @{$data->{"choices"};} 7 12 19
Condition100%6/6
$data and $data->{"choices"} and @{$data->{"choices"};}
!ll&&!rl&&r
5212
$data and $data->{"choices"}
!ll&&!rl&&r
1414
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 12 return $data->{choices}[0]{message}{content};
67 }
68  
69  
70 33 has hermes_schema_prompt => (
71 is => 'ro',
72 isa => 'Str',
73 lazy => 1,
74 builder => '_build_hermes_schema_prompt',
75 );
76  
77 13 ▶sub _build_hermes_schema_prompt {
Subroutine _build_hermes_schema_prompt: called CC=1 SCAR=0.0
78 13 return <<'PROMPT';
79 Answer in JSON that adheres to this JSON schema:
80 <schema>
81 %s
82 </schema>
83 PROMPT
84 }
85  
86  
87 # The hermes wire puts the schema of a json_schema response_format into a
88 # leading system message as well, as _hermes_tool_messages does with the tools
89 # (karr k234). Any other response_format leaves the conversation alone.
90 174 ▶sub _hermes_schema_messages {
Subroutine _hermes_schema_messages: called CC=6 SCAR=18.2
91 174 my ( $self, $conversation, $format ) = @_;
92 return $conversation
93 unless ref $format eq 'HASH' && ( $format->{type} // '' ) eq 'json_schema'
94 174 ▶ && ref $format->{json_schema} eq 'HASH' && ref $format->{json_schema}{schema} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $format eq "HASH" and ($format->{"type"} // "") eq "json_schema" and ref $format->{"json_schema"} eq "HASH" and ref $format->{"json_schema"}{"schema"} eq "HASH" 160 14 174
Condition72%8/11
ref $format eq "HASH" and ($format->{"type"} // "") eq "json_schema" and ref $format->{"json_schema"} eq "HASH" and ref $format->{"json_schema"}{"schema"} eq "HASH"
!ll&&!rl&&r
160014
ref $format eq "HASH" and ($format->{"type"} // "") eq "json_schema" and ref $format->{"json_schema"} eq "HASH"
!ll&&!rl&&r
160014
ref $format eq "HASH" and ($format->{"type"} // "") eq "json_schema"
!ll&&!rl&&r
159114
$format->{"type"} // ""
l!l
150
MC/DC25%1/4
ref $format eq "HASH" and ($format->{"type"} // "") eq "json_schema" and ref $format->{"json_schema"} eq "HASH" and ref $format->{"json_schema"}{"schema"} eq "HASH"
ref $format eq "HASH" $format->{"type"} ref $format->{"json_schema"} eq "HASH" ref $format->{"json_schema"}{"schema"} eq "HASH"
Truth table40%2/5
A and B and C and D
A= ref $format eq "HASH"B= $format->{"type"}C= ref $format->{"json_schema"} eq "HASH"D= ref $format->{"json_schema"}{"schema"} eq "HASH"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
95 14 my $prompt = sprintf( $self->hermes_schema_prompt, $self->encode_json_text( $format->{json_schema}{schema} ) );
96 14 return [ { role => 'system', content => $prompt }, @$conversation ];
97 }
98  
99 # The hermes wire has no tools / tool_choice / parallel_tool_calls body key:
100 # the tools ride the system prompt, which cannot force a tool (karr k234,
101 # ADR 0002). Composing Role::Tools gives the native flags, so this layer-2 rule
102 # clears them for every hermes engine (NousResearch, AKI native; ADR 0016: two
103 # consumers from different parents, so the rule lives on the role). Kept:
104 # tools_hermes, tool_choice_auto (what the prompt says) and tool_choice_none
105 # (chat_f withholds the tools, k231). The rule keys on the resolved tag, not
106 # on the composition (karr k251): a tool_wire_format => 'openai' constructor
107 # override sends the tools natively, so it keeps the native flags from the
108 # role inventory and loses tools_hermes instead. Deleting only, so the order
109 # against an engine's own around engine_capabilities or its model corrections
110 # does not matter.
111 928 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
112 928 my ( $orig, $self, @rest ) = @_;
113 928 my $caps = $self->$orig(@rest);
114 928 ▶ if ( $self->tool_wire_format eq 'hermes' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tool_wire_format eq 'hermes') { } 869 59 928
115 869 delete @{$caps}{qw( tools_native tool_choice_any tool_choice_named parallel_tool_use )};
116 }
117 else {
118 59 delete $caps->{tools_hermes};
119 }
120 928 return $caps;
121 33 };
122  
123 # The tool-format behaviour (format_tools, response_tool_calls,
124 # extract_tool_call, response_text_content, format_tool_results,
125 # build_tool_chat_request) is provided by the tag-driven defaults in
126 # Langertha::Role::Tools for tool_wire_format => 'hermes'. This role now only
127 # carries the Hermes-specific configuration (tag names + prompt template) those
128 # defaults read.
129  
130  
131 33 1;
132  
133 __END__
134  
135 =pod
136  
137 =encoding UTF-8
138  
139 =head1 NAME
140  
141 Langertha::Role::HermesTools - Hermes-style tool calling via XML tags
142  
143 =head1 VERSION
144  
145 version 0.503
146  
147 =head1 SYNOPSIS
148  
149 package Langertha::Engine::MyEngine;
150 use Moose;
151 extends 'Langertha::Engine::Remote';
152  
153 with 'Langertha::Role::Tools';
154 with 'Langertha::Role::HermesTools';
155  
156 sub _build_tool_wire_format { 'hermes' }
157  
158 =head1 DESCRIPTION
159  
160 This role configures Hermes-style tool calling: instead of using an API's
161 native C<tools> parameter, tool definitions are injected into the system prompt
162 as C<E<lt>toolsE<gt>> XML and the model responds with C<E<lt>tool_callE<gt>> XML
163 tags containing JSON. This works with any chat model regardless of native tool
164 API support.
165  
166 The behaviour itself lives in the tag-driven defaults of
167 L<Langertha::Role::Tools> (selected by C<tool_wire_format =E<gt> 'hermes'>). This
168 role now only carries the Hermes-specific I<configuration> those defaults read:
169 the call/response tag names (L</hermes_call_tag>, L</hermes_response_tag>), the
170 prompt template (L</hermes_tool_prompt>), and the response-content extractor
171 (L</hermes_extract_content>). Compose it alongside L<Langertha::Role::Tools> and
172 set C<_build_tool_wire_format> to C<'hermes'>.
173  
174 The C<hermes> wire has no C<tools>, C<tool_choice> or C<parallel_tool_calls>
175 body key, so the role clears C<tools_native>, C<tool_choice_any>,
176 C<tool_choice_named> and C<parallel_tool_use> from
177 L<Langertha::Role::Capabilities/engine_capabilities>; C<tools_hermes>,
178 C<tool_choice_auto> and C<tool_choice_none> stay.
179  
180 =head2 hermes_call_tag
181  
182 hermes_call_tag => 'function_call'
183  
184 The XML tag name used for tool calls in the model's output. Both the prompt
185 template and the response parser use this tag. Defaults to C<tool_call>.
186  
187 =head2 hermes_response_tag
188  
189 hermes_response_tag => 'function_response'
190  
191 The XML tag name used when sending tool results back to the model. Defaults to
192 C<tool_response>.
193  
194 =head2 hermes_tool_instructions
195  
196 hermes_tool_instructions => 'You are a helpful assistant that can call functions.'
197  
198 The instruction text prepended to the Hermes tool system prompt. Customize this
199 to change the model's behavior without altering the structural XML template. The
200 default instructs the model to call functions without making assumptions about
201 argument values.
202  
203 =head2 hermes_tool_prompt
204  
205 The full system prompt template used for Hermes tool calling. Must contain a
206 C<%s> placeholder where the tools JSON will be inserted. Built automatically
207 from L</hermes_tool_instructions> and L</hermes_call_tag>. Override this only
208 if you need full control over the prompt structure.
209  
210 =head2 hermes_extract_content
211  
212 my $content = $self->hermes_extract_content($data);
213  
214 Extracts raw text content from a parsed LLM response for Hermes tool call
215 parsing. Defaults to OpenAI response format (C<choices[0].message.content>).
216 Override this method in engines with non-OpenAI response structures.
217  
218 =head2 hermes_schema_prompt
219  
220 The system prompt template for Hermes structured output, in the form of the
221 Hermes function-calling prompt format. Must contain a C<%s> placeholder where
222 the JSON schema is inserted. L<Langertha::Role::Chat/chat_f> and
223 L<Langertha::Role::Chat/chat_stream_realtime_f> put it in front of the
224 conversation for every C<json_schema> C<response_format> on a C<hermes>
225 engine that takes C<response_format> (including the rewrite of a forced
226 tool), so a backend that ignores C<response_format> still sees the schema.
227  
228 =head1 SEE ALSO
229  
230 =over
231  
232 =item * L<Langertha::Role::Tools> - Base tool calling role
233  
234 =item * L<Langertha::Engine::NousResearch> - Hermes model engine
235  
236 =item * L<Langertha::Engine::AKI> - AKI.IO engine using Hermes tools
237  
238 =back
239  
240 =head1 SUPPORT
241  
242 =head2 Issues
243  
244 Please report bugs and feature requests on GitHub at
245 L<https://github.com/Getty/langertha/issues>.
246  
247 =head2 IRC
248  
249 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
250  
251 =head1 CONTRIBUTING
252  
253 Contributions are welcome! Please fork the repository and submit a pull request.
254  
255 =head1 AUTHOR
256  
257 Torsten Raudssus <getty@cpan.org>
258  
259 =head1 COPYRIGHT AND LICENSE
260  
261 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
262  
263 This is free software; you can redistribute it and/or modify it under
264 the same terms as the Perl 5 programming language system itself.
265  
266 =cut
« blib/lib/Langertha/Role/Embedding.pm ↑ Summary blib/lib/Langertha/Role/KeepAlive.pm »