blib/lib/Langertha/Engine/XAI.pm

filter: Statementstmt 100% 27 / 27 Branchbran 100% 2 / 2 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 33.3% 1 / 3 totaltotal 86.9% 40 / 46 CC 3 SCAR 3.4
CC
3
Cov
94%
_build_api_key
2.3
__ANON__
2.0
BEGIN
1.0
CRAP 1.4
« blib/lib/Langertha/Manifest/Auth.pm ↑ Summary blib/lib/Langertha/Content.pm »
1 package Langertha::Engine::XAI;
2 # ABSTRACT: xAI Grok API
3 10 our $VERSION = '0.503';
4 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 extends 'Langertha::Engine::OpenAIBase';
8  
9 10 with map { 'Langertha::Role::'.$_ } qw(
10 ImageGeneration
11 Tools
12 );
13  
14  
15 37 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
16 createChatCompletion
17 createImage
18 )]}
19  
20 has '+url' => (
21 lazy => 1,
22 27 ▶ default => sub { 'https://api.x.ai/v1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 10 );
24  
25 1 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=8.1
26 1 my ( $self ) = @_;
27 return $ENV{LANGERTHA_XAI_API_KEY}
28 1 ▶ || croak "".(ref $self)." requires LANGERTHA_XAI_API_KEY or api_key set";
Condition33%1/3
$ENV{'LANGERTHA_XAI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_XAI_API_KEY or api_key set')
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'LANGERTHA_XAI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_XAI_API_KEY or api_key set')
$ENV{'LANGERTHA_XAI_API_KEY'} croak('' . ref($self) . ' requires LANGERTHA_XAI_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_XAI_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_XAI_API_KEY or api_key set')
ABresult
1X1
011
000
29 }
30  
31 16 ▶sub default_model { 'grok-4.7' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
32  
33 # image_input (k266, k280, ADR 0019 k266 Update): every grok-4 chat model in
34 # the docs.x.ai catalogue lists text+image input -- grok-4.3, 4.5, 4.6, 4.7,
35 # the grok-4.20 family and grok-build-0.1 (aliases grok-code-fast-1 /
36 # grok-code-fast); retired grok-4-0709 / grok-4-fast-* / grok-4-1-fast-*
37 # redirect to grok-4.3 (llm-advisor, docs only, read 2026-09-25). Other
38 # families are unchecked, so the catch-all first row clears the flag.
39 117 ▶sub model_capability_corrections {
Subroutine model_capability_corrections: called CC=1 SCAR=0.0
Pod undocumented
40 return (
41 117 qr/\A/ => { image_input => 0 },
42 qr/\Agrok-4(?:[.-]|\z)/ => { image_input => 1 },
43 qr/\Agrok-(?:build-|code-fast)/ => { image_input => 1 },
44 );
45 }
46  
47 2 ▶sub default_image_model { 'grok-imagine-image-2.0' }
Subroutine default_image_model: called CC=1 SCAR=0.0
Pod undocumented
48  
49 # xAI's /v1/images/generations takes aspect_ratio and resolution in place of
50 # OpenAI's size / quality / style and does not accept those; one passed in
51 # (Langertha::ImageGen forwards size and quality) is dropped with a warning,
52 # as k308 drops response_format for gpt-image. -- karr k309
53 4 ▶around image_request => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
54 4 my ( $orig, $self, $prompt, %extra ) = @_;
55 4 ▶ if ( my @unsupported = grep { exists $extra{$_} } qw( quality size style ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unsupported) = grep({exists $extra{$_};} "quality", "size", "style")) 2 2 4
56 2 delete @extra{@unsupported};
57 2 carp "".(ref $self)." image_request: xAI does not take "
58 . join( ', ', @unsupported ) . " (use aspect_ratio / resolution); dropped";
59 }
60 4 return $self->$orig( $prompt, %extra );
61 10 };
62  
63  
64 10 __PACKAGE__->meta->make_immutable;
65  
66  
67 10 1;
68  
69 __END__
70  
71 =pod
72  
73 =encoding UTF-8
74  
75 =head1 NAME
76  
77 Langertha::Engine::XAI - xAI Grok API
78  
79 =head1 VERSION
80  
81 version 0.503
82  
83 =head1 SYNOPSIS
84  
85 use Langertha::Engine::XAI;
86  
87 my $xai = Langertha::Engine::XAI->new(
88 api_key => $ENV{XAI_API_KEY},
89 model => 'grok-4.7',
90 system_prompt => 'You are a helpful assistant',
91 );
92  
93 print $xai->simple_chat('Say something nice');
94  
95 # Streaming
96 $xai->simple_chat_stream(sub {
97 print shift->content;
98 }, 'Write a poem');
99  
100 # Tool calling
101 my $response = await $xai->chat_with_tools_f('Search for Perl modules');
102  
103 # Image generation (Imagine API)
104 my $images = $xai->simple_image('A lighthouse at dusk',
105 aspect_ratio => '16:9', resolution => '2k');
106 print $images->[0]{url}, "\n";
107 # async: await $xai->simple_image_f(...)
108  
109 =head1 DESCRIPTION
110  
111 Provides access to L<xAI|https://x.ai/>'s Grok models via their
112 OpenAI-compatible API at C<https://api.x.ai/v1>. Composes
113 L<Langertha::Role::OpenAICompatible> with xAI's endpoint and API key
114 handling, plus L<Langertha::Role::Tools> for MCP tool calling.
115  
116 Grok 4.7 (C<grok-4.7>, the default) is xAI's current flagship general model,
117 with a 500K-token context window and agentic tool calling. C<model> takes any
118 id the API serves, such as the previous default C<grok-4.6>; xAI's models page
119 lists only C<grok-4.7> today, so check L<https://docs.x.ai/docs/models> before
120 relying on an older id. Grok has no knowledge of current events beyond its
121 training cut-off unless you enable xAI's server-side Web Search / X Search
122 tools.
123  
124 The engine covers chat, streaming, tool calling, structured output, and
125 image generation with the Imagine API (C</v1/images/generations>, default
126 model C<grok-imagine-image-2.0>, see L</image_request>). xAI's audio (Voice
127 API) and video endpoints are not exposed.
128  
129 C<reasoning_effort> goes out on C<chat/completions> only with a level the
130 model accepts: C<low>/C<medium>/C<high>/C<xhigh> on C<grok-4.6> and later,
131 C<low>/C<medium>/C<high> on C<grok-4.5>. Grok always reasons (server default
132 C<high>), so C<none>, C<minimal> and C<max> are dropped and the default
133 applies (see L<Langertha::Reasoning::Profile>).
134  
135 Set C<prompt_cache_key> to a stable per-conversation value to steer xAI's
136 best-effort prompt-cache routing: it goes out as a C<prompt_cache_key> body
137 field on C<chat/completions>, which xAI plumbs internally to its
138 C<x-grok-conv-id> sticky-routing hint. Check
139 C<< $response->usage->cached_tokens >> to confirm a cache hit actually
140 happened.
141  
142 Get your API key at L<https://console.x.ai/> and set
143 C<LANGERTHA_XAI_API_KEY> in your environment.
144  
145 B<THIS API IS WORK IN PROGRESS>
146  
147 =head2 image_request
148  
149 my $request = $xai->image_request('A lighthouse at dusk',
150 aspect_ratio => '16:9',
151 resolution => '2k',
152 n => 2,
153 response_format => 'b64_json',
154 );
155  
156 Builds the Imagine API request (C<POST /v1/images/generations>) with
157 C<image_model> (default: C<grok-imagine-image-2.0>). xAI's own options pass
158 through as given: C<aspect_ratio> (such as C<1:1>, C<16:9>), C<resolution>
159 (C<1k>, C<1.5k>, C<2k>), C<n> (up to 10 images) and C<response_format>
160 (C<url>, the default, or C<b64_json>). The OpenAI options C<size>,
161 C<quality> and C<style> are not accepted by xAI and are dropped with a
162 warning. L<Langertha::Role::OpenAICompatible/simple_image> and
163 L<Langertha::Role::ImageGeneration/simple_image_f> return the ArrayRef of
164 images (C<url> or C<b64_json> each).
165  
166 =head1 SEE ALSO
167  
168 =over
169  
170 =item * L<https://status.x.ai/> - xAI service status
171  
172 =item * L<https://docs.x.ai/docs/models> - Official xAI models documentation
173  
174 =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role
175  
176 =item * L<Langertha::Role::Tools> - MCP tool calling interface
177  
178 =item * L<Langertha::Role::ImageGeneration> - Image generation role (Imagine API)
179  
180 =item * L<https://docs.x.ai/docs/guides/image-generations> - xAI image generation guide
181  
182 =item * L<Langertha::Engine::Groq> - Another OpenAI-compatible engine
183  
184 =back
185  
186 =head1 SUPPORT
187  
188 =head2 Issues
189  
190 Please report bugs and feature requests on GitHub at
191 L<https://github.com/Getty/langertha/issues>.
192  
193 =head2 IRC
194  
195 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
196  
197 =head1 CONTRIBUTING
198  
199 Contributions are welcome! Please fork the repository and submit a pull request.
200  
201 =head1 AUTHOR
202  
203 Torsten Raudssus <getty@cpan.org>
204  
205 =head1 COPYRIGHT AND LICENSE
206  
207 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
208  
209 This is free software; you can redistribute it and/or modify it under
210 the same terms as the Perl 5 programming language system itself.
211  
212 =cut
« blib/lib/Langertha/Manifest/Auth.pm ↑ Summary blib/lib/Langertha/Content.pm »