| 1 | package Langertha::Engine::NousResearch; | ||||||||||||||||||||
| 2 | # ABSTRACT: Nous Research Inference API | ||||||||||||||||||||
| 3 | 7 | our $VERSION = '0.502'; | |||||||||||||||||||
| 4 | 7 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 7 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 7 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 7 | with 'Langertha::Role::Tools', 'Langertha::Role::HermesTools', 'Langertha::Role::StaticModels'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 3 | ▶ | sub _build_supported_operations {[qw( | ||||||||||||||||||
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | createChatCompletion | ||||||||||||||||||||
| 14 | )]} | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | has '+url' => ( | ||||||||||||||||||||
| 17 | lazy => 1, | ||||||||||||||||||||
| 18 | 2 | ▶ | default => sub { 'https://inference-api.nousresearch.com/v1' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 7 | ); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | sub _build_api_key { | ||||||||||||||||||||
| 22 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=8.1
| |||||||||||||||||||||
| 23 | return $ENV{LANGERTHA_NOUSRESEARCH_API_KEY} | ||||||||||||||||||||
| 24 | 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')
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')
| |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 2 | ▶ | sub default_model { 'Hermes-4-70B' } | ||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | sub _build_static_models {[ | ||||||||||||||||||||
| 30 | 1 | ▶ | { id => 'Hermes-4-70B' }, | ||||||||||||||||||
|
Subroutine
_build_static_models: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | { id => 'Hermes-4-405B' }, | ||||||||||||||||||||
| 32 | ]} | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 7 | has reasoning => ( | |||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||
| 36 | isa => 'Bool', | ||||||||||||||||||||
| 37 | default => 0, | ||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 7 | my $_default_reasoning_prompt = <<'END_REASONING_PROMPT'; | |||||||||||||||||||
| 42 | You are a deep thinking AI, you may use extremely long chains of thought to | ||||||||||||||||||||
| 43 | deeply consider the problem and deliberate with yourself via systematic | ||||||||||||||||||||
| 44 | reasoning processes to help come to a correct solution prior to answering. | ||||||||||||||||||||
| 45 | You should enclose your thoughts and internal monologue inside <think> </think> | ||||||||||||||||||||
| 46 | tags, and then provide your solution or response to the problem. | ||||||||||||||||||||
| 47 | END_REASONING_PROMPT | ||||||||||||||||||||
| 48 | 7 | chomp $_default_reasoning_prompt; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | has reasoning_prompt => ( | ||||||||||||||||||||
| 51 | is => 'ro', | ||||||||||||||||||||
| 52 | isa => 'Str', | ||||||||||||||||||||
| 53 | lazy => 1, | ||||||||||||||||||||
| 54 | 2 | ▶ | default => sub { $_default_reasoning_prompt }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | 7 | ); | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | around chat_messages => sub { | ||||||||||||||||||||
| 59 | 5 | ▶ | my ( $orig, $self, @messages ) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 60 | 5 | my $msgs = $self->$orig(@messages); | |||||||||||||||||||
| 61 | 5 | ▶ | return $msgs unless $self->reasoning; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | # Prepend reasoning prompt as first system message | ||||||||||||||||||||
| 63 | 2 | unshift @$msgs, { role => 'system', content => $self->reasoning_prompt }; | |||||||||||||||||||
| 64 | 2 | return $msgs; | |||||||||||||||||||
| 65 | 7 | }; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 7 | 1; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | __END__ | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =pod | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =encoding UTF-8 | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 NAME | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Langertha::Engine::NousResearch - Nous Research Inference API | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head1 VERSION | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | version 0.502 | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | use Langertha::Engine::NousResearch; | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | my $nous = Langertha::Engine::NousResearch->new( | ||||||||||||||||||||
| 91 | api_key => $ENV{NOUSRESEARCH_API_KEY}, | ||||||||||||||||||||
| 92 | model => 'Hermes-4-70B', | ||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | print $nous->simple_chat('Explain the Hermes prompt format'); | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # Chain-of-thought reasoning (Hermes 4) | ||||||||||||||||||||
| 98 | my $nous = Langertha::Engine::NousResearch->new( | ||||||||||||||||||||
| 99 | api_key => $ENV{NOUSRESEARCH_API_KEY}, | ||||||||||||||||||||
| 100 | model => 'Hermes-4-70B', | ||||||||||||||||||||
| 101 | reasoning => 1, | ||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | my $response = $nous->simple_chat('Solve this step by step...'); | ||||||||||||||||||||
| 105 | say $response; # clean answer | ||||||||||||||||||||
| 106 | say $response->thinking; # chain-of-thought reasoning | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # MCP tool calling (via HermesTools role) | ||||||||||||||||||||
| 109 | use Future::AsyncAwait; | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | my $nous = Langertha::Engine::NousResearch->new( | ||||||||||||||||||||
| 112 | api_key => $ENV{NOUSRESEARCH_API_KEY}, | ||||||||||||||||||||
| 113 | model => 'Hermes-4-70B', | ||||||||||||||||||||
| 114 | mcp_servers => [$mcp], | ||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | my $response = await $nous->chat_with_tools_f('Add 7 and 15'); | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =head1 DESCRIPTION | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | Provides access to Nous Research's inference API. Composes | ||||||||||||||||||||
| 122 | L<Langertha::Role::OpenAICompatible> with Nous's endpoint | ||||||||||||||||||||
| 123 | (C<https://inference-api.nousresearch.com/v1>) and Hermes tool calling. | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | Available models: C<Hermes-4-70B> (default), C<Hermes-4-405B>. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | Composes L<Langertha::Role::HermesTools> for tool calling. Tool descriptions | ||||||||||||||||||||
| 128 | are injected into the system prompt as C<< <tools> >> XML, and | ||||||||||||||||||||
| 129 | C<< <tool_call> >> tags are parsed from the model output. No server-side tool | ||||||||||||||||||||
| 130 | calling support required. See L<Langertha::Role::HermesTools> for | ||||||||||||||||||||
| 131 | customization options. | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | Get your API key at L<https://portal.nousresearch.com/> and set | ||||||||||||||||||||
| 134 | C<LANGERTHA_NOUSRESEARCH_API_KEY>. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =head2 reasoning | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | reasoning => 1 | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Enable chain-of-thought reasoning for Hermes 4 and DeepHermes 3 models. | ||||||||||||||||||||
| 143 | Prepends the standard Nous reasoning system prompt that instructs the model | ||||||||||||||||||||
| 144 | to use C<E<lt>thinkE<gt>> tags. The thinking content is automatically | ||||||||||||||||||||
| 145 | extracted into L<Langertha::Response/thinking> by L<Langertha::Role::ThinkTag>. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | With DeepHermes 3, reasoning output appears inline as C<E<lt>thinkE<gt>> tags | ||||||||||||||||||||
| 148 | (handled by the think tag filter). With Hermes 4 (without response prefill), | ||||||||||||||||||||
| 149 | reasoning appears in the C<reasoning_content> response field (handled by | ||||||||||||||||||||
| 150 | native extraction). | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | Defaults to C<0> (disabled). | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head2 reasoning_prompt | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | The system prompt prepended when C<reasoning> is enabled. Defaults to the | ||||||||||||||||||||
| 157 | standard Nous Research reasoning prompt from the Hermes model documentation. | ||||||||||||||||||||
| 158 | Unless you have a specific technical reason (e.g. a different model requires | ||||||||||||||||||||
| 159 | a different trigger format), it is strongly recommended to keep the default. | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head1 SEE ALSO | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =over | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =item * L<https://nousresearch.com/> - Nous Research homepage | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =item * L<https://portal.nousresearch.com/api-docs> - API documentation | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =item * L<Langertha::Role::HermesTools> - Hermes-style tool calling via XML tags | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =back | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =head1 SUPPORT | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =head2 Issues | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||
| 180 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =head2 IRC | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head1 CONTRIBUTING | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =head1 AUTHOR | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 199 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||