blib/lib/Langertha/Engine/OpenAI.pm

filter: Statementstmt 100% 43 / 43 Branchbran 91.6% 22 / 24 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 12 / 12 Podpod 33.3% 1 / 3 totaltotal 87.1% 81 / 93 CC 16 SCAR 16.4
CC
16
Cov
92%
_temperature_rejected_by_reasoning
10.0
_max_tokens_key
4.2
_build_whisper
3.0
CRAP 5.1
« blib/lib/Langertha/Role/ResponseSize.pm ↑ Summary blib/lib/Langertha/Reasoning/Profile.pm »
1 package Langertha::Engine::OpenAI;
2 # ABSTRACT: OpenAI API
3 101 our $VERSION = '0.503';
4 101 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 101 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 101 ▶use Langertha::Engine::TranscriptionBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 101 ▶use Langertha::Reasoning::Profile;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 101 extends 'Langertha::Engine::OpenAIBase';
10  
11 101 with map { 'Langertha::Role::'.$_ } qw(
12 Embedding
13 Transcription
14 ImageGeneration
15 Tools
16 );
17  
18  
19 101 has compatibility_for_engine => (
20 is => 'ro',
21 predicate => 'has_compatibility_for_engine',
22 );
23  
24  
25 has '+url' => (
26 lazy => 1,
27 270 ▶ default => sub { 'https://api.openai.com/v1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 101 );
29  
30 1 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=8.1
31 1 my ( $self ) = @_;
32 return $ENV{LANGERTHA_OPENAI_API_KEY}
33 1 ▶ || croak "".(ref $self)." requires LANGERTHA_OPENAI_API_KEY or api_key set";
Condition33%1/3
$ENV{'LANGERTHA_OPENAI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
l!l&&r!l&&!r
100
MC/DC0%0/2
$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')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_OPENAI_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
ABresult
1X1
011
000
34 }
35  
36 52 ▶sub default_model { 'gpt-5.6-terra' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
37  
38 # OpenAI removes whisper-1 on 2027-02-26; its successor gpt-transcribe exists
39 # only on OpenAI, so only this engine (and its whisper handle) defaults to it
40 # (k308, k313).
41 30 ▶sub default_transcription_model { 'gpt-transcribe' }
Subroutine default_transcription_model: called CC=1 SCAR=0.0
Pod undocumented
42  
43 # image_input (k266, ADR 0019 k266 Update): every modern OpenAI chat model is a
44 # vision model (llm-advisor, docs only, 2026-09-25), so the family keeps the
45 # role-derived flag and only the text-only legacy / non-chat ids clear it.
46 # OpenAIResponses inherits this table.
47 1767 ▶sub model_capability_corrections {
Subroutine model_capability_corrections: called CC=1 SCAR=0.0
Pod undocumented
48 return (
49 1767 qr/\A(?:gpt-3\.5|o1-mini|o3-mini|text-embedding|whisper|tts-|dall-e|gpt-image|gpt-realtime|gpt-audio)/
50 => { image_input => 0 },
51 qr/-(?:audio|realtime|transcribe|tts)(?:-|\z)/ => { image_input => 0 },
52 'gpt-4' => { image_input => 0 },
53 qr/\Agpt-4-0(?:314|613)/ => { image_input => 0 },
54 qr/\Agpt-4-32k/ => { image_input => 0 },
55 qr/\Agpt-4-(?:\d{4}-preview|turbo-preview)\z/ => { image_input => 0 },
56 qr/\Ao1-preview/ => { image_input => 0 },
57 qr/\Agpt-oss/ => { image_input => 0 },
58 qr/\A(?:davinci|babbage)/ => { image_input => 0 },
59 );
60 }
61  
62 # The completion-length body key diverges by OpenAI model within the shared
63 # openai wire format: the gpt-5.x and gpt-6 reasoning lines dropped max_tokens
64 # entirely — gpt-5.x verified live 2026-08-13 (gpt-5.1 / gpt-5.6-terra reject it
65 # with HTTP 400 "Use 'max_completion_tokens' instead"); gpt-6 confirmed against
66 # the Chat Completions API reference (max_tokens "is now deprecated in favor of
67 # max_completion_tokens, and is not compatible with o-series models", extended
68 # to the gpt-5 / gpt-6 reasoning family — developers.openai.com, 2026-09-14) —
69 # while gpt-4.x / gpt-4o still accept it. Match the gpt-5 and gpt-6 families by
70 # anchored prefix, so gpt-5, gpt-5.1, gpt-5.6-*, gpt-6-astra etc. all hit the
71 # new key; unknown future ids keep the old key (the safe default for every
72 # other engine in the OpenAI-compatible family).
73 17 ▶sub _max_tokens_key {
Subroutine _max_tokens_key: called CC=4 SCAR=14.3
74 17 my ( $self ) = @_;
75 17 ▶ my $model = $self->can('chat_model') ? ( $self->chat_model // '' ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->can('chat_model') ? : 17 0 17
Condition50%1/2
$self->chat_model // ""
l!l
170
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
Aresult
11
00
76 17 ▶ return $model =~ /\Agpt-[56]/ ? 'max_completion_tokens' : 'max_tokens';
Branch: 1 branch
BranchTrueFalseTotal
$model =~ /\Agpt-[56]/ ? : 7 10 17
77 }
78  
79 # k155: OpenAI reasoning models 400 on a non-default temperature while reasoning
80 # is active -- "Unsupported value: 'temperature' does not support 0.7 with this
81 # model. Only the default (1) value is supported." (live-verified 2026-09-17
82 # against /v1/chat/completions on gpt-5.6-terra + gpt-5.6). The rejection is
83 # EFFORT-AWARE, not a flat per-model capability clear: at reasoning_effort=none
84 # (where the model accepts it) the same call returns 200, so clearing the
85 # temperature capability wholesale would wrongly drop the valid effort=none path.
86 # It also fires on the NO-EFFORT path for a model whose server-side default effort
87 # is a reasoning level -- so this predicate resolves the effort INCLUDING that
88 # default, via the Profile's default_reasoning_off signal (k185): the
89 # gpt-5.1/5.2/5.4 line defaults to reasoning-OFF (reasoning_tokens=0 with no
90 # effort) and keeps a non-default temperature there, while gpt-5.5/5.6, gpt-6, the
91 # o-series and legacy gpt-5 default to reasoning-ON. It is consumed READ-ONLY by
92 # the shared _temperature_kwargs gate in Role::OpenAICompatible /
93 # Role::ResponsesCompatible; OpenAIResponses inherits it and runs on the
94 # 'responses' wire. Non-reasoning OpenAI models (gpt-4o, gpt-4.1, gpt-5-chat,
95 # gpt-5.N-chat, any unknown id) are classified by the Profile and keep their
96 # temperature; every other OpenAI-compatible engine never reaches this predicate
97 # (they do not define it).
98 257 ▶sub _temperature_rejected_by_reasoning {
Subroutine _temperature_rejected_by_reasoning: called CC=10 SCAR=23.1
99 257 my ( $self, $controls ) = @_;
100 257 ▶ my $model = $self->can('chat_model') ? ( $self->chat_model // '' ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->can('chat_model') ? : 257 0 257
Condition50%1/2
$self->chat_model // ""
l!l
2570
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
Aresult
11
00
101 257 my $profile = Langertha::Reasoning::Profile->for_model($model);
102 # Which OpenAI models HAVE reasoning (and thus can reject temperature) is
103 # Profile wire-truth (karr k186, ADR 0023): the o-series, gpt-5 / gpt-5.N
104 # except every -chat id, and gpt-6. Non-reasoning models and unknown ids are
105 # classified non-reasoning there, so their temperature is kept.
106 257 ▶ return 0 unless $profile->is_reasoning_model;
Branch: 1 branch
BranchTrueFalseTotal
unless $profile->is_reasoning_model 165 92 257
107 # Resolved reasoning effort: a per-request control (chat_f, karr #46) beats the
108 # engine attribute; neither set means the model's server-side default applies.
109 my $effort = exists $controls->{reasoning_effort} ? $controls->{reasoning_effort}
110 92 ▶ : $self->has_reasoning_effort ? $self->reasoning_effort
Branch: 2 branches
BranchTrueFalseTotal
exists $$controls{'reasoning_effort'} ? : 54 38 92
$self->has_reasoning_effort ? : 14 24 38
111 : undef;
112 # No explicit effort: the model's server-side default effort applies. For most
113 # reasoning models that default is a reasoning level (temperature rejected), but
114 # the gpt-5.1/5.2/5.4 line defaults to reasoning-OFF, so a non-default
115 # temperature is honored there. The Profile carries which is which
116 # (default_reasoning_off, ADR 0023 wire-truth; live k185 2026-09-19).
117 92 ▶ if ( !defined $effort ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $effort) 24 68 92
118 24 ▶ return 0 if $profile->default_reasoning_off;
Branch: 1 branch
BranchTrueFalseTotal
if $profile->default_reasoning_off 4 20 24
119 20 return 1;
120 }
121 # Explicit effort=none disables reasoning (temperature accepted) only where the
122 # model's wire actually accepts 'none' as the disable value -- a read-only
123 # consult of the same Langertha::Reasoning::Profile effort table the serializer
124 # uses (ADR 0023). A model that cannot be disabled (gpt-6) drops a 'none' effort
125 # server-side and keeps reasoning on, so temperature stays rejected there.
126 68 ▶ if ( $effort eq 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($effort eq "none") 8 60 68
127 8 ▶ return 0 if $profile->effort_accepted_on( $self->reasoning_wire_format, 'none' );
Branch: 1 branch
BranchTrueFalseTotal
if $profile->effort_accepted_on($self->reasoning_wire_format, "none") 7 1 8
128 }
129 61 return 1;
130 }
131  
132 101 has whisper => (
133 is => 'ro',
134 isa => 'Langertha::Engine::TranscriptionBase',
135 lazy_build => 1,
136 );
137  
138 8 ▶sub _build_whisper {
Subroutine _build_whisper: called CC=3 SCAR=11.0
139 8 my ($self) = @_;
140 # Same settings as $self->simple_transcription would use. -- karr k293
141 8 ▶ return Langertha::Engine::TranscriptionBase->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_user_agent_timeout ? : 1 7 8
defined $self->connect_address ? : 1 7 8
142 api_key => $self->api_key,
143 url => $self->url,
144 transcription_model => $self->transcription_model,
145 user_agent_agent => $self->user_agent_agent,
146 $self->has_user_agent_timeout ? ( user_agent_timeout => $self->user_agent_timeout ) : (),
147 defined $self->connect_address ? ( connect_address => $self->connect_address ) : (),
148 );
149 }
150  
151  
152 101 __PACKAGE__->meta->make_immutable;
153  
154  
155 101 1;
156  
157 __END__
158  
159 =pod
160  
161 =encoding UTF-8
162  
163 =head1 NAME
164  
165 Langertha::Engine::OpenAI - OpenAI API
166  
167 =head1 VERSION
168  
169 version 0.503
170  
171 =head1 SYNOPSIS
172  
173 use Langertha::Engine::OpenAI;
174  
175 my $openai = Langertha::Engine::OpenAI->new(
176 api_key => $ENV{OPENAI_API_KEY},
177 model => 'gpt-5.6-terra',
178 system_prompt => 'You are a helpful assistant',
179 temperature => 0.7,
180 );
181  
182 my $response = $openai->simple_chat('Say something nice');
183 print $response;
184  
185 # Embeddings
186 my $vector = $openai->simple_embedding('Some text to embed');
187  
188 # Transcription (Whisper)
189 my $text = $openai->simple_transcription('/path/to/audio.mp3');
190  
191 # Async with Future::AsyncAwait
192 use Future::AsyncAwait;
193  
194 async sub ask_gpt {
195 my $response = await $openai->simple_chat_f('What is Perl?');
196 say $response;
197 my $vector = await $openai->simple_embedding_f('Some text to embed');
198 }
199  
200 =head1 DESCRIPTION
201  
202 Provides access to OpenAI's APIs, including GPT models, embeddings, and
203 Whisper transcription. Composes L<Langertha::Role::OpenAICompatible> for the
204 standard OpenAI API format.
205  
206 Popular models: C<gpt-6-astra> (GPT-6 flagship — 1.05M-token context, 128K max
207 output, text+image input, knowledge cutoff 2026-04-30), C<gpt-5.6-terra>
208 (default, balances intelligence and cost — the GPT-5.6 successor of the former
209 mini tier), C<gpt-5.6> (Sol, frontier), C<gpt-5.6-luna> (cost-sensitive,
210 successor of the former nano tier), C<text-embedding-3-large> (embeddings),
211 C<gpt-transcribe> (transcription, default), C<gpt-image-2> (image generation,
212 default).
213  
214 Dynamic model listing is supported via L<Langertha::Role::Models/list_models>.
215 Results are cached for C<models_cache_ttl> seconds (default: 3600).
216  
217 Get your API key at L<https://platform.openai.com/> and set
218 C<LANGERTHA_OPENAI_API_KEY> in your environment.
219  
220 B<THIS API IS WORK IN PROGRESS>
221  
222 =head2 compatibility_for_engine
223  
224 Optional identifier of the engine this instance is acting as a compatibility
225 shim for. Used internally when one engine is accessed via another's OpenAI
226 endpoint.
227  
228 =head2 whisper
229  
230 Lazy-built L<Langertha::Engine::TranscriptionBase> instance bound to
231 this engine's C<api_key>, C<url>, C<transcription_model> (C<gpt-transcribe>
232 unless set), C<user_agent_agent>, C<user_agent_timeout> and
233 L<Langertha::Role::HTTP/connect_address>, so it
234 transcribes exactly as C<< $openai->simple_transcription >> does.
235 Useful when you have an OpenAI engine handy and want a focused
236 transcription handle without re-stating credentials:
237  
238 my $text = $openai->whisper->simple_transcription('/path/audio.mp3');
239  
240 The transcription engine is fully independent: configure it directly
241 (C<temperature>, C<language>, etc.) via the returned object, or
242 construct your own L<Langertha::Engine::TranscriptionBase> if you need
243 a different model or endpoint.
244  
245 =head1 SEE ALSO
246  
247 =over
248  
249 =item * L<https://status.openai.com/> - OpenAI service status
250  
251 =item * L<https://platform.openai.com/docs> - Official OpenAI documentation
252  
253 =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role composed by this engine
254  
255 =item * L<Langertha::Role::Tools> - MCP tool calling interface
256  
257 =item * L<Langertha::Engine::DeepSeek> - DeepSeek (via OpenAICompatible role)
258  
259 =item * L<Langertha::Engine::Groq> - Groq (via OpenAICompatible role)
260  
261 =item * L<Langertha::Engine::Mistral> - Mistral (via OpenAICompatible role)
262  
263 =item * L<Langertha::Engine::vLLM> - vLLM inference server (via OpenAICompatible role)
264  
265 =item * L<Langertha::Engine::NousResearch> - Nous Research (via OpenAICompatible role)
266  
267 =item * L<Langertha::Engine::Perplexity> - Perplexity Sonar (via OpenAICompatible role)
268  
269 =back
270  
271 =head1 SUPPORT
272  
273 =head2 Issues
274  
275 Please report bugs and feature requests on GitHub at
276 L<https://github.com/Getty/langertha/issues>.
277  
278 =head2 IRC
279  
280 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
281  
282 =head1 CONTRIBUTING
283  
284 Contributions are welcome! Please fork the repository and submit a pull request.
285  
286 =head1 AUTHOR
287  
288 Torsten Raudssus <getty@cpan.org>
289  
290 =head1 COPYRIGHT AND LICENSE
291  
292 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
293  
294 This is free software; you can redistribute it and/or modify it under
295 the same terms as the Perl 5 programming language system itself.
296  
297 =cut
« blib/lib/Langertha/Role/ResponseSize.pm ↑ Summary blib/lib/Langertha/Reasoning/Profile.pm »