blib/lib/App/Greple/xlate/gpty/gpt5.pm

filter: Statementstmt 39.8% 43 / 108 Branchbran 0.0% 0 / 32 Conditioncond 10.5% 2 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 64.7% 11 / 17 Podpod 50.0% 2 / 4 totaltotal 30.5% 58 / 190 CC 23 SCAR 42.1
CC
23
Cov
28%
xlate
132.0
gpty
110.0
xlate_each
12.0
CRAP 67.0
« blib/lib/App/Greple/xlate/gpty/gpt4o.pm ↑ Summary blib/lib/App/Greple/xlate/gpty/gpt3.pm »
1 package App::Greple::xlate::gpty::gpt5;
2  
3 2 our $VERSION = "2.03";
4  
5 =head1 NAME
6  
7 App::Greple::xlate::gpty::gpt5 - GPT-5.5 translation engine (gpty backend) for greple xlate module
8  
9 =head1 SYNOPSIS
10  
11 greple -Mxlate --xlate-engine=gpt5 --xlate=ja file.txt
12  
13 =head1 DESCRIPTION
14  
15 This module provides GPT-5.5 translation support for the App::Greple::xlate
16 module. GPT-5.5 is OpenAI's latest language model, offering enhanced reasoning
17 capabilities and improved translation quality. The engine name remains C<gpt5>
18 for backward compatibility, while the default model is C<gpt-5.5>.
19  
20 =head1 GPT-5.5 API SPECIFICATIONS
21  
22 =head2 Model
23  
24 =over 4
25  
26 =item * B<gpt-5.5> - Full model with maximum capabilities (snapshot: C<gpt-5.5-2026-04-23>)
27  
28 =back
29  
30 Unlike the GPT-5 series, GPT-5.5 is offered as a single model with no
31 C<mini>/C<nano> variants.
32  
33 =head2 Token Limits
34  
35 =over 4
36  
37 =item * B<Context window>: 1,050,000 tokens
38  
39 =item * B<Output limit>: 128,000 tokens (including reasoning tokens)
40  
41 =back
42  
43 Prompts exceeding 272,000 input tokens are billed at 2x input and 1.5x output
44 rates for the full session.
45  
46 =head2 Input/Output Support
47  
48 =over 4
49  
50 =item * B<Input>: Text and images
51  
52 =item * B<Output>: Text only
53  
54 =back
55  
56 =head2 New API Parameters
57  
58 The GPT-5 family introduces several new parameters for fine-grained control:
59  
60 =head3 reasoning_effort
61  
62 Controls the model's thinking time and reasoning depth:
63  
64 =over 4
65  
66 =item * B<none> - Reasoning effectively disabled; the model behaves like a non-reasoning model for the fastest latency, suitable for deterministic tasks such as translation
67  
68 =item * B<low> - Low reasoning effort, prioritizes speed while keeping some planning
69  
70 =item * B<medium> - Balanced reasoning (default)
71  
72 =item * B<high> - High reasoning effort, prioritizes quality
73  
74 =item * B<xhigh> - Maximum reasoning effort
75  
76 =back
77  
78 Note: GPT-5.5 replaces the GPT-5 C<minimal> level with C<none>, and adds the
79 C<xhigh> level. The default is C<medium>. This engine uses C<none> for
80 translation to favor speed and cost.
81  
82 =head3 verbosity
83  
84 Controls the length and detail of responses:
85  
86 =over 4
87  
88 =item * B<low> - Minimal, terse responses
89  
90 =item * B<medium> - Balanced detail level
91  
92 =item * B<high> - Comprehensive, verbose responses
93  
94 =back
95  
96 =head3 max_completion_tokens
97  
98 Specifies the maximum number of completion tokens in the response.
99 Unlike the legacy max_tokens parameter, this specifically controls
100 output tokens and is the recommended approach for GPT-5.5.
101  
102 =head2 Enhanced Features
103  
104 =head3 Reduced Hallucinations
105  
106 The GPT-5 family is significantly less likely to hallucinate compared to
107 previous generations of models, which improves the factual reliability of
108 translations.
109  
110 =head3 Custom Tools Support
111  
112 The GPT-5 family supports custom tools that can receive plaintext payloads
113 instead of JSON, enabling more flexible integration with external systems.
114  
115 =head3 Context-Free Grammar (CFG)
116  
117 Allows strict output constraints to match predefined syntax rules,
118 useful for ensuring valid format generation.
119  
120 =head2 Pricing
121  
122 =over 4
123  
124 =item * B<Input>: $5.00/1M tokens
125  
126 =item * B<Cached input>: $0.50/1M tokens
127  
128 =item * B<Output>: $30.00/1M tokens
129  
130 =back
131  
132 Note: prompts exceeding 272,000 input tokens are billed at 2x input and 1.5x
133 output rates for the full session (applies to standard, batch, and flex).
134  
135 =head1 CONFIGURATION
136  
137 This module uses the following default parameters:
138  
139 =over 4
140  
141 =item * B<engine>: gpt-5.5
142  
143 =item * B<temperature>: 1 (fixed for GPT-5.5)
144  
145 =item * B<max_length>: 3000 characters per batch
146  
147 =item * B<reasoning_effort>: none (for translation tasks; fastest)
148  
149 =item * B<max_completion_tokens>: 16000
150  
151 =back
152  
153 =head1 ENVIRONMENT VARIABLES
154  
155 =over 4
156  
157 =item * B<OPENAI_API_KEY> - Required OpenAI API key, read by the C<gpty> command
158  
159 =back
160  
161 =head1 RELATED OPTIONS
162  
163 Batching and debugging are controlled through the standard
164 L<App::Greple::xlate> command-line options, not environment variables:
165  
166 =over 4
167  
168 =item * B<--xlate-maxlen>=I<chars> - Maximum characters sent per request
169 (defaults to this engine's value of 3000 when unset)
170  
171 =item * B<--xlate-maxline>=I<n> - Maximum lines sent per request
172 (default 0 = unlimited); useful as a safety valve if a large batch causes a
173 response element-count mismatch
174  
175 =item * B<--xlate-debug> - Dump the C<gpty> command and parameters
176  
177 =back
178  
179 =head1 DEPENDENCIES
180  
181 =over 4
182  
183 =item * L<App::Greple::xlate>
184  
185 =item * L<Command::Run> - For gpty command execution
186  
187 =item * L<JSON> - For JSON array processing
188  
189 =back
190  
191 =head1 SEE ALSO
192  
193 =over 4
194  
195 =item * L<App::Greple::xlate>
196  
197 =item * L<App::Greple::xlate::deepl>
198  
199 =item * L<App::Greple::xlate::gpty::gpt3>
200  
201 =item * L<App::Greple::xlate::gpty::gpt4o>
202  
203 =item * OpenAI GPT-5.5 Documentation: L<https://developers.openai.com/api/docs/models/gpt-5.5>
204  
205 =back
206  
207 =head1 AUTHOR
208  
209 Kazumasa Utashiro
210  
211 =head1 LICENSE
212  
213 Copyright 2024-2026 Kazumasa Utashiro.
214  
215 This library is free software; you can redistribute it and/or modify
216 it under the same terms as Perl itself.
217  
218 =cut
219  
220 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
222 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
225 {
226 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
227 0 ▶ *Data::Dumper::qquote = sub { qq["${\(shift)}"] };
Subroutine __ANON__: not called CC=1 SCAR=2.6
228 2 $Data::Dumper::Useperl = 1;
229 }
230  
231 2 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
232 2 ▶use Command::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 2 ▶use App::Greple::xlate qw(%opt &opt);
Subroutine BEGIN: called CC=1 SCAR=0.0
235 2 ▶use App::Greple::xlate::Lang qw(%LANGNAME);
Subroutine BEGIN: called CC=1 SCAR=0.0
236  
237 2 ▶our $lang_from //= 'ORIGINAL';
Condition50%1/2
our $lang_from //= "ORIGINAL"
l!l
02
MC/DC0%0/1
our $lang_from //= "ORIGINAL"
our $lang_from
Truth table50%1/2
A
A= our $lang_from
Aresult
11
00
238 2 ▶our $lang_to //= 'JA';
Condition50%1/2
our $lang_to //= "JA"
l!l
02
MC/DC0%0/1
our $lang_to //= "JA"
our $lang_to
Truth table50%1/2
A
A= our $lang_to
Aresult
11
00
239 2 our $auth_key;
240 2 our $method = __PACKAGE__ =~ s/.*://r;
241  
242 2 my %param = (
243 gpt5 => { engine => 'gpt-5.5', temp => '1', max => 3000, sub => \&gpty,
244 reasoning_effort => 'none', verbosity => 'low', max_completion_tokens => 16000,
245 prompt => <<END
246 Translate the following JSON array into %s.
247 For each input array element, output only the corresponding translated element at the same array index.
248 Leave blank strings unchanged. Within every element, preserve XML-style marker tags (e.g., '<m id="1" />') exactly and translate only the surrounding text.
249 Do not output the original (pre-translation) text under any circumstances.
250 The number and order of output elements must always match the input exactly: output element n must correspond to input element n.
251 Output only the translated elements or unchanged tags/blank strings as a JSON array.
252 Do not leave any unnecessary spaces or tabs at the end of any array element in your output.
253 Before finishing, carefully check that there are absolutely no omissions, duplicate content, or trailing spaces of any kind in your output.
254  
255 Return the result as a JSON array and nothing else.
256 Your entire output must be valid JSON.
257 Do not include any explanations, code blocks, or text outside of the JSON array.
258 If you cannot produce a valid JSON array, return an empty JSON array ([]).
259 END
260 },
261 );
262  
263 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
264 0 my($mod, $argv) = @_;
265 0 $mod->setopt(default => "-Mxlate --xlate-engine=$method");
266 }
267  
268 0 ▶sub gpty {
Subroutine gpty: not called CC=10 SCAR=47.0
Pod documented
269 0 state $gpty = Command::Run->new;
270 0 my $text = shift;
271 0 my $param = $param{$method};
272 0 ▶ my $prompt = opt('prompt') || $param->{prompt};
Condition0%0/3
opt('prompt') || $param->{'prompt'}
l!l&&r!l&&!r
000
MC/DC0%0/2
opt('prompt') || $param->{'prompt'}
opt('prompt') $param->{'prompt'}
Truth table0%0/3
A || B
A= opt('prompt')B= $param->{'prompt'}
ABresult
1X1
011
000
273 0 my @vars = do {
274 0 ▶ if ($prompt =~ /%s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prompt =~ /%s/u) { } 0 0 0
275 0 ▶ $LANGNAME{$lang_to} // die "$lang_to: unknown lang.\n"
Condition0%0/2
$App::Greple::xlate::gpty::gpt5::LANGNAME{$App::Greple::xlate::gpty::gpt5::lang_to} // die("$App::Greple::xlate::gpty::gpt5::lang_to: unknown lang.\n")
l!l
00
MC/DC0%0/1
$App::Greple::xlate::gpty::gpt5::LANGNAME{$App::Greple::xlate::gpty::gpt5::lang_to} // die("$App::Greple::xlate::gpty::gpt5::lang_to: unknown lang.\n")
$App::Greple::xlate::gpty::gpt5::LANGNAME{$App::Greple::xlate::gpty::gpt5::lang_to}
Truth table0%0/2
A
A= $App::Greple::xlate::gpty::gpt5::LANGNAME{$App::Greple::xlate::gpty::gpt5::lang_to}
Aresult
11
00
276 } else {
277 0 ();
278 }
279 };
280 0 my $system = sprintf($prompt, @vars);
281 # Add context information directly to the main system prompt
282 0 ▶ if (my @contexts = @{$opt{contexts}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@contexts) = @{$App::Greple::xlate::gpty::gpt5::opt{"contexts"};}) 0 0 0
283 0 $system .= "\n\nTranslation context:\n" . join("\n", map "- $_", @contexts);
284 }
285 my @command = (
286 'gpty',
287 '--engine' => $param->{engine},
288 0 '--system' => $system,
289 );
290 # Add temperature if specified and supported
291 0 ▶ if (defined $param->{temp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"temp"}) 0 0 0
292 0 push @command, '--temperature' => $param->{temp};
293 }
294 # Add GPT-5/O1 specific parameters if they exist
295 0 ▶ if (defined $param->{max_completion_tokens}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"max_completion_tokens"}) 0 0 0
296 0 push @command, '--max-completion-tokens' => $param->{max_completion_tokens};
297 }
298 0 ▶ if (defined $param->{reasoning_effort}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"reasoning_effort"}) 0 0 0
299 0 push @command, '--reasoning-effort' => $param->{reasoning_effort};
300 }
301 0 ▶ if (defined $param->{verbosity}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"verbosity"}) 0 0 0
302 0 push @command, '--verbosity' => $param->{verbosity};
303 }
304 0 push @command, '-';
305 0 ▶ warn Dumper \@command if opt('debug');
Branch: 1 branch
BranchTrueFalseTotal
if opt("debug") 0 0 0
306 0 $gpty->command(@command)->with(stdin => $text)->update->data;
307 }
308  
309 0 ▶sub _progress {
Subroutine _progress: not called CC=2 SCAR=17.9
310 0 ▶ print STDERR @_ if opt('progress');
Branch: 1 branch
BranchTrueFalseTotal
if opt("progress") 0 0 0
311 }
312  
313 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
314 2 my $json = JSON->new->canonical->pretty;
315  
316 0 ▶sub xlate_each {
Subroutine xlate_each: not called CC=3 SCAR=24.8
Pod undocumented
317 0 ▶ my $call = $param{$method}->{sub} // die;
Condition0%0/2
$param{$App::Greple::xlate::gpty::gpt5::method}{"sub"} // die()
l!l
00
MC/DC0%0/1
$param{$App::Greple::xlate::gpty::gpt5::method}{"sub"} // die()
$param{$App::Greple::xlate::gpty::gpt5::method}{"sub"}
Truth table0%0/2
A
A= $param{$App::Greple::xlate::gpty::gpt5::method}{"sub"}
Aresult
11
00
318 0 my @count = map { int tr/\n/\n/ } @_;
319 0 _progress("From:\n", map s/^/\t< /mgr, @_);
320 0 my($in, $out);
321 0 my @in = map { m/.*\n/mg } @_;
322 0 my $obj = $json->decode($out = $call->($in = $json->encode(\@in)));
323 0 my @out = map { s/(?<!\n)\z/\n/r } @$obj;
324 0 _progress("To:\n", map s/^/\t> /mgr, @out);
325 0 ▶ if (@out < @in) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out < @in) 0 0 0
326 0 my $to = join '', @out;
327 0 die sprintf("Unexpected response (%d < %d):\n\n%s\n",
328 int(@out), int(@in), $to);
329 }
330 0 map { join '', splice @out, 0, $_ } @count;
331 }
332  
333 0 ▶sub xlate {
Subroutine xlate: not called CC=11 SCAR=48.8
Pod documented
334 0 ▶ my @from = map { /\n\z/ ? $_ : "$_\n" } @_;
Branch: 1 branch
BranchTrueFalseTotal
/\n\z/u ? : 0 0 0
335 0 my @to;
336 0 ▶ my $max = $App::Greple::xlate::max_length || $param{$method}->{max} // die;
Condition0%0/5
($App::Greple::xlate::max_length || $param{$App::Greple::xlate::gpty::gpt5::method}{"max"}) // die()
l!l
00
$App::Greple::xlate::max_length or $param{$App::Greple::xlate::gpty::gpt5::method}{"max"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($App::Greple::xlate::max_length || $param{$App::Greple::xlate::gpty::gpt5::method}{"max"}) // die()
$App::Greple::xlate::max_length $param{$App::Greple::xlate::gpty::gpt5::method}{"max"}
Truth table0%0/3
A or B
A= $App::Greple::xlate::max_lengthB= $param{$App::Greple::xlate::gpty::gpt5::method}{"max"}
ABresult
1X1
011
000
337 0 my $maxline = $App::Greple::xlate::max_line;
338 0 ▶ if (my @len = grep { $_ > $max } map length, @from) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@len) = grep({$_ > $max;} map(length($_), @from))) 0 0 0
339 0 die "Contain lines longer than max length (@len > $max).\n";
340 }
341 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
342 0 my @tmp;
343 0 my $len = 0;
344 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
345 0 my $next = length $from[0];
346 0 ▶ last if $len + $next > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $len + $next > $max 0 0 0
347 0 $len += $next;
348 0 push @tmp, shift @from;
349 0 ▶ last if $maxline > 0 and @tmp >= $maxline;
Branch: 1 branch
BranchTrueFalseTotal
if $maxline > 0 and @tmp >= $maxline 0 0 0
Condition0%0/3
$maxline > 0 and @tmp >= $maxline
!ll&&!rl&&r
000
MC/DC0%0/2
$maxline > 0 and @tmp >= $maxline
$maxline > 0 @tmp >= $maxline
Truth table0%0/3
A and B
A= $maxline > 0B= @tmp >= $maxline
ABresult
0X0
100
111
350 }
351 0 ▶ @tmp > 0 or die "Probably text is longer than max length ($max).\n";
Branch: 1 branch
BranchTrueFalseTotal
@tmp > 0 or die "Probably text is longer than max length ($max).\n" 0 0 0
352 0 push @to, xlate_each @tmp;
353 }
354 0 @to;
355 }
356  
357 2 1;
358  
359 __DATA__
« blib/lib/App/Greple/xlate/gpty/gpt4o.pm ↑ Summary blib/lib/App/Greple/xlate/gpty/gpt3.pm »