| 1 | package Langertha::Engine::Mistral; | ||||||||||||||||||||
| 2 | # ABSTRACT: Mistral API | ||||||||||||||||||||
| 3 | 15 | our $VERSION = '0.503'; | |||||||||||||||||||
| 4 | 15 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 15 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 15 | ▶ | use File::ShareDir::ProjectDistDir qw( :all ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 15 | ▶ | use Module::Runtime qw( use_module ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 15 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 15 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||
| 12 | Embedding | ||||||||||||||||||||
| 13 | Transcription | ||||||||||||||||||||
| 14 | Tools | ||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | has '+url' => ( | ||||||||||||||||||||
| 19 | lazy => 1, | ||||||||||||||||||||
| 20 | 12 | ▶ | default => sub { 'https://api.mistral.ai' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 15 | ); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 1 | ▶ | sub _build_api_key { | ||||||||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=8.1
| |||||||||||||||||||||
| 24 | 1 | my ( $self ) = @_; | |||||||||||||||||||
| 25 | return $ENV{LANGERTHA_MISTRAL_API_KEY} | ||||||||||||||||||||
| 26 | 1 | ▶ | || croak "".(ref $self)." requires LANGERTHA_MISTRAL_API_KEY or api_key set"; | ||||||||||||||||||
|
Condition33%1/3
$ENV{'LANGERTHA_MISTRAL_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_MISTRAL_API_KEY or api_key set')
MC/DC0%0/2
$ENV{'LANGERTHA_MISTRAL_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_MISTRAL_API_KEY or api_key set')
$ENV{'LANGERTHA_MISTRAL_API_KEY'}
croak('' . ref($self) . ' requires LANGERTHA_MISTRAL_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_MISTRAL_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_MISTRAL_API_KEY or api_key set')
| |||||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | ▶ | sub openapi_file { yaml => dist_file('Langertha','mistral.yaml') }; | ||||||||||||||||||
|
Subroutine
openapi_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 22 | ▶ | sub _build_openapi_operations { | ||||||||||||||||||
|
Subroutine
_build_openapi_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 22 | return use_module('Langertha::Spec::Mistral')->data; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 17 | ▶ | sub default_model { 'mistral-small-latest' } | ||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # image_input (k266, ADR 0019 k266 Update): Mistral serves text-only and vision | ||||||||||||||||||||
| 38 | # models side by side (llm-advisor, docs only, 2026-09-25), so the catch-all | ||||||||||||||||||||
| 39 | # first row clears the flag and the vision models re-assert it: the | ||||||||||||||||||||
| 40 | # small/medium/large -latest aliases, Pixtral, Small >= 3.1 (2503 on, Small 4 = | ||||||||||||||||||||
| 41 | # 2603), Medium 3.x (2505 on), Large 3 (2512 on) and Ministral 3 (2512 on). | ||||||||||||||||||||
| 42 | # Codestral, Nemo, Large 2407/2411, Ministral 2410 and Small 2409/2501 are | ||||||||||||||||||||
| 43 | # text-only and fall to the catch-all. Confirmed against the model cards | ||||||||||||||||||||
| 44 | # (llm-advisor, docs only, read 2026-09-25; the vision guide itself is stale): | ||||||||||||||||||||
| 45 | # mistral-small-latest -> mistral-small-2603 (Small 4, text+image); Medium 3.5 | ||||||||||||||||||||
| 46 | # = mistral-medium-3-5 / mistral-medium-3 / mistral-medium-latest (no dated | ||||||||||||||||||||
| 47 | # id on the card); Large 3 = mistral-large-2512 / mistral-large-latest; | ||||||||||||||||||||
| 48 | # Ministral 3 = ministral-{3,8,14}b-2512 / ministral-{3,8,14}b-latest. The | ||||||||||||||||||||
| 49 | # retired Pixtral, Small 2503/2506 and Medium 2505/2508 rows stay (harmless). | ||||||||||||||||||||
| 50 | 113 | ▶ | sub model_capability_corrections { | ||||||||||||||||||
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 51 | return ( | ||||||||||||||||||||
| 52 | 113 | qr/\A/ => { image_input => 0 }, | |||||||||||||||||||
| 53 | qr/\Amistral-(?:small|medium|large)-latest\z/ => { image_input => 1 }, | ||||||||||||||||||||
| 54 | qr/\Apixtral-/ => { image_input => 1 }, | ||||||||||||||||||||
| 55 | qr/\Amistral-small-(?:250[3-9]|251\d|2[6-9]\d\d)/ => { image_input => 1 }, | ||||||||||||||||||||
| 56 | qr/\Amistral-medium-(?:250[5-9]|251\d|2[6-9]\d\d)/ => { image_input => 1 }, | ||||||||||||||||||||
| 57 | qr/\Amistral-large-(?:251[2-9]|2[6-9]\d\d)/ => { image_input => 1 }, | ||||||||||||||||||||
| 58 | qr/\Aministral-\d+b-(?:251[2-9]|2[6-9]\d\d)/ => { image_input => 1 }, | ||||||||||||||||||||
| 59 | qr/\Amistral-medium-3(?:-5)?\z/ => { image_input => 1 }, | ||||||||||||||||||||
| 60 | qr/\Aministral-\d+b-latest\z/ => { image_input => 1 }, | ||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 32 | ▶ | sub chat_operation_id { 'chat_completion_v1_chat_completions_post' } | ||||||||||||||||||
|
Subroutine
chat_operation_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 7 | ▶ | sub list_models_path { '/v1/models' } | ||||||||||||||||||
|
Subroutine
list_models_path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # The static table above is the answer until the caller probes: /v1/models | ||||||||||||||||||||
| 69 | # states capabilities.vision per model and alias, and a probed fact wins over | ||||||||||||||||||||
| 70 | # the table for the models it describes (ADR 0032). | ||||||||||||||||||||
| 71 | 10 | ▶ | sub model_metadata_format { 'mistral' } | ||||||||||||||||||
|
Subroutine
model_metadata_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 72 | 5 | ▶ | sub model_metadata_url { $_[0]->url . $_[0]->list_models_path } | ||||||||||||||||||
|
Subroutine
model_metadata_url: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 5 | ▶ | sub embedding_operation_id { 'embeddings_v1_embeddings_post' } | ||||||||||||||||||
|
Subroutine
embedding_operation_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # Mistral's embedding model; the OpenAI role's text-embedding-3-large is not | ||||||||||||||||||||
| 77 | # served here (k291). | ||||||||||||||||||||
| 78 | 2 | ▶ | sub default_embedding_model { 'mistral-embed' } | ||||||||||||||||||
|
Subroutine
default_embedding_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | # Mistral spells it output_dimension (EmbeddingRequest is | ||||||||||||||||||||
| 81 | # additionalProperties:false) and documents it for codestral-embed only (k319). | ||||||||||||||||||||
| 82 | 4 | ▶ | sub _embedding_dimensions_field { | ||||||||||||||||||
|
Subroutine
_embedding_dimensions_field: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 83 | 4 | my ( $self ) = @_; | |||||||||||||||||||
| 84 | 4 | ▶ | return ( $self->embedding_model // '' ) =~ /\Acodestral-embed/ ? 'output_dimension' : undef; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->embedding_model // ""
MC/DC0%0/1
$self->embedding_model // ""
$self->embedding_model
Truth table50%1/2
A
A= $self->embedding_model
| |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 6 | ▶ | sub transcription_operation_id { 'audio_api_v1_transcriptions_post' } | ||||||||||||||||||
|
Subroutine
transcription_operation_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 2 | ▶ | sub default_transcription_model { 'voxtral-mini-latest' } | ||||||||||||||||||
|
Subroutine
default_transcription_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # Mistral reads its list-valued form fields as repeated parts under the plain | ||||||||||||||||||||
| 92 | # name, no [] (its SDKs' Speakeasy "standard" multipart format and its curl | ||||||||||||||||||||
| 93 | # docs); OpenAI's name[] key (k286) risks a silent no-op there. The [] spelling | ||||||||||||||||||||
| 94 | # is accepted and normalized too. -- karr k309, k315 | ||||||||||||||||||||
| 95 | 6 | ▶ | around transcription_request => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 96 | 6 | my ( $orig, $self, $file, %extra ) = @_; | |||||||||||||||||||
| 97 | 6 | for my $field (qw( timestamp_granularities context_bias )) { | |||||||||||||||||||
| 98 | 12 | ▶ | my $value = exists $extra{"${field}[]"} ? delete $extra{"${field}[]"} : $extra{$field}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 12 | ▶ | next unless ref $value eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 4 | $extra{$field} = { repeated => $value }; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 6 | return $self->$orig( $file, %extra ); | |||||||||||||||||||
| 103 | 15 | }; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 15 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 15 | 1; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =pod | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =encoding UTF-8 | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head1 NAME | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | Langertha::Engine::Mistral - Mistral API | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =head1 VERSION | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | version 0.503 | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head1 SYNOPSIS | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | use Langertha::Engine::Mistral; | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | my $mistral = Langertha::Engine::Mistral->new( | ||||||||||||||||||||
| 130 | api_key => $ENV{MISTRAL_API_KEY}, | ||||||||||||||||||||
| 131 | model => 'mistral-large-latest', | ||||||||||||||||||||
| 132 | system_prompt => 'You are a helpful assistant', | ||||||||||||||||||||
| 133 | temperature => 0.5, | ||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | print $mistral->simple_chat('Say something nice'); | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | my $vector = $mistral->simple_embedding($content); | ||||||||||||||||||||
| 139 | # async: await $mistral->simple_embedding_f($content) | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # Voxtral transcription | ||||||||||||||||||||
| 142 | my $text = $mistral->simple_transcription('/path/to/audio.mp3'); | ||||||||||||||||||||
| 143 | my $result = $mistral->simple_transcription_result('/path/to/audio.mp3', | ||||||||||||||||||||
| 144 | timestamp_granularities => ['segment'], | ||||||||||||||||||||
| 145 | diarize => 'true', | ||||||||||||||||||||
| 146 | context_bias => [qw( Langertha Voxtral )], | ||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||
| 148 | print "$_->{speaker_id}: $_->{text}\n" for @{ $result->{segments} }; | ||||||||||||||||||||
| 149 | # async: await $mistral->simple_transcription_f(...) | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =head1 DESCRIPTION | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | Provides access to Mistral AI's models via their API. Composes | ||||||||||||||||||||
| 154 | L<Langertha::Role::OpenAICompatible> with Mistral's endpoint | ||||||||||||||||||||
| 155 | (C<https://api.mistral.ai>) and its OpenAPI spec. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | Popular models: C<mistral-small-latest> (default, fast), C<mistral-large-latest> | ||||||||||||||||||||
| 158 | (most capable, 675B parameters), C<codestral-latest> (code generation), | ||||||||||||||||||||
| 159 | C<devstral-latest> (development workflows), C<pixtral-large-latest> (vision). | ||||||||||||||||||||
| 160 | Supports chat, embeddings (default embedding model C<mistral-embed>), tool | ||||||||||||||||||||
| 161 | calling, and audio transcription with Voxtral: C</v1/audio/transcriptions>, | ||||||||||||||||||||
| 162 | default model C<voxtral-mini-latest>, see L</transcription_request>. The audio | ||||||||||||||||||||
| 163 | is given as for every engine: a path, C<\$bytes> or a filehandle (see | ||||||||||||||||||||
| 164 | L<Langertha::Role::Transcription/transcription_file_part>). | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | L<Langertha::Role::Embedding/embedding_dimensions> is sent as Mistral's | ||||||||||||||||||||
| 167 | C<output_dimension> (not C<dimensions>, which the embeddings endpoint | ||||||||||||||||||||
| 168 | rejects) for C<codestral-embed*> models, the only ones Mistral documents it | ||||||||||||||||||||
| 169 | for. With any other embedding model, C<mistral-embed> (fixed 1024) included, | ||||||||||||||||||||
| 170 | it is not sent and carps once. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | Dynamic model listing via C<list_models()>. Get your API key at | ||||||||||||||||||||
| 173 | L<https://docs.mistral.ai/getting-started/quickstart/> and set | ||||||||||||||||||||
| 174 | C<LANGERTHA_MISTRAL_API_KEY>. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head2 transcription_request | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | my $request = $mistral->transcription_request($audio, | ||||||||||||||||||||
| 181 | timestamp_granularities => ['segment'], | ||||||||||||||||||||
| 182 | diarize => 'true', | ||||||||||||||||||||
| 183 | context_bias => [qw( Langertha Voxtral )], | ||||||||||||||||||||
| 184 | ); | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | Builds the Voxtral transcription request: C<POST /v1/audio/transcriptions> as | ||||||||||||||||||||
| 187 | C<multipart/form-data>, with C<transcription_model> (default: | ||||||||||||||||||||
| 188 | C<voxtral-mini-latest>). C<filename> in C<%extra> names the upload; the other | ||||||||||||||||||||
| 189 | pairs are sent as form fields: | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =over | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | =item * C<timestamp_granularities> - C<segment> and/or C<word>; the | ||||||||||||||||||||
| 194 | C<segments> of the answer then carry C<start> and C<end>. Mistral does not | ||||||||||||||||||||
| 195 | accept it together with C<language>. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =item * C<diarize> - C<'true'> labels each segment with a C<speaker_id>. Form | ||||||||||||||||||||
| 198 | fields are text, so pass the string rather than a JSON boolean object. | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =item * C<context_bias> - words or names the model should favour, each | ||||||||||||||||||||
| 201 | without commas or whitespace. | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =item * C<language>, C<temperature>. | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =back | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | C<timestamp_granularities> and C<context_bias> take an ArrayRef and send one | ||||||||||||||||||||
| 208 | part per element under the plain name, without C<[]>, which is how Mistral | ||||||||||||||||||||
| 209 | reads a list field (see the C<repeated> marker of | ||||||||||||||||||||
| 210 | L<Langertha::Role::HTTP/generate_multipart_body>); the C<[]> spelling | ||||||||||||||||||||
| 211 | (C<timestamp_granularities[]>) is accepted and sent the same way. Get the | ||||||||||||||||||||
| 212 | whole answer (C<text>, C<language>, C<segments>, C<usage>) with | ||||||||||||||||||||
| 213 | L<Langertha::Role::Transcription/simple_transcription_result>, or the text | ||||||||||||||||||||
| 214 | alone with L<Langertha::Role::Transcription/simple_transcription>; both have | ||||||||||||||||||||
| 215 | C<_f> variants. | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =head1 SEE ALSO | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =over | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =item * L<https://status.mistral.ai/> - Mistral service status | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | =item * L<https://mistral.ai/models> - Official Mistral models documentation | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | =item * L<Langertha::Role::Transcription> - Transcription role (Voxtral) | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | =item * L<https://docs.mistral.ai/api/endpoint/audio/transcriptions> - Mistral transcription API | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =item * L<Langertha::Engine::DeepSeek> - Another OpenAI-compatible engine | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =back | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | =head1 SUPPORT | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =head2 Issues | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||
| 240 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | =head2 IRC | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | =head1 CONTRIBUTING | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | =head1 AUTHOR | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 259 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||