| 1 | package Langertha::Engine::OpenAI; | ||||||||||||||||||||||
| 2 | # ABSTRACT: OpenAI API | ||||||||||||||||||||||
| 3 | 110 | our $VERSION = '0.505'; | |||||||||||||||||||||
| 4 | 110 | ▶ | use Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 110 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 110 | ▶ | use Langertha::Engine::TranscriptionBase; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 110 | ▶ | use Langertha::Reasoning::Profile; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 110 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | # StreamUsage: stream_options.include_usage is in the chat completions API | ||||||||||||||||||||||
| 12 | # reference (ADR 0038). | ||||||||||||||||||||||
| 13 | 110 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||||
| 14 | Embedding | ||||||||||||||||||||||
| 15 | Transcription | ||||||||||||||||||||||
| 16 | ImageGeneration | ||||||||||||||||||||||
| 17 | Tools | ||||||||||||||||||||||
| 18 | StreamUsage | ||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | 110 | has compatibility_for_engine => ( | |||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||
| 24 | predicate => 'has_compatibility_for_engine', | ||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | has '+url' => ( | ||||||||||||||||||||||
| 29 | lazy => 1, | ||||||||||||||||||||||
| 30 | 272 | ▶ | default => sub { 'https://api.openai.com/v1' }, | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 31 | 110 | ); | |||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | 1 | ▶ | sub _build_api_key { | ||||||||||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=10.9
| |||||||||||||||||||||||
| 34 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||
|
Subroutine
_build_api_key: not called CC=2 SCAR=10.9
| |||||||||||||||||||||||
| 35 | return $ENV{LANGERTHA_OPENAI_API_KEY} | ||||||||||||||||||||||
| 36 | 0 | ▶ | || croak "".(ref $self)." requires LANGERTHA_OPENAI_API_KEY or api_key set"; | ||||||||||||||||||||
|
Condition25%1/4
$ENV{'LANGERTHA_OPENAI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
$ENV{'LANGERTHA_OPENAI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
MC/DC0%0/1
$ENV{'LANGERTHA_OPENAI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
$ENV{'LANGERTHA_OPENAI_API_KEY'}
MC/DC0%0/1
$ENV{'LANGERTHA_OPENAI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
$ENV{'LANGERTHA_OPENAI_API_KEY'}
Truth table50%1/2
A
A= $ENV{'LANGERTHA_OPENAI_API_KEY'}
Truth table0%0/2
A
A= $ENV{'LANGERTHA_OPENAI_API_KEY'}
| |||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | 67 | ▶ | sub default_model { 'gpt-5.6-terra' } | ||||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | # OpenAI removes whisper-1 on 2027-02-26; its successor gpt-transcribe exists | ||||||||||||||||||||||
| 42 | # only on OpenAI, so only this engine (and its whisper handle) defaults to it | ||||||||||||||||||||||
| 43 | # (k308, k313). | ||||||||||||||||||||||
| 44 | 30 | ▶ | sub default_transcription_model { 'gpt-transcribe' } | ||||||||||||||||||||
|
Subroutine
default_transcription_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | # image_input (k266, ADR 0019 k266 Update): every modern OpenAI chat model is a | ||||||||||||||||||||||
| 47 | # vision model (llm-advisor, docs only, 2026-09-25), so the family keeps the | ||||||||||||||||||||||
| 48 | # role-derived flag and only the text-only legacy / non-chat ids clear it. | ||||||||||||||||||||||
| 49 | # OpenAIResponses inherits this table. | ||||||||||||||||||||||
| 50 | 2056 | ▶ | sub model_capability_corrections { | ||||||||||||||||||||
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 51 | return ( | ||||||||||||||||||||||
| 52 | 2056 | qr/\A(?:gpt-3\.5|o1-mini|o3-mini|text-embedding|whisper|tts-|dall-e|gpt-image|gpt-realtime|gpt-audio)/ | |||||||||||||||||||||
| 53 | => { image_input => 0 }, | ||||||||||||||||||||||
| 54 | qr/-(?:audio|realtime|transcribe|tts)(?:-|\z)/ => { image_input => 0 }, | ||||||||||||||||||||||
| 55 | 'gpt-4' => { image_input => 0 }, | ||||||||||||||||||||||
| 56 | qr/\Agpt-4-0(?:314|613)/ => { image_input => 0 }, | ||||||||||||||||||||||
| 57 | qr/\Agpt-4-32k/ => { image_input => 0 }, | ||||||||||||||||||||||
| 58 | qr/\Agpt-4-(?:\d{4}-preview|turbo-preview)\z/ => { image_input => 0 }, | ||||||||||||||||||||||
| 59 | qr/\Ao1-preview/ => { image_input => 0 }, | ||||||||||||||||||||||
| 60 | qr/\Agpt-oss/ => { image_input => 0 }, | ||||||||||||||||||||||
| 61 | qr/\A(?:davinci|babbage)/ => { image_input => 0 }, | ||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||
| 64 | |||||||||||||||||||||||
| 65 | # The completion-length body key diverges by OpenAI model within the shared | ||||||||||||||||||||||
| 66 | # openai wire format: the gpt-5.x and gpt-6 reasoning lines dropped max_tokens | ||||||||||||||||||||||
| 67 | # entirely — gpt-5.x verified live 2026-08-13 (gpt-5.1 / gpt-5.6-terra reject it | ||||||||||||||||||||||
| 68 | # with HTTP 400 "Use 'max_completion_tokens' instead"); gpt-6 confirmed against | ||||||||||||||||||||||
| 69 | # the Chat Completions API reference (max_tokens "is now deprecated in favor of | ||||||||||||||||||||||
| 70 | # max_completion_tokens, and is not compatible with o-series models", extended | ||||||||||||||||||||||
| 71 | # to the gpt-5 / gpt-6 reasoning family — developers.openai.com, 2026-09-14) — | ||||||||||||||||||||||
| 72 | # while gpt-4.x / gpt-4o still accept it. Match the gpt-5 and gpt-6 families by | ||||||||||||||||||||||
| 73 | # anchored prefix, so gpt-5, gpt-5.1, gpt-5.6-*, gpt-6-astra etc. all hit the | ||||||||||||||||||||||
| 74 | # new key; unknown future ids keep the old key (the safe default for every | ||||||||||||||||||||||
| 75 | # other engine in the OpenAI-compatible family). | ||||||||||||||||||||||
| 76 | 17 | ▶ | sub _max_tokens_key { | ||||||||||||||||||||
|
Subroutine
_max_tokens_key: called CC=4 SCAR=14.3
| |||||||||||||||||||||||
| 77 | 17 | my ( $self ) = @_; | |||||||||||||||||||||
| 78 | 17 | ▶ | my $model = $self->can('chat_model') ? ( $self->chat_model // '' ) : ''; | ||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->chat_model // ""
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
| |||||||||||||||||||||||
| 79 | 17 | ▶ | return $model =~ /\Agpt-[56]/ ? 'max_completion_tokens' : 'max_tokens'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | # k155: OpenAI reasoning models 400 on a non-default temperature while reasoning | ||||||||||||||||||||||
| 83 | # is active -- "Unsupported value: 'temperature' does not support 0.7 with this | ||||||||||||||||||||||
| 84 | # model. Only the default (1) value is supported." (live-verified 2026-09-17 | ||||||||||||||||||||||
| 85 | # against /v1/chat/completions on gpt-5.6-terra + gpt-5.6). The rejection is | ||||||||||||||||||||||
| 86 | # EFFORT-AWARE, not a flat per-model capability clear: at reasoning_effort=none | ||||||||||||||||||||||
| 87 | # (where the model accepts it) the same call returns 200, so clearing the | ||||||||||||||||||||||
| 88 | # temperature capability wholesale would wrongly drop the valid effort=none path. | ||||||||||||||||||||||
| 89 | # It also fires on the NO-EFFORT path for a model whose server-side default effort | ||||||||||||||||||||||
| 90 | # is a reasoning level -- so this predicate resolves the effort INCLUDING that | ||||||||||||||||||||||
| 91 | # default, via the Profile's default_reasoning_off signal (k185): the | ||||||||||||||||||||||
| 92 | # gpt-5.1/5.2/5.4 line defaults to reasoning-OFF (reasoning_tokens=0 with no | ||||||||||||||||||||||
| 93 | # effort) and keeps a non-default temperature there, while gpt-5.5/5.6, gpt-6, the | ||||||||||||||||||||||
| 94 | # o-series and legacy gpt-5 default to reasoning-ON. It is consumed READ-ONLY by | ||||||||||||||||||||||
| 95 | # the shared _temperature_kwargs gate in Role::OpenAICompatible / | ||||||||||||||||||||||
| 96 | # Role::ResponsesCompatible; OpenAIResponses inherits it and runs on the | ||||||||||||||||||||||
| 97 | # 'responses' wire. Non-reasoning OpenAI models (gpt-4o, gpt-4.1, gpt-5-chat, | ||||||||||||||||||||||
| 98 | # gpt-5.N-chat, any unknown id) are classified by the Profile and keep their | ||||||||||||||||||||||
| 99 | # temperature; every other OpenAI-compatible engine never reaches this predicate | ||||||||||||||||||||||
| 100 | # (they do not define it). | ||||||||||||||||||||||
| 101 | 260 | ▶ | sub _temperature_rejected_by_reasoning { | ||||||||||||||||||||
|
Subroutine
_temperature_rejected_by_reasoning: called CC=9 SCAR=22.1
| |||||||||||||||||||||||
| 102 | 260 | my ( $self, $controls ) = @_; | |||||||||||||||||||||
| 103 | 260 | ▶ | my $model = $self->can('chat_model') ? ( $self->chat_model // '' ) : ''; | ||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->chat_model // ""
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
| |||||||||||||||||||||||
| 104 | 260 | my $profile = Langertha::Reasoning::Profile->for_model($model); | |||||||||||||||||||||
| 105 | # Which OpenAI models HAVE reasoning (and thus can reject temperature) is | ||||||||||||||||||||||
| 106 | # Profile wire-truth (karr k186, ADR 0023): the o-series, gpt-5 / gpt-5.N | ||||||||||||||||||||||
| 107 | # except every -chat id, and gpt-6. Non-reasoning models and unknown ids are | ||||||||||||||||||||||
| 108 | # classified non-reasoning there, so their temperature is kept. | ||||||||||||||||||||||
| 109 | 260 | ▶ | return 0 unless $profile->is_reasoning_model; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 110 | # Resolved reasoning effort: a per-request control (chat_f, karr #46) beats the | ||||||||||||||||||||||
| 111 | # engine attribute; neither set means the model's server-side default applies. | ||||||||||||||||||||||
| 112 | # Resolved by the same layering the serializer uses, so an off switch | ||||||||||||||||||||||
| 113 | # (thinking_enabled => 0, karr k382) reads as effort 'none' here too. | ||||||||||||||||||||||
| 114 | 95 | ▶ | my %reasoning = $self->_reasoning_controls_for( %{ $controls // {} } ); | ||||||||||||||||||||
|
Condition50%1/2
$controls // {}
MC/DC0%0/1
$controls // {}
$controls
Truth table50%1/2
A
A= $controls
| |||||||||||||||||||||||
| 115 | 95 | my $effort = $reasoning{effort}; | |||||||||||||||||||||
| 116 | # No explicit effort: the model's server-side default effort applies. For most | ||||||||||||||||||||||
| 117 | # reasoning models that default is a reasoning level (temperature rejected), but | ||||||||||||||||||||||
| 118 | # the gpt-5.1/5.2/5.4 line defaults to reasoning-OFF, so a non-default | ||||||||||||||||||||||
| 119 | # temperature is honored there. The Profile carries which is which | ||||||||||||||||||||||
| 120 | # (default_reasoning_off, ADR 0023 wire-truth; live k185 2026-09-19). | ||||||||||||||||||||||
| 121 | 95 | ▶ | if ( !defined $effort ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 122 | 25 | ▶ | return 0 if $profile->default_reasoning_off; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 123 | 21 | return 1; | |||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||
| 125 | # Explicit effort=none disables reasoning (temperature accepted) only where the | ||||||||||||||||||||||
| 126 | # model's wire actually accepts 'none' as the disable value -- a read-only | ||||||||||||||||||||||
| 127 | # consult of the same Langertha::Reasoning::Profile effort table the serializer | ||||||||||||||||||||||
| 128 | # uses (ADR 0023). A model that cannot be disabled (gpt-6) drops a 'none' effort | ||||||||||||||||||||||
| 129 | # server-side and keeps reasoning on, so temperature stays rejected there. | ||||||||||||||||||||||
| 130 | 70 | ▶ | if ( $effort eq 'none' ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 131 | 10 | ▶ | return 0 if $profile->effort_accepted_on( $self->reasoning_wire_format, 'none' ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||
| 133 | 61 | return 1; | |||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | 110 | has whisper => ( | |||||||||||||||||||||
| 137 | is => 'ro', | ||||||||||||||||||||||
| 138 | isa => 'Langertha::Engine::TranscriptionBase', | ||||||||||||||||||||||
| 139 | lazy_build => 1, | ||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | 8 | ▶ | sub _build_whisper { | ||||||||||||||||||||
|
Subroutine
_build_whisper: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 143 | 8 | my ($self) = @_; | |||||||||||||||||||||
| 144 | # Same settings as $self->simple_transcription would use. -- karr k293 | ||||||||||||||||||||||
| 145 | 8 | ▶ | return Langertha::Engine::TranscriptionBase->new( | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 146 | api_key => $self->api_key, | ||||||||||||||||||||||
| 147 | url => $self->url, | ||||||||||||||||||||||
| 148 | transcription_model => $self->transcription_model, | ||||||||||||||||||||||
| 149 | user_agent_agent => $self->user_agent_agent, | ||||||||||||||||||||||
| 150 | $self->has_user_agent_timeout ? ( user_agent_timeout => $self->user_agent_timeout ) : (), | ||||||||||||||||||||||
| 151 | defined $self->connect_address ? ( connect_address => $self->connect_address ) : (), | ||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||
| 154 | |||||||||||||||||||||||
| 155 | |||||||||||||||||||||||
| 156 | 110 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||
| 157 | |||||||||||||||||||||||
| 158 | |||||||||||||||||||||||
| 159 | 110 | 1; | |||||||||||||||||||||
| 160 | |||||||||||||||||||||||
| 161 | __END__ | ||||||||||||||||||||||