| 1 | package Langertha::Engine::NousResearch; | ||||||||||||||
| 2 | # ABSTRACT: Nous Research Inference API | ||||||||||||||
| 3 | 28 | our $VERSION = '0.505'; | |||||||||||||
| 4 | 28 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 28 | ▶ | use Carp qw( croak ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 28 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | 28 | 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 | 297 | ▶ | sub _is_hermes_model { | ||||||||||||
|
Subroutine
_is_hermes_model: called CC=2 SCAR=6.9
| |||||||||||||||
| 24 | 297 | my ( $self ) = @_; | |||||||||||||
| 25 | 297 | ▶ | return $self->chat_model =~ m{\A(?:nousresearch/)?(?:nous-)?(?:deep)?hermes}i ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | 280 | ▶ | sub _build_tool_wire_format { | ||||||||||||
|
Subroutine
_build_tool_wire_format: called CC=2 SCAR=6.9
| |||||||||||||||
| 32 | 280 | my ( $self ) = @_; | |||||||||||||
| 33 | 280 | ▶ | return $self->_is_hermes_model ? 'hermes' : 'openai'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | |||||||||||||||
| 37 | 208 | ▶ | 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 | 28 | ); | |||||||||||||
| 45 | |||||||||||||||
| 46 | 1 | ▶ | sub _build_api_key { | ||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=7.2
| |||||||||||||||
| 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"; | ||||||||||||
|
Condition50%1/2
$ENV{'LANGERTHA_NOUSRESEARCH_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_NOUSRESEARCH_API_KEY or api_key set')
MC/DC0%0/1
$ENV{'LANGERTHA_NOUSRESEARCH_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_NOUSRESEARCH_API_KEY or api_key set')
$ENV{'LANGERTHA_NOUSRESEARCH_API_KEY'}
Truth table50%1/2
A
A= $ENV{'LANGERTHA_NOUSRESEARCH_API_KEY'}
| |||||||||||||||
| 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 | 28 | has reasoning => ( | |||||||||||||
| 61 | is => 'ro', | ||||||||||||||
| 62 | isa => 'Bool', | ||||||||||||||
| 63 | default => 0, | ||||||||||||||
| 64 | ); | ||||||||||||||
| 65 | |||||||||||||||
| 66 | |||||||||||||||
| 67 | 28 | 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 | 28 | 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 | 28 | ); | |||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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 | 28 | }; | |||||||||||||
| 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
| |||||||||||||||
| 112 | 22 | return %decision; | |||||||||||||
| 113 | 28 | }; | |||||||||||||
| 114 | |||||||||||||||
| 115 | 1054 | ▶ | around engine_capabilities => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 116 | 1054 | my ( $orig, $self, @rest ) = @_; | |||||||||||||
| 117 | 1054 | my $caps = $self->$orig(@rest); | |||||||||||||
| 118 | # image_input (k266, ADR 0019): vision is undocumented / unverified on this face, so no claim. | ||||||||||||||
| 119 | 1054 | delete $caps->{image_input}; | |||||||||||||
| 120 | 1054 | return $caps; | |||||||||||||
| 121 | 28 | }; | |||||||||||||
| 122 | |||||||||||||||
| 123 | 28 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 124 | |||||||||||||||
| 125 | |||||||||||||||
| 126 | 28 | 1; | |||||||||||||
| 127 | |||||||||||||||
| 128 | __END__ | ||||||||||||||