| 1 | package Langertha::Engine::LMStudioAnthropic; | ||||||||||||
| 2 | # ABSTRACT: LM Studio via Anthropic-compatible API | ||||||||||||
| 3 | 13 | our $VERSION = '0.503'; | |||||||||||
| 4 | 13 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 13 | extends 'Langertha::Engine::AnthropicBase'; | |||||||||||
| 7 | |||||||||||||
| 8 | |||||||||||||
| 9 | has '+url' => ( | ||||||||||||
| 10 | lazy => 1, | ||||||||||||
| 11 | 1 | ▶ | default => sub { 'http://localhost:1234' }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 13 | ); | |||||||||||
| 13 | |||||||||||||
| 14 | 8 | ▶ | sub _build_api_key { | ||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=6.9
| |||||||||||||
| 15 | 8 | ▶ | return $ENV{LANGERTHA_LMSTUDIO_API_KEY} || 'lmstudio'; | ||||||||||
|
Condition100%2/2
$ENV{'LANGERTHA_LMSTUDIO_API_KEY'} || 'lmstudio'
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'}
| |||||||||||||
| 16 | } | ||||||||||||
| 17 | |||||||||||||
| 18 | |||||||||||||
| 19 | 15 | ▶ | sub default_model { 'default' } | ||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 20 | |||||||||||||
| 21 | # Shares the LM Studio key with the native engine (derivation would name the | ||||||||||||
| 22 | # protocol variant); optional, the local server accepts the 'lmstudio' dummy. | ||||||||||||
| 23 | 1 | ▶ | sub api_key_env { 'LANGERTHA_LMSTUDIO_API_KEY' } | ||||||||||
|
Subroutine
api_key_env: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 24 | 1 | ▶ | sub api_key_required { 0 } | ||||||||||
|
Subroutine
api_key_required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 25 | |||||||||||||
| 26 | # image_input (k266, ADR 0019): self-hosted: the served model is launch state | ||||||||||||
| 27 | # the client cannot see, so no static claim. A layer-3 catch-all rather than a | ||||||||||||
| 28 | # layer-2 delete, so a fact probed from the server's native /api/v1/models | ||||||||||||
| 29 | # (capabilities.vision) can answer per model (ADR 0032), as on the other two | ||||||||||||
| 30 | # LM Studio faces. | ||||||||||||
| 31 | 136 | ▶ | sub model_capability_corrections { | ||||||||||
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 32 | 136 | return ( qr/\A/ => { image_input => 0 } ); | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | # Source blocks in a tool_result (karr k372, ADR 0001 k372 Update): off, | ||||||||||||
| 36 | # conservatively, like MoonshotAnthropic. NOT live-verified (no LM Studio | ||||||||||||
| 37 | # server available); the evidence is indirect: the anthropic-compat docs | ||||||||||||
| 38 | # (lmstudio.ai/docs/developer/anthropic-compat) only point to Anthropic's own, | ||||||||||||
| 39 | # the changelog names only "Images in tool call results" for /v1/messages, and | ||||||||||||
| 40 | # lmstudio-bug-tracker#1792 (LM Studio 0.4.11, Apr 2026) reports the API | ||||||||||||
| 41 | # rejecting PDFs with a 400 even for vision models and offering no document | ||||||||||||
| 42 | # type. A text resource (the common MCP case) goes out as a text block, a PDF | ||||||||||||
| 43 | # as the k336 placeholder, a native document / search_result as its text. | ||||||||||||
| 44 | # Wire truth, not image_input: a vision model gets the same treatment. | ||||||||||||
| 45 | 7 | ▶ | sub _tool_result_source_blocks_on_wire { 0 } | ||||||||||
|
Subroutine
_tool_result_source_blocks_on_wire: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | |||||||||||||
| 47 | # url is the server root (the envelope appends /v1/messages); the native | ||||||||||||
| 48 | # models list sits beside it. | ||||||||||||
| 49 | 15 | ▶ | sub model_metadata_format { 'lmstudio' } | ||||||||||
|
Subroutine
model_metadata_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 50 | 71 | ▶ | sub model_metadata_url { | ||||||||||
|
Subroutine
model_metadata_url: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 51 | 71 | return Langertha::ModelProbe->server_root_url( $_[0]->url ) . '/api/v1/models'; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | # LM Studio documents only "Authorization: Bearer" for its native REST API | ||||||||||||
| 55 | # (lmstudio.ai/docs/developer/core/authentication); x-api-key is documented for | ||||||||||||
| 56 | # /v1/messages. So the probe request also carries the token as Bearer, the chat | ||||||||||||
| 57 | # wire is unchanged. | ||||||||||||
| 58 | 62 | ▶ | around update_request => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 59 | 62 | my ( $orig, $self, $request ) = @_; | |||||||||||
| 60 | 62 | $self->$orig($request); | |||||||||||
| 61 | 62 | ▶ | $request->header( 'Authorization', 'Bearer ' . $self->api_key ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | if $request->uri->as_string eq $self->model_metadata_url; | ||||||||||||
| 63 | 62 | return; | |||||||||||
| 64 | 13 | }; | |||||||||||
| 65 | |||||||||||||
| 66 | 13 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 67 | |||||||||||||
| 68 | |||||||||||||
| 69 | 13 | 1; | |||||||||||
| 70 | |||||||||||||
| 71 | __END__ | ||||||||||||
| 72 | |||||||||||||
| 73 | =pod | ||||||||||||
| 74 | |||||||||||||
| 75 | =encoding UTF-8 | ||||||||||||
| 76 | |||||||||||||
| 77 | =head1 NAME | ||||||||||||
| 78 | |||||||||||||
| 79 | Langertha::Engine::LMStudioAnthropic - LM Studio via Anthropic-compatible API | ||||||||||||
| 80 | |||||||||||||
| 81 | =head1 VERSION | ||||||||||||
| 82 | |||||||||||||
| 83 | version 0.503 | ||||||||||||
| 84 | |||||||||||||
| 85 | =head1 SYNOPSIS | ||||||||||||
| 86 | |||||||||||||
| 87 | use Langertha::Engine::LMStudioAnthropic; | ||||||||||||
| 88 | |||||||||||||
| 89 | my $lm_anthropic = Langertha::Engine::LMStudioAnthropic->new( | ||||||||||||
| 90 | url => 'http://localhost:1234', | ||||||||||||
| 91 | model => 'qwen2.5-7b-instruct-1m', | ||||||||||||
| 92 | ); | ||||||||||||
| 93 | |||||||||||||
| 94 | print $lm_anthropic->simple_chat('Hello from Anthropic-compatible endpoint'); | ||||||||||||
| 95 | |||||||||||||
| 96 | =head1 DESCRIPTION | ||||||||||||
| 97 | |||||||||||||
| 98 | Adapter for LM Studio's Anthropic-compatible local endpoint | ||||||||||||
| 99 | (C<POST /v1/messages> on the LM Studio server URL, default | ||||||||||||
| 100 | C<http://localhost:1234>). | ||||||||||||
| 101 | |||||||||||||
| 102 | LM Studio requires a non-empty C<x-api-key> header for this endpoint, but the | ||||||||||||
| 103 | value is not validated against Anthropic. This class defaults to C<lmstudio> | ||||||||||||
| 104 | when no API key is configured. | ||||||||||||
| 105 | |||||||||||||
| 106 | A tool result that carries an embedded text resource or PDF goes out as plain | ||||||||||||
| 107 | text (the PDF as a placeholder): LM Studio documents no C<document> or | ||||||||||||
| 108 | C<search_result> block inside a C<tool_result>, and its bug tracker reports PDFs | ||||||||||||
| 109 | rejected with a 400. This is conservative and not live-verified. | ||||||||||||
| 110 | |||||||||||||
| 111 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||
| 112 | |||||||||||||
| 113 | =head2 api_key | ||||||||||||
| 114 | |||||||||||||
| 115 | API key sent as C<x-api-key> to the Anthropic-compatible endpoint. | ||||||||||||
| 116 | LM Studio accepts arbitrary non-empty values. Defaults to C<lmstudio> when | ||||||||||||
| 117 | no explicit C<api_key> and no C<LANGERTHA_LMSTUDIO_API_KEY> are set. | ||||||||||||
| 118 | |||||||||||||
| 119 | =head1 SEE ALSO | ||||||||||||
| 120 | |||||||||||||
| 121 | =over | ||||||||||||
| 122 | |||||||||||||
| 123 | =item * L<Langertha::Engine::LMStudio> - Native LM Studio API (C</api/v1/chat>) | ||||||||||||
| 124 | |||||||||||||
| 125 | =item * L<Langertha::Engine::AnthropicBase> - Base Anthropic-compatible engine | ||||||||||||
| 126 | |||||||||||||
| 127 | =back | ||||||||||||
| 128 | |||||||||||||
| 129 | =head1 SUPPORT | ||||||||||||
| 130 | |||||||||||||
| 131 | =head2 Issues | ||||||||||||
| 132 | |||||||||||||
| 133 | Please report bugs and feature requests on GitHub at | ||||||||||||
| 134 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||
| 135 | |||||||||||||
| 136 | =head2 IRC | ||||||||||||
| 137 | |||||||||||||
| 138 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||
| 139 | |||||||||||||
| 140 | =head1 CONTRIBUTING | ||||||||||||
| 141 | |||||||||||||
| 142 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||
| 143 | |||||||||||||
| 144 | =head1 AUTHOR | ||||||||||||
| 145 | |||||||||||||
| 146 | Torsten Raudssus <getty@cpan.org> | ||||||||||||
| 147 | |||||||||||||
| 148 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 149 | |||||||||||||
| 150 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||
| 151 | |||||||||||||
| 152 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 153 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 154 | |||||||||||||
| 155 | =cut | ||||||||||||