blib/lib/Langertha/Engine/NousResearch.pm

filter: Statementstmt 100% 45 / 45 Branchbran 100% 12 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 94.7% 72 / 76 CC 8 SCAR 6.6
CC
8
Cov
97%
__ANON__
3.0
__ANON__
3.0
_build_api_key
2.3
CRAP 1.9
« blib/lib/Langertha/Spec/OpenAI.pm ↑ Summary blib/lib/Langertha/Role/ImageGeneration.pm »
1 package Langertha::Engine::NousResearch;
2 # ABSTRACT: Nous Research Inference API
3 26 our $VERSION = '0.503';
4 26 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 extends 'Langertha::Engine::OpenAIBase';
8  
9 26 with 'Langertha::Role::Tools', 'Langertha::Role::HermesTools', 'Langertha::Role::StaticModels';
10  
11 # karr k238 (ADR 0033, amends 0001/0002/0019): hermes is not the endpoint
12 # dialect, it is the wire for Hermes models only. NousResearch is an
13 # OpenAI-compatible gateway fronting ~341 models; a non-Hermes slug
14 # (anthropic/..., openai/..., ...) routes to a real backend that takes native
15 # OpenAI tools, so hermes there would be wrong. Hermes models want the hermes
16 # wire -- tools ride the system prompt and <tool_call> tags are lifted onto
17 # Response.tool_calls -- which is the robust choice given upstream
18 # hermes-agent#741, where Hermes-4 intermittently emits tool calls as
19 # <tool_call> XML / bare JSON even with native calling wired up. So Hermes is
20 # the named exception; native is the default (safer for the 280+ non-Hermes
21 # slugs and any unknown new one). Matches Hermes-4/-4.3, Hermes-3, DeepHermes,
22 # the nousresearch/ prefix, lowercase, and the legacy Nous-Hermes-2 form.
23 291 ▶sub _is_hermes_model {
Subroutine _is_hermes_model: called CC=2 SCAR=6.9
24 291 my ( $self ) = @_;
25 291 ▶ return $self->chat_model =~ m{\A(?:nousresearch/)?(?:nous-)?(?:deep)?hermes}i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->chat_model =~ m[\A(?:nousresearch/)?(?:nous-)?(?:deep)?hermes]i ? : 269 22 291
26 }
27  
28 # Resolved once per instance from chat_model (default_model is 'Hermes-4-70B',
29 # so reading it never croaks). A tool_wire_format => ... constructor arg wins
30 # via the init_arg (Role::Tools), so a user can force either wire on any slug.
31 274 ▶sub _build_tool_wire_format {
Subroutine _build_tool_wire_format: called CC=2 SCAR=6.9
32 274 my ( $self ) = @_;
33 274 ▶ return $self->_is_hermes_model ? 'hermes' : 'openai';
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_hermes_model ? : 255 19 274
34 }
35  
36  
37 207 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
38 createChatCompletion
39 )]}
40  
41 has '+url' => (
42 lazy => 1,
43 198 ▶ default => sub { 'https://inference-api.nousresearch.com/v1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 26 );
45  
46 1 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=8.1
47 1 my ( $self ) = @_;
48 return $ENV{LANGERTHA_NOUSRESEARCH_API_KEY}
49 1 ▶ || croak "".(ref $self)." requires LANGERTHA_NOUSRESEARCH_API_KEY or api_key set";
Condition33%1/3
$ENV{'LANGERTHA_NOUSRESEARCH_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_NOUSRESEARCH_API_KEY or api_key set')
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'LANGERTHA_NOUSRESEARCH_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_NOUSRESEARCH_API_KEY or api_key set')
$ENV{'LANGERTHA_NOUSRESEARCH_API_KEY'} croak('' . ref($self) . ' requires LANGERTHA_NOUSRESEARCH_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_NOUSRESEARCH_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_NOUSRESEARCH_API_KEY or api_key set')
ABresult
1X1
011
000
50 }
51  
52 41 ▶sub default_model { 'Hermes-4-70B' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
53  
54 1 ▶sub _build_static_models {[
Subroutine _build_static_models: called CC=1 SCAR=0.0
55 1 { id => 'Hermes-4-70B' },
56 { id => 'Hermes-4-405B' },
57 { id => 'Hermes-4.3-36B' },
58 ]}
59  
60 26 has reasoning => (
61 is => 'ro',
62 isa => 'Bool',
63 default => 0,
64 );
65  
66  
67 26 my $_default_reasoning_prompt = <<'END_REASONING_PROMPT';
68 You are a deep thinking AI, you may use extremely long chains of thought to
69 deeply consider the problem and deliberate with yourself via systematic
70 reasoning processes to help come to a correct solution prior to answering.
71 You should enclose your thoughts and internal monologue inside <think> </think>
72 tags, and then provide your solution or response to the problem.
73 END_REASONING_PROMPT
74 26 chomp $_default_reasoning_prompt;
75  
76 has reasoning_prompt => (
77 is => 'ro',
78 isa => 'Str',
79 lazy => 1,
80 9 ▶ default => sub { $_default_reasoning_prompt },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 26 );
82  
83  
84 # Prepend the reasoning prompt as the first system message. Wrapping
85 # _system_messages (not chat_messages) makes Langertha::Chat send it too,
86 # also when the wrapper brings its own system prompt (karr k277).
87 208 ▶around _system_messages => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
88 208 my ( $orig, $self, @override ) = @_;
89 208 my @system = $self->$orig(@override);
90 208 ▶ return @system unless $self->reasoning;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->reasoning 197 11 208
91 # The Nous reasoning prompt is a Hermes-model feature (karr k238, ADR 0033):
92 # gate it on _is_hermes_model, the same predicate _build_tool_wire_format
93 # uses, NOT on tool_wire_format. This decouples the reasoning prompt from the
94 # tool transport, so forcing tool_wire_format => 'openai' on a genuine Hermes
95 # model still gets reasoning. On a non-Hermes slug the prompt is meaningless,
96 # so drop it with one warning.
97 11 ▶ unless ( $self->_is_hermes_model ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_hermes_model) 1 10 11
98 1 $self->_langertha_carp( "".( ref $self ).": reasoning => 1 is ignored -- the Nous"
99 . " reasoning prompt is a Hermes-model feature and '" . $self->chat_model
100 . "' is not a Hermes model", 'nous_reasoning_non_hermes' );
101 1 return @system;
102 }
103 10 return ( { role => 'system', content => $self->reasoning_prompt }, @system );
104 26 };
105  
106 # The reasoning prompt follows chat_model too, so a per-request model that
107 # crosses the Hermes line flips it (karr k352, Role::Chat::_warn_model_override).
108 22 ▶around _model_scoped_wire_decisions => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
109 22 my ( $orig, $self, @args ) = @_;
110 22 my %decision = $self->$orig(@args);
111 22 ▶ $decision{'reasoning prompt'} = $self->_is_hermes_model ? 1 : 0 if $self->reasoning;
Branch: 2 branches
BranchTrueFalseTotal
if $self->reasoning 6 16 22
$self->_is_hermes_model ? : 4 2 6
112 22 return %decision;
113 26 };
114  
115 908 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
116 908 my ( $orig, $self, @rest ) = @_;
117 908 my $caps = $self->$orig(@rest);
118 # image_input (k266, ADR 0019): vision is undocumented / unverified on this face, so no claim.
119 908 delete $caps->{image_input};
120 908 return $caps;
121 26 };
122  
123 26 __PACKAGE__->meta->make_immutable;
124  
125  
126 26 1;
127  
128 __END__
129  
130 =pod
131  
132 =encoding UTF-8
133  
134 =head1 NAME
135  
136 Langertha::Engine::NousResearch - Nous Research Inference API
137  
138 =head1 VERSION
139  
140 version 0.503
141  
142 =head1 SYNOPSIS
143  
144 use Langertha::Engine::NousResearch;
145  
146 my $nous = Langertha::Engine::NousResearch->new(
147 api_key => $ENV{NOUSRESEARCH_API_KEY},
148 model => 'Hermes-4-70B',
149 );
150  
151 print $nous->simple_chat('Explain the Hermes prompt format');
152  
153 # Chain-of-thought reasoning (Hermes 4)
154 my $nous = Langertha::Engine::NousResearch->new(
155 api_key => $ENV{NOUSRESEARCH_API_KEY},
156 model => 'Hermes-4-70B',
157 reasoning => 1,
158 );
159  
160 my $response = $nous->simple_chat('Solve this step by step...');
161 say $response; # clean answer
162 say $response->thinking; # chain-of-thought reasoning
163  
164 # MCP tool calling (via HermesTools role)
165 use Future::AsyncAwait;
166  
167 my $nous = Langertha::Engine::NousResearch->new(
168 api_key => $ENV{NOUSRESEARCH_API_KEY},
169 model => 'Hermes-4-70B',
170 mcp_servers => [$mcp],
171 );
172  
173 my $response = await $nous->chat_with_tools_f('Add 7 and 15');
174  
175 =head1 DESCRIPTION
176  
177 Provides access to Nous Research's inference API. Composes
178 L<Langertha::Role::OpenAICompatible> with Nous's endpoint
179 (C<https://inference-api.nousresearch.com/v1>) and Hermes tool calling.
180  
181 Available models: C<Hermes-4-70B> (default), C<Hermes-4-405B>,
182 C<Hermes-4.3-36B>.
183  
184 The endpoint is a gateway that also routes many non-Hermes models (Claude,
185 GPT, Gemini, ...) to their real backends. The tool wire is therefore chosen
186 B<per model>: a Hermes model (C<Hermes-*>, C<DeepHermes-*>) uses the Hermes
187 prompt format, and every other slug uses native OpenAI C<tools>. Override
188 C<tool_wire_format> (C<hermes> | C<openai>) to force either wire on any slug.
189  
190 For a Hermes model, L<Langertha::Role::HermesTools> injects tool descriptions
191 into the system prompt as C<< <tools> >> XML and parses C<< <tool_call> >> tags
192 from the model output; no server-side tool calling is required. This is also
193 the more robust wire for Hermes 4, which intermittently emits its tool calls
194 as C<< <tool_call> >> text even when native calling is offered. The prompt
195 cannot force a tool, so on the Hermes wire the engine does not claim
196 C<tool_choice_named>: L<Langertha::Role::Chat/chat_f> answers a forced tool
197 with a C<json_schema> C<response_format> (the schema also goes into the system
198 prompt) and puts the parsed reply on L<Langertha::Response/tool_calls> as a
199 synthetic call. For a non-Hermes slug the tools and a forced C<tool_choice> go
200 out natively.
201  
202 Get your API key at L<https://portal.nousresearch.com/> and set
203 C<LANGERTHA_NOUSRESEARCH_API_KEY>.
204  
205 B<THIS API IS WORK IN PROGRESS>
206  
207 =head2 reasoning
208  
209 reasoning => 1
210  
211 Enable chain-of-thought reasoning for Hermes 4 and DeepHermes 3 models.
212 Prepends the standard Nous reasoning system prompt that instructs the model
213 to use C<E<lt>thinkE<gt>> tags. The thinking content is automatically
214 extracted into L<Langertha::Response/thinking> by L<Langertha::Role::ThinkTag>.
215 This is a Hermes-model feature: on a non-Hermes slug the prompt is ignored
216 with a warning (the gate is the model, not C<tool_wire_format>, so forcing the
217 C<openai> tool wire on a genuine Hermes model still enables reasoning).
218  
219 With DeepHermes 3, reasoning output appears inline as C<E<lt>thinkE<gt>> tags
220 (handled by the think tag filter). With Hermes 4 (without response prefill),
221 reasoning appears in the C<reasoning_content> response field (handled by
222 native extraction).
223  
224 Defaults to C<0> (disabled).
225  
226 =head2 reasoning_prompt
227  
228 The system prompt prepended when C<reasoning> is enabled. Defaults to the
229 standard Nous Research reasoning prompt from the Hermes model documentation.
230 Unless you have a specific technical reason (e.g. a different model requires
231 a different trigger format), it is strongly recommended to keep the default.
232  
233 =head1 SEE ALSO
234  
235 =over
236  
237 =item * L<https://nousresearch.com/> - Nous Research homepage
238  
239 =item * L<https://portal.nousresearch.com/api-docs> - API documentation
240  
241 =item * L<Langertha::Role::HermesTools> - Hermes-style tool calling via XML tags
242  
243 =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role
244  
245 =back
246  
247 =head1 SUPPORT
248  
249 =head2 Issues
250  
251 Please report bugs and feature requests on GitHub at
252 L<https://github.com/Getty/langertha/issues>.
253  
254 =head2 IRC
255  
256 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
257  
258 =head1 CONTRIBUTING
259  
260 Contributions are welcome! Please fork the repository and submit a pull request.
261  
262 =head1 AUTHOR
263  
264 Torsten Raudssus <getty@cpan.org>
265  
266 =head1 COPYRIGHT AND LICENSE
267  
268 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
269  
270 This is free software; you can redistribute it and/or modify it under
271 the same terms as the Perl 5 programming language system itself.
272  
273 =cut
« blib/lib/Langertha/Spec/OpenAI.pm ↑ Summary blib/lib/Langertha/Role/ImageGeneration.pm »