| 1 | package Langertha::Engine::Cerebras; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Cerebras Inference API | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | with 'Langertha::Role::Tools'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | has '+url' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 13 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | default => sub { 'https://api.cerebras.ai/v1' }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 12 | ); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | sub _build_api_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 19 | return $ENV{LANGERTHA_CEREBRAS_API_KEY} | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | || croak "".(ref $self)." requires LANGERTHA_CEREBRAS_API_KEY or api_key set"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$ENV{'LANGERTHA_CEREBRAS_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_CEREBRAS_API_KEY or api_key set')
MC/DC0%0/2
$ENV{'LANGERTHA_CEREBRAS_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_CEREBRAS_API_KEY or api_key set')
$ENV{'LANGERTHA_CEREBRAS_API_KEY'}
croak('' . ref($self) . ' requires LANGERTHA_CEREBRAS_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_CEREBRAS_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_CEREBRAS_API_KEY or api_key set')
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 19 | ▶ | sub default_model { 'gpt-oss-120b' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # Image inputs are base64 data URLs only | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # (inference-docs.cerebras.ai/capabilities/image-inputs), karr k267. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | sub _content_inline_images_only { 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_content_inline_images_only: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 18 | ▶ | sub _build_supported_operations {[qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | createChatCompletion | ||||||||||||||||||||||||||||||||||||||||||
| 31 | )]} | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # karr #148: the Cerebras API rejects a request that carries both tools and a | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # structured-output response_format of EITHER type (json_object or json_schema) | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # with an opaque HTTP 400. Its docs make this per-model, but every model this | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # engine currently serves (gpt-oss-120b default, zai-glm-4.7) rejects the | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # combination, so an all-models matcher (qr//) expresses the platform-wide | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # reality on the model-scoped exclusion seam (Langertha::Role::Chat). There is no | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # shared gpt-oss base rule (removed k184); this all-models rule is Cerebras's only | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # exclusion and refuses both json_object and json_schema alongside tools. Consulted | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # by chat_f and chat_stream_realtime_f. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 11 | ▶ | sub model_capability_exclusions { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
model_capability_exclusions: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 11 | qr// => \&_exclude_tools_with_any_response_format, | |||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 11 | ▶ | sub _exclude_tools_with_any_response_format { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_exclude_tools_with_any_response_format: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 11 | my ( $self, %request ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 11 | my $rf = $request{response_format}; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 11 | ▶ | my $type = ( ref $rf eq 'HASH' ) ? ( $rf->{type} // '' ) : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$rf{"type"} // ""
MC/DC0%0/1
$$rf{"type"} // ""
$$rf{"type"}
Truth table50%1/2
A
A= $$rf{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | return unless $request{has_tools} | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 11 | ▶ | && ( $type eq 'json_object' || $type eq 'json_schema' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$request{"has_tools"} and $type eq "json_object" || $type eq "json_schema"
$type eq "json_object" or $type eq "json_schema"
MC/DC100%3/3
$request{"has_tools"} and $type eq "json_object" || $type eq "json_schema"
$request{"has_tools"}
$type eq "json_object"
$type eq "json_schema"
Truth table100%4/4
A and B or C
A= $request{"has_tools"}B= $type eq "json_object"C= $type eq "json_schema"
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 8 | croak "".(ref $self)." cannot combine tools and response_format in one " | |||||||||||||||||||||||||||||||||||||||||
| 55 | ."request: the Cerebras API rejects this combination with HTTP 400. Send " | ||||||||||||||||||||||||||||||||||||||||||
| 56 | ."tools or response_format, not both (run the tools first, then a second " | ||||||||||||||||||||||||||||||||||||||||||
| 57 | ."structured-output turn)."; | ||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | # image_input (k266, ADR 0019 k266 Update): only the vision models take image | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # input (inference-docs.cerebras.ai/capabilities/image-inputs, public preview, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # base64 PNG/JPEG only -- URL images are inlined by _content_inline_images_only; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # llm-advisor, docs only, 2026-09-25). The catch-all first row clears the flag, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # so the default gpt-oss-120b and unknown ids make no claim. | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 92 | ▶ | sub model_capability_corrections { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 92 | qr/\A/ => { image_input => 0 }, | |||||||||||||||||||||||||||||||||||||||||
| 68 | qr/\A(?:qwen-3\.8-27b|gemma-4-31b|kimi-k2\.7-code)/ => { image_input => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 12 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | Langertha::Engine::Cerebras - Cerebras Inference API | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | version 0.503 | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | use Langertha::Engine::Cerebras; | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | my $cerebras = Langertha::Engine::Cerebras->new( | ||||||||||||||||||||||||||||||||||||||||||
| 96 | api_key => $ENV{CEREBRAS_API_KEY}, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | model => 'gpt-oss-120b', | ||||||||||||||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | print $cerebras->simple_chat('Hello from Perl!'); | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | Provides access to Cerebras Inference, the fastest AI inference platform. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | Composes L<Langertha::Role::OpenAICompatible> with Cerebras's endpoint | ||||||||||||||||||||||||||||||||||||||||||
| 106 | (C<https://api.cerebras.ai/v1>) and API key handling. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | Cerebras uses custom wafer-scale chips to deliver extremely fast inference | ||||||||||||||||||||||||||||||||||||||||||
| 109 | speeds. The public endpoint serves C<gpt-oss-120b> (production, the default) | ||||||||||||||||||||||||||||||||||||||||||
| 110 | and C<zai-glm-4.7> (preview). | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | Supports chat, streaming, and MCP tool calling. Embeddings and transcription | ||||||||||||||||||||||||||||||||||||||||||
| 113 | are not supported. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | Get your API key at L<https://cloud.cerebras.ai/> and set | ||||||||||||||||||||||||||||||||||||||||||
| 116 | C<LANGERTHA_CEREBRAS_API_KEY> in your environment. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | =item * L<https://status.cerebras.ai/> - Cerebras service status | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | =item * L<https://inference-docs.cerebras.ai/> - Cerebras Inference documentation | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||
| 137 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 156 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||||||||||||||||||||