blib/lib/Langertha/Engine/LMStudioOpenAI.pm

filter: Statementstmt 100% 25 / 25 Branchbran n/a Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 13 / 13 Podpod 42.8% 3 / 7 totaltotal 91.6% 44 / 48 CC 2 SCAR 1.3
CC
2
Cov
100%
_build_api_key
2.0
BEGIN
1.0
__ANON__
1.0
CRAP 1.1
« blib/lib/Langertha/Engine/AKIAnthropic.pm ↑ Summary blib/lib/Langertha/UsageRecord.pm »
1 package Langertha::Engine::LMStudioOpenAI;
2 # ABSTRACT: LM Studio via OpenAI-compatible API
3 10 our $VERSION = '0.503';
4 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 extends 'Langertha::Engine::OpenAIBase';
7  
8 10 with 'Langertha::Role::Embedding', 'Langertha::Role::Tools';
9  
10  
11 has '+url' => (
12 lazy => 1,
13 2 ▶ default => sub { 'http://localhost:1234/v1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 10 );
15  
16 11 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=6.9
17 11 ▶ return $ENV{LANGERTHA_LMSTUDIO_API_KEY} || 'lmstudio';
Condition100%2/2
$ENV{'LANGERTHA_LMSTUDIO_API_KEY'} || 'lmstudio'
l!l
29
MC/DC100%1/1
$ENV{'LANGERTHA_LMSTUDIO_API_KEY'} || 'lmstudio'
$ENV{'LANGERTHA_LMSTUDIO_API_KEY'}
Truth table100%2/2
A
A= $ENV{'LANGERTHA_LMSTUDIO_API_KEY'}
Aresult
11
00
18 }
19  
20  
21 21 ▶sub default_model { 'default' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
22 # No fixed embedding model: the caller's model, else no model field (k297).
23 4 ▶sub default_embedding_model { undef }
Subroutine default_embedding_model: called CC=1 SCAR=0.0
Pod undocumented
24  
25 # LM Studio documents no dimensions field for /v1/embeddings (k319).
26 2 ▶sub _embedding_dimensions_field { undef }
Subroutine _embedding_dimensions_field: called CC=1 SCAR=0.0
27  
28 # Shares the LM Studio key with the native engine (derivation would name the
29 # protocol variant); optional, the local server accepts the 'lmstudio' dummy.
30 2 ▶sub api_key_env { 'LANGERTHA_LMSTUDIO_API_KEY' }
Subroutine api_key_env: called CC=1 SCAR=0.0
Pod documented
31 2 ▶sub api_key_required { 0 }
Subroutine api_key_required: called CC=1 SCAR=0.0
Pod documented
32  
33  
34 21 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
35 createChatCompletion
36 createEmbedding
37 )]}
38  
39 # LM Studio documents a closed parameter list for /v1/chat/completions
40 # (lmstudio.ai/docs/developer/openai-compat/chat-completions) and
41 # prompt_cache_key is not on it (checked 2026-09-25, karr #200), so the
42 # OpenAI cache-routing hint is not advertised (ADR 0002 layer 2).
43 66 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 66 my ( $orig, $self, @rest ) = @_;
45 66 my $caps = $self->$orig(@rest);
46 66 delete $caps->{prompt_cache_key};
47 66 return $caps;
48 10 };
49  
50 # image_input (k266, ADR 0019): self-hosted: the served model is launch state
51 # the client cannot see, so no static claim. A layer-3 catch-all rather than a
52 # layer-2 delete, so a fact probed from the server's native /api/v1/models
53 # (capabilities.vision) can answer per model (ADR 0032).
54 66 ▶sub model_capability_corrections {
Subroutine model_capability_corrections: called CC=1 SCAR=0.0
Pod undocumented
55 66 return ( qr/\A/ => { image_input => 0 } );
56 }
57  
58 4 ▶sub model_metadata_format { 'lmstudio' }
Subroutine model_metadata_format: called CC=1 SCAR=0.0
Pod undocumented
59 4 ▶sub model_metadata_url {
Subroutine model_metadata_url: called CC=1 SCAR=0.0
Pod undocumented
60 4 return Langertha::ModelProbe->server_root_url( $_[0]->url ) . '/api/v1/models';
61 }
62  
63 10 __PACKAGE__->meta->make_immutable;
64  
65  
66  
67 10 1;
68  
69 __END__
70  
71 =pod
72  
73 =encoding UTF-8
74  
75 =head1 NAME
76  
77 Langertha::Engine::LMStudioOpenAI - LM Studio via OpenAI-compatible API
78  
79 =head1 VERSION
80  
81 version 0.503
82  
83 =head1 SYNOPSIS
84  
85 use Langertha::Engine::LMStudioOpenAI;
86  
87 # 1. Simple chat
88 my $lm_oai = Langertha::Engine::LMStudioOpenAI->new(
89 url => 'http://localhost:1234/v1',
90 model => 'qwen2.5-7b-instruct-1m',
91 );
92  
93 print $lm_oai->simple_chat('Hello from OpenAI-compatible endpoint');
94  
95 # 2. Streaming
96 $lm_oai->simple_chat_stream(sub {
97 print shift->content;
98 }, 'Write a haiku about Perl');
99  
100 # 3. Embeddings (LM Studio serves /v1/embeddings when an embedding
101 # model is loaded)
102 my $vector = $lm_oai->simple_embedding('Some text to embed');
103  
104 # 4. MCP tool calling (LM Studio supports native tool calling via the
105 # OpenAI-compatible /v1 endpoint for compatible models)
106 use Future::AsyncAwait;
107  
108 my $lm_oai = Langertha::Engine::LMStudioOpenAI->new(
109 url => 'http://localhost:1234/v1',
110 model => 'qwen2.5-7b-instruct-1m',
111 mcp_servers => [$mcp],
112 );
113  
114 my $response = await $lm_oai->chat_with_tools_f('Add 7 and 15');
115  
116 =head1 DESCRIPTION
117  
118 Adapter for LM Studio's OpenAI-compatible local endpoint
119 (C</v1/chat/completions>, C</v1/models>, C</v1/embeddings>).
120  
121 Extends L<Langertha::Engine::OpenAIBase> (which composes
122 L<Langertha::Role::OpenAICompatible>, L<Langertha::Role::OpenAPI>,
123 L<Langertha::Role::Models>, L<Langertha::Role::Temperature>,
124 L<Langertha::Role::ResponseSize>, L<Langertha::Role::SystemPrompt>,
125 L<Langertha::Role::ResponseFormat>, L<Langertha::Role::Streaming>,
126 L<Langertha::Role::Chat>, L<Langertha::Role::ReasoningEffort>, and
127 L<Langertha::Role::PromptCache>); LMStudioOpenAI itself additionally
128 composes L<Langertha::Role::Embedding> (C</v1/embeddings>) and
129 L<Langertha::Role::Tools> (MCP tool calling).
130  
131 L<Langertha::Role::Embedding/embedding_dimensions> is not sent (it carps
132 once): LM Studio does not document a C<dimensions> field for
133 C</v1/embeddings>, so whether it is honored is unknown.
134  
135 Authentication is optional. If C<api_key> (or C<LANGERTHA_LMSTUDIO_API_KEY>)
136 is set, it is sent as a bearer token.
137  
138 B<Runtime metrics:> LM Studio does not expose a Prometheus C</metrics>
139 endpoint, so L<Langertha::Role::Runtime::MetricsPoll> is intentionally
140 B<not> composed here. Sister engines that do (vLLM, SGLang, llama.cpp)
141 expose Prometheus text-format metrics at the server root.
142  
143 =head2 api_key
144  
145 Optional bearer token for LM Studio's OpenAI-compatible endpoint.
146 If not provided, reads from C<LANGERTHA_LMSTUDIO_API_KEY> and otherwise
147 defaults to C<lmstudio>.
148  
149 =head2 model
150  
151 Chat model name. Defaults to C<default>. For real requests, set this to
152 an actually loaded LM Studio model key (for example
153 C<qwen2.5-7b-instruct-1m>).
154  
155 =head1 CAPABILITIES
156  
157 Advertised flags (derived from composed roles via L<Langertha::Role::Capabilities>):
158  
159 =over 4
160  
161 =item * C<chat> — L<Langertha::Role::Chat>
162  
163 =item * C<streaming> — L<Langertha::Role::Streaming>
164  
165 =item * C<tools_native> + C<tool_choice_{auto,any,none,named}> — L<Langertha::Role::Tools>
166  
167 =item * C<embedding> — L<Langertha::Role::Embedding>
168  
169 =item * C<response_format_{json_object,json_schema}> — L<Langertha::Role::ResponseFormat>
170  
171 =item * C<temperature> — L<Langertha::Role::Temperature>
172  
173 =item * C<response_size>, C<system_prompt>, C<parallel_tool_use>, C<context_size>, C<seed>
174 — generation-parameter knobs the engine will honour
175  
176 =back
177  
178 =head1 SEE ALSO
179  
180 =over
181  
182 =item * L<Langertha::Engine::LMStudio> - Native LM Studio API
183  
184 =item * L<Langertha::Engine::OpenAIBase> - Base class for OpenAI-compatible engines
185  
186 =item * L<Langertha::Engine::vLLM> - Sister self-hosted OpenAI-compatible engine (also Embedding)
187  
188 =item * L<Langertha::Engine::LlamaCpp> - Sister self-hosted OpenAI-compatible engine (also Embedding)
189  
190 =back
191  
192 =head1 SUPPORT
193  
194 =head2 Issues
195  
196 Please report bugs and feature requests on GitHub at
197 L<https://github.com/Getty/langertha/issues>.
198  
199 =head2 IRC
200  
201 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
202  
203 =head1 CONTRIBUTING
204  
205 Contributions are welcome! Please fork the repository and submit a pull request.
206  
207 =head1 AUTHOR
208  
209 Torsten Raudssus <getty@cpan.org>
210  
211 =head1 COPYRIGHT AND LICENSE
212  
213 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
214  
215 This is free software; you can redistribute it and/or modify it under
216 the same terms as the Perl 5 programming language system itself.
217  
218 =cut
« blib/lib/Langertha/Engine/AKIAnthropic.pm ↑ Summary blib/lib/Langertha/UsageRecord.pm »