| 1 | package Langertha::Role::HermesTools; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Hermes-style tool calling via XML tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 35 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 35 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 35 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 35 | has hermes_call_tag => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | default => 'tool_call', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 35 | has hermes_response_tag => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | default => 'tool_response', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 35 | 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 | 35 | 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
Condition100%6/6
$data and $data->{"choices"} and @{$data->{"choices"};}
$data and $data->{"choices"}
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"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 12 | return $data->{choices}[0]{message}{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 35 | 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
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"
ref $format eq "HASH" and ($format->{"type"} // "") eq "json_schema" and ref $format->{"json_schema"} eq "HASH"
ref $format eq "HASH" and ($format->{"type"} // "") eq "json_schema"
$format->{"type"} // ""
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 1084 | ▶ | around engine_capabilities => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 1084 | my ( $orig, $self, @rest ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 1084 | my $caps = $self->$orig(@rest); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 1084 | ▶ | if ( $self->tool_wire_format eq 'hermes' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 1011 | delete @{$caps}{qw( tools_native tool_choice_any tool_choice_named parallel_tool_use )}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 73 | delete $caps->{tools_hermes}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 1084 | return $caps; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 35 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 35 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||