blib/lib/Langertha/Engine/Groq.pm

filter: Statementstmt 93.7% 45 / 48 Branchbran 90.0% 18 / 20 Conditioncond 80.0% 16 / 20 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 92.3% 12 / 13 Podpod 25.0% 1 / 4 totaltotal 85.4% 100 / 117 CC 18 SCAR 15.5
CC
18
Cov
90%
_exclude_json_schema_with_tools_or_streaming
8.0
__ANON__
7.6
_build_api_key
2.3
CRAP 4.7
« blib/lib/Langertha/Role/Transcription.pm ↑ Summary blib/lib/Langertha/Moment.pm »
1 package Langertha::Engine::Groq;
2 # ABSTRACT: GroqCloud API
3 21 our $VERSION = '0.503';
4 21 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 21 extends 'Langertha::Engine::OpenAIBase';
8  
9 21 with map { 'Langertha::Role::'.$_ } qw(
10 Transcription
11 Tools
12 );
13  
14  
15 1 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=8.1
16 1 my ( $self ) = @_;
17 return $ENV{LANGERTHA_GROQ_API_KEY}
18 1 ▶ || croak "".(ref $self)." requires LANGERTHA_GROQ_API_KEY or api_key set";
Condition33%1/3
$ENV{'LANGERTHA_GROQ_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_GROQ_API_KEY or api_key set')
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'LANGERTHA_GROQ_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_GROQ_API_KEY or api_key set')
$ENV{'LANGERTHA_GROQ_API_KEY'} croak('' . ref($self) . ' requires LANGERTHA_GROQ_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_GROQ_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_GROQ_API_KEY or api_key set')
ABresult
1X1
011
000
19 }
20  
21 has '+url' => (
22 lazy => 1,
23 20 ▶ default => sub { 'https://api.groq.com/openai/v1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 21 );
25  
26 0 ▶sub default_model { croak "".(ref $_[0])." requires a default_model" }
Subroutine default_model: not called CC=1 SCAR=6.9
Pod documented
27  
28 3 ▶sub default_transcription_model { 'whisper-large-v3' }
Subroutine default_transcription_model: called CC=1 SCAR=0.0
Pod undocumented
29  
30 52 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
31 createChatCompletion
32 createTranscription
33 )]}
34  
35 # karr #148 / #184: Groq rejects a JSON response_format combined with tool use --
36 # BOTH json_object and json_schema 400 alongside tools, with the same "json mode
37 # cannot be combined with tool/function calling" message (live-verified
38 # 2026-09-19). Its Structured Outputs (json_schema) additionally reject streaming;
39 # json_object + streaming is not disproven and is left through. This is a
40 # Groq-platform property that holds across the models it serves, so an all-models
41 # matcher (qr//) expresses it on the model-scoped exclusion seam
42 # (Langertha::Role::Chat). There is no shared gpt-oss base rule (removed k184),
43 # so this all-models rule is the only exclusion on Groq's own gpt-oss route.
44 # Consulted by chat_f (streaming => 0) and chat_stream_realtime_f (streaming => 1).
45 40 ▶sub model_capability_exclusions {
Subroutine model_capability_exclusions: called CC=1 SCAR=0.0
Pod undocumented
46 return (
47 40 qr// => \&_exclude_json_schema_with_tools_or_streaming,
48 );
49 }
50  
51 40 ▶sub _exclude_json_schema_with_tools_or_streaming {
Subroutine _exclude_json_schema_with_tools_or_streaming: called CC=8 SCAR=20.8
52 40 my ( $self, %request ) = @_;
53 40 my $rf = $request{response_format};
54 40 ▶ my $type = ( ref $rf eq 'HASH' ) ? ( $rf->{type} // '' ) : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $rf eq 'HASH' ? : 14 26 40
Condition50%1/2
$$rf{"type"} // ""
l!l
140
MC/DC0%0/1
$$rf{"type"} // ""
$$rf{"type"}
Truth table50%1/2
A
A= $$rf{"type"}
Aresult
11
00
55 # Streaming: only json_schema is live-confirmed to 400 (json_object + streaming
56 # is not disproven, so it is left through).
57 40 ▶ if ( $request{streaming} && $type eq 'json_schema' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request{"streaming"} and $type eq "json_schema") 2 38 40
Condition100%3/3
$request{"streaming"} and $type eq "json_schema"
!ll&&!rl&&r
28102
MC/DC100%2/2
$request{"streaming"} and $type eq "json_schema"
$request{"streaming"} $type eq "json_schema"
Truth table100%3/3
A and B
A= $request{"streaming"}B= $type eq "json_schema"
ABresult
0X0
100
111
58 2 croak "".(ref $self)." cannot combine response_format json_schema with "
59 ."streaming: Groq Structured Outputs do not support streaming and the "
60 ."API rejects this with HTTP 400. Use the non-streaming chat_f for "
61 ."json_schema output.";
62 }
63 # Tools: BOTH json_object and json_schema 400 alongside tools.
64 38 ▶ if ( $request{has_tools} && ( $type eq 'json_schema' || $type eq 'json_object' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request{"has_tools"} and $type eq "json_schema" || $type eq "json_object") 8 30 38
Condition100%6/6
$request{"has_tools"} and $type eq "json_schema" || $type eq "json_object"
!ll&&!rl&&r
2918
$type eq "json_schema" or $type eq "json_object"
l!l&&r!l&&!r
351
MC/DC100%3/3
$request{"has_tools"} and $type eq "json_schema" || $type eq "json_object"
$request{"has_tools"} $type eq "json_schema" $type eq "json_object"
Truth table100%4/4
A and B or C
A= $request{"has_tools"}B= $type eq "json_schema"C= $type eq "json_object"
ABCresult
0XX0
1000
11X1
1011
65 8 croak "".(ref $self)." cannot combine tools with a JSON response_format "
66 ."(json_object or json_schema) in one request: Groq rejects json mode "
67 ."combined with tool/function calling with HTTP 400. Send tools or a JSON "
68 ."response_format, not both.";
69 }
70 30 return;
71 }
72  
73 # image_input (k266, ADR 0019 k266 Update): the only Groq vision model is
74 # qwen/qwen3.8-27b (console.groq.com/docs/vision, llm-advisor, docs only,
75 # 2026-09-25); the catch-all first row clears the flag for every other id.
76 #
77 # Groq has no default model: building chat_model croaks, and the layer-3 walk
78 # reads chat_model. With no model configured the table is therefore empty (so
79 # supports() keeps answering instead of croaking, as it did before k266) and
80 # the around below makes no image_input claim in its place.
81 334 ▶sub _has_configured_model {
Subroutine _has_configured_model: called CC=2 SCAR=6.9
82 334 my ( $self ) = @_;
83 334 ▶ return $self->has_chat_model || $self->has_model;
Condition100%3/3
$self->has_chat_model || $self->has_model
l!l&&r!l&&!r
315154
MC/DC100%2/2
$self->has_chat_model || $self->has_model
$self->has_chat_model $self->has_model
Truth table100%3/3
A || B
A= $self->has_chat_modelB= $self->has_model
ABresult
1X1
011
000
84 }
85  
86 167 ▶sub model_capability_corrections {
Subroutine model_capability_corrections: called CC=2 SCAR=6.9
Pod undocumented
87 167 my ( $self ) = @_;
88 167 ▶ return () unless $self->_has_configured_model;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_configured_model 2 165 167
89 return (
90 165 qr/\A/ => { image_input => 0 },
91 'qwen/qwen3.8-27b' => { image_input => 1 },
92 );
93 }
94  
95 167 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
96 167 my ( $orig, $self, @rest ) = @_;
97 167 my $caps = $self->$orig(@rest);
98 167 ▶ delete $caps->{image_input} unless $self->_has_configured_model;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_configured_model 2 165 167
99 167 return $caps;
100 21 };
101  
102 # Groq reports a stream's usage under x_groq.usage on its last chunk instead
103 # of a top-level usage (Groq API reference / SDK chunk type; not live-verified).
104 # One provider's spelling, so it is read here (ADR 0018 tier 3), guarded by the
105 # canonical predicate: a top-level usage, when Groq sends one, wins. -- k298
106 14 ▶around parse_stream_chunk => sub {
Subroutine __ANON__: called CC=7 SCAR=20.2
107 14 my ( $orig, $self, $data, @rest ) = @_;
108 14 my $chunk = $self->$orig( $data, @rest );
109 14 ▶ return $chunk if $chunk && $chunk->has_usage;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk and $chunk->has_usage 1 13 14
Condition66%2/3
$chunk and $chunk->has_usage
!ll&&!rl&&r
0131
MC/DC50%1/2
$chunk and $chunk->has_usage
$chunk $chunk->has_usage
Truth table66%2/3
A and B
A= $chunkB= $chunk->has_usage
ABresult
0X0
100
111
110 13 ▶ my $x_groq = ref $data eq 'HASH' ? $data->{x_groq} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 13 0 13
111 13 ▶ my %usage = $self->_openai_stream_usage_kwargs( ref $x_groq eq 'HASH' ? $x_groq->{usage} : undef );
Branch: 1 branch
BranchTrueFalseTotal
ref $x_groq eq 'HASH' ? : 2 11 13
112 13 ▶ return $chunk unless %usage;
Branch: 1 branch
BranchTrueFalseTotal
unless %usage 12 1 13
113 1 ▶ return $chunk->meta->clone_object( $chunk, %usage ) if $chunk;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk 1 0 1
114 0 require Langertha::Stream::Chunk;
115 0 return Langertha::Stream::Chunk->new( content => '', raw => $data, is_final => 0, %usage );
116 21 };
117  
118 21 __PACKAGE__->meta->make_immutable;
119  
120  
121 21 1;
122  
123 __END__
124  
125 =pod
126  
127 =encoding UTF-8
128  
129 =head1 NAME
130  
131 Langertha::Engine::Groq - GroqCloud API
132  
133 =head1 VERSION
134  
135 version 0.503
136  
137 =head1 SYNOPSIS
138  
139 use Langertha::Engine::Groq;
140  
141 my $groq = Langertha::Engine::Groq->new(
142 api_key => $ENV{GROQ_API_KEY},
143 model => 'llama-3.3-70b-versatile',
144 system_prompt => 'You are a helpful assistant',
145 );
146  
147 print $groq->simple_chat('Say something nice');
148  
149 # Audio transcription
150 my $text = $groq->simple_transcription('/path/to/audio.mp3');
151 # async: await $groq->simple_transcription_f(...)
152  
153 =head1 DESCRIPTION
154  
155 Provides access to Groq's ultra-fast LLM inference via their GroqCloud API.
156 Composes L<Langertha::Role::OpenAICompatible> with Groq's endpoint
157 (C<https://api.groq.com/openai/v1>) and API key handling.
158  
159 Popular models: C<llama-3.3-70b-versatile>, C<llama-3-groq-70b-tool-use>,
160 C<deepseek-r1-distill-llama-70b>, C<qwen-2.5-coder-32b>. Audio transcription
161 uses C<whisper-large-v3> by default. No default chat model is set; C<model>
162 must be specified explicitly.
163  
164 Dynamic model listing via C<list_models()>. Get your API key at
165 L<https://console.groq.com/keys> and set C<LANGERTHA_GROQ_API_KEY>.
166  
167 B<THIS API IS WORK IN PROGRESS>
168  
169 =head1 SEE ALSO
170  
171 =over
172  
173 =item * L<https://groqstatus.com/> - Groq service status
174  
175 =item * L<https://console.groq.com/docs/models> - Official Groq models documentation
176  
177 =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role
178  
179 =item * L<Langertha::Role::Transcription> - Transcription role (Groq hosts Whisper)
180  
181 =item * L<Langertha::Engine::DeepSeek> - Another OpenAI-compatible engine
182  
183 =back
184  
185 =head1 SUPPORT
186  
187 =head2 Issues
188  
189 Please report bugs and feature requests on GitHub at
190 L<https://github.com/Getty/langertha/issues>.
191  
192 =head2 IRC
193  
194 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
195  
196 =head1 CONTRIBUTING
197  
198 Contributions are welcome! Please fork the repository and submit a pull request.
199  
200 =head1 AUTHOR
201  
202 Torsten Raudssus <getty@cpan.org>
203  
204 =head1 COPYRIGHT AND LICENSE
205  
206 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
207  
208 This is free software; you can redistribute it and/or modify it under
209 the same terms as the Perl 5 programming language system itself.
210  
211 =cut
« blib/lib/Langertha/Role/Transcription.pm ↑ Summary blib/lib/Langertha/Moment.pm »