blib/lib/Langertha/Engine/Moonshot.pm

filter: Statementstmt 100% 29 / 29 Branchbran n/a Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 25.0% 1 / 4 totaltotal 86.2% 44 / 51 CC 2 SCAR 1.7
CC
2
Cov
94%
_build_api_key
2.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« blib/lib/Langertha/Engine/Whisper.pm ↑ Summary blib/lib/Langertha/Engine/TranscriptionBase.pm »
1 package Langertha::Engine::Moonshot;
2 # ABSTRACT: Moonshot AI Kimi API (OpenAI-compatible)
3 14 our $VERSION = '0.503';
4 14 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 extends 'Langertha::Engine::OpenAIBase';
8  
9 14 with map { 'Langertha::Role::'.$_ } qw(
10 StaticModels
11 Tools
12 );
13  
14  
15 133 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
16 createChatCompletion
17 )]}
18  
19 has '+url' => (
20 lazy => 1,
21 111 ▶ default => sub { 'https://api.moonshot.ai/v1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 14 );
23  
24 1 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=8.1
25 1 my ( $self ) = @_;
26 return $ENV{LANGERTHA_MOONSHOT_API_KEY}
27 1 ▶ || croak "".(ref $self)." requires LANGERTHA_MOONSHOT_API_KEY or api_key set";
Condition33%1/3
$ENV{'LANGERTHA_MOONSHOT_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'LANGERTHA_MOONSHOT_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
$ENV{'LANGERTHA_MOONSHOT_API_KEY'} croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_MOONSHOT_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
ABresult
1X1
011
000
28 }
29  
30 31 ▶sub default_model { 'kimi-k3' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
31  
32 # Kimi vision takes base64 data URLs (or ms:// file ids), no remote image URLs
33 # (platform.kimi.ai/docs/guide/use-kimi-vision-model), karr k267.
34 8 ▶sub _content_inline_images_only { 1 }
Subroutine _content_inline_images_only: called CC=1 SCAR=0.0
35  
36 37 ▶sub default_response_size { 4096 }
Subroutine default_response_size: called CC=1 SCAR=0.0
Pod undocumented
37  
38 # Kimi counts reasoning_content against max_tokens and recommends
39 # max_tokens >= 16000 while thinking is on (karr k225; platform.kimi.ai, advisor
40 # 2026-09-25 on k219, docs only). kimi-k3 and kimi-k2.7-code(-highspeed) always
41 # think, kimi-k2.6 thinks by default, so they default to 16000 instead of 4096.
42 # Only when no response_size is set; max_tokens is a ceiling, billing follows
43 # the tokens produced (ADR 0019 k225 Update).
44 325 ▶sub model_response_size_defaults {
Subroutine model_response_size_defaults: called CC=1 SCAR=0.0
Pod undocumented
45 return (
46 325 qr/\Akimi-k3(?!\d)/ => 16000,
47 qr/\Akimi-k2\.(?:6|7-code(?:-highspeed)?)\z/ => 16000,
48 );
49 }
50  
51 1 ▶sub _build_static_models {[
Subroutine _build_static_models: called CC=1 SCAR=0.0
52 1 { id => 'kimi-k3' },
53 { id => 'kimi-k2.7-code' },
54 { id => 'kimi-k2.7-code-highspeed' },
55 { id => 'kimi-k2.6' },
56 ]}
57  
58 # Per-model tool_choice reality on Kimi's OpenAI-compatible endpoint
59 # (ADR 0002 amendment, ADR 0019; platform.kimi.ai/docs/guide/use-tool-choice):
60 # * kimi-k3 (the engine default) always thinks, and forcing a *specific*
61 # tool is incompatible with thinking -> a named tool_choice returns 400.
62 # Clear tool_choice_named there (auto/any/none stay).
63 # * The K2.x line does not support `required` and errors if it is passed.
64 # Canonical `any` serializes to wire `required` (Langertha::ToolChoice),
65 # so clear tool_choice_any there (auto/none/named stay).
66 # * Reasoning is per model too (karr k207, platform.kimi.ai models overview,
67 # advisor 2026-09-25, docs only): kimi-k3 takes a top-level
68 # reasoning_effort (low|high|max, server default max; the accepted set
69 # lives in its Reasoning::Profile row), while the K2.x line takes only the
70 # Kimi `thinking` object. Clear reasoning_effort there (dotted and
71 # dash-form K2 ids alike, e.g. kimi-k2-thinking), and
72 # Role::ReasoningEffort then sends no reasoning field (the k204 gate).
73 # kimi-k2.6 alone is re-asserted (karr k219, exact id): it takes a
74 # top-level thinking {type: enabled|disabled} (KimiK26ChatRequest schema,
75 # kimi-k2-6-quickstart; advisor 2026-09-25, docs only, not live), which
76 # the opted-in toggle (_reasoning_thinking_toggle below) serializes from
77 # reasoning_effort. kimi-k2.7-code(-highspeed) stays cleared: `disabled`
78 # is an error there and the guides say not to pass thinking at all, so
79 # there is nothing to send.
80 # * Temperature is fixed server-side on every current Kimi id (karr k214,
81 # platform.kimi.ai models overview, advisor 2026-09-25, docs + third-party
82 # 400 reports, not live): kimi-k3 and kimi-k2.7-code(-highspeed) take only
83 # 1.0, kimi-k2.6 1.0 with thinking and only 0.6 without, anything else is a
84 # 400. Not effort-dependent, and 1 is not safe either (k2.6 non-thinking),
85 # so the flag is cleared and the field never goes out (ADR 0025 k214 Update).
86 # * image_input (k266, llm-advisor, docs only, 2026-09-25): the vision models
87 # kimi-k3, kimi-k2.6 and kimi-k2.7-code take image input (platform.kimi.ai
88 # use-kimi-vision-model; base64 only, Role::Chat inlines URL images here,
89 # k267); other ids are unchecked, so the catch-all first row clears the flag.
90 # The rows are deliberately distinct per model — that is the discriminating
91 # information the flat role-derived row could not carry.
92 544 ▶sub model_capability_corrections {
Subroutine model_capability_corrections: called CC=1 SCAR=0.0
Pod undocumented
93 return (
94 544 qr/\A/ => { image_input => 0 },
95 'kimi-k3' => { tool_choice_named => 0 },
96 qr/\Akimi-k3(?!\d)/ => { temperature => 0 },
97 qr/\Akimi-k2(?!\d)/ => { tool_choice_any => 0, reasoning_effort => 0, temperature => 0 },
98 'kimi-k2.6' => { reasoning_effort => 1 },
99 qr/\Akimi-k(?:3|2\.6|2\.7-code)(?!\d)/ => { image_input => 1 },
100 );
101 }
102  
103 # Kimi's chat/completions schema (platform.kimi.ai/docs/api/chat) has no
104 # parallel_tool_calls field; only its separate Responses API lists one. Clear
105 # parallel_tool_use engine-wide so it is dropped with a carp rather than sent
106 # unhonored (karr k242, docs only).
107 544 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 544 my ( $orig, $self, @rest ) = @_;
109 544 my $caps = $self->$orig(@rest);
110 544 delete $caps->{parallel_tool_use};
111 544 return $caps;
112 14 };
113  
114 # Kimi's chat/completions speaks the K2.x `thinking` on/off toggle as a
115 # top-level object (karr k219; ADR 0023 k209 Update: the toggle is an
116 # endpoint's opt-in). Only kimi-k2.6 reaches it: its Profile row maps none ->
117 # {type: disabled}, any other level -> {type: enabled}, and never sends `keep`.
118 # kimi-k3 has no toggle row and keeps its reasoning_effort (k207).
119 73 ▶sub _reasoning_thinking_toggle { 1 }
Subroutine _reasoning_thinking_toggle: called CC=1 SCAR=0.0
120  
121 14 __PACKAGE__->meta->make_immutable;
122  
123  
124 14 1;
125  
126 __END__
127  
128 =pod
129  
130 =encoding UTF-8
131  
132 =head1 NAME
133  
134 Langertha::Engine::Moonshot - Moonshot AI Kimi API (OpenAI-compatible)
135  
136 =head1 VERSION
137  
138 version 0.503
139  
140 =head1 SYNOPSIS
141  
142 use Langertha::Engine::Moonshot;
143  
144 my $moonshot = Langertha::Engine::Moonshot->new(
145 api_key => $ENV{MOONSHOT_API_KEY},
146 model => 'kimi-k3',
147 );
148  
149 print $moonshot->simple_chat('Hello from Perl!');
150  
151 # Streaming
152 $moonshot->simple_chat_stream(sub {
153 print shift->content;
154 }, 'Write a poem');
155  
156 # Tool calling
157 my $response = await $moonshot->chat_with_tools_f('Search for Perl modules');
158  
159 =head1 DESCRIPTION
160  
161 Provides access to L<Moonshot AI|https://www.moonshot.ai/>'s Kimi models via
162 their native OpenAI-compatible endpoint at C<https://api.moonshot.ai/v1>.
163  
164 Moonshot AI is a Beijing-based AI company; their Kimi models are natively
165 multimodal (text, image, and video input) with strong coding, reasoning, and
166 agentic capabilities. C<kimi-k3> offers a 1M-token context window; the K2.x
167 legacy models below remain at 256K.
168  
169 B<Why the OpenAI endpoint:> Moonshot also exposes an Anthropic-compatible
170 C</anthropic> endpoint; if you need the Anthropic wire format, use
171 L<Langertha::Engine::MoonshotAnthropic>. The native OpenAI-compatible endpoint
172 is the recommended default.
173  
174 B<Available models:>
175  
176 =over 4
177  
178 =item * C<kimi-k3> — Current flagship (default). Kimi's most capable model:
179 2.8 trillion parameters, native visual understanding, 1M context, frontier
180 reasoning and agentic tasks.
181  
182 =item * C<kimi-k2.7-code> — Dedicated coding model: more reliable
183 instruction following in long contexts and higher coding task success. 256K
184 context.
185  
186 =item * C<kimi-k2.7-code-highspeed> — High-speed variant of C<kimi-k2.7-code>
187 (~180 tokens/s, up to ~260 tokens/s in short-context scenarios).
188  
189 =item * C<kimi-k2.6> — Previous multimodal model: thinking and non-thinking
190 modes, dialogue and Agent tasks. 256K context.
191  
192 =back
193  
194 B<Sunset:> C<kimi-k2.5> and the C<moonshot-v1-*> generation series are no
195 longer available to newly registered users and reach full platform sunset on
196 2026-08-31; they are deliberately no longer listed here. The older C<kimi-k2>
197 preview series was discontinued on 2026-05-25.
198  
199 See L<https://platform.kimi.ai/docs/models> for the full model catalog.
200  
201 B<Reasoning note:> reasoning control differs per model family on this
202 endpoint. The K2.x line uses a Kimi-specific top-level C<thinking> object
203 (C<{ type =E<gt> 'enabled' }> / C<{ type =E<gt> 'disabled' }>), not the
204 OpenAI-wire C<reasoning_effort> field. On C<kimi-k2.6> this engine serializes
205 C<reasoning_effort> onto that toggle: C<none> sends
206 C<< thinking =E<gt> { type =E<gt> 'disabled' } >>, any other level
207 C<< thinking =E<gt> { type =E<gt> 'enabled' } >> (every level gives the same
208 depth), and no C<reasoning_effort> field goes out. C<kimi-k2.7-code> and
209 C<kimi-k2.7-code-highspeed> always think and must not be sent a C<thinking>
210 field, so the engine does not advertise C<reasoning_effort> there and sends
211 nothing. This K2.x wire is taken from Moonshot's documentation and is not
212 verified against the live API. C<kimi-k3> instead accepts a top-level
213 C<reasoning_effort> of C<low> / C<high> / C<max> and defaults to C<max>
214 server-side when the field is omitted; it always reasons. On C<kimi-k3> the
215 engine sends C<reasoning_effort> when it is one of those three values and drops
216 any other level, so the server default applies.
217  
218 B<Temperature:> every current Kimi model fixes C<temperature> server-side and
219 rejects other values, so this engine never sends one; a C<temperature> other
220 than C<1> is dropped with a warning.
221  
222 B<Response size:> Kimi counts reasoning toward C<max_tokens> and recommends at
223 least 16000 while thinking is on, so C<kimi-k3>, C<kimi-k2.7-code>,
224 C<kimi-k2.7-code-highspeed> and C<kimi-k2.6> default to 16000 (per model, so
225 C<kimi-k2.6> keeps 16000 even with C<< reasoning_effort => 'none' >>); other ids
226 keep 4096. An explicit C<response_size> is always sent as given.
227  
228 Supports chat, streaming, tool calling, and structured output. Embeddings,
229 transcription, and image generation are not supported via this endpoint.
230  
231 Get your API key at L<https://platform.kimi.ai/> and set
232 C<LANGERTHA_MOONSHOT_API_KEY> in your environment.
233  
234 =head1 SEE ALSO
235  
236 =over
237  
238 =item * L<Langertha::Engine::MoonshotAnthropic> - Moonshot via Anthropic-compatible endpoint
239  
240 =item * L<https://platform.kimi.ai/docs/api/overview> - Kimi OpenAI-compatible API docs
241  
242 =item * L<Langertha::Engine::OpenAIBase> - Base class for OpenAI-compatible engines
243  
244 =item * L<Langertha::Role::Tools> - MCP tool calling interface
245  
246 =back
247  
248 =head1 SUPPORT
249  
250 =head2 Issues
251  
252 Please report bugs and feature requests on GitHub at
253 L<https://github.com/Getty/langertha/issues>.
254  
255 =head2 IRC
256  
257 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
258  
259 =head1 CONTRIBUTING
260  
261 Contributions are welcome! Please fork the repository and submit a pull request.
262  
263 =head1 AUTHOR
264  
265 Torsten Raudssus <getty@cpan.org>
266  
267 =head1 COPYRIGHT AND LICENSE
268  
269 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
270  
271 This is free software; you can redistribute it and/or modify it under
272 the same terms as the Perl 5 programming language system itself.
273  
274 =cut
« blib/lib/Langertha/Engine/Whisper.pm ↑ Summary blib/lib/Langertha/Engine/TranscriptionBase.pm »