blib/lib/Langertha/Engine/LlamaCpp.pm

filter: Statementstmt 100% 27 / 27 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 33.3% 2 / 6 totaltotal 91.3% 42 / 46 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
_build_knob_wire_format
1.0
CRAP 1.0
« blib/lib/Langertha/Input.pm ↑ Summary blib/lib/Langertha/Engine/OllamaOpenAI.pm »
1 package Langertha::Engine::LlamaCpp;
2 # ABSTRACT: llama.cpp server
3 13 our $VERSION = '0.503';
4 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 extends 'Langertha::Engine::OpenAIBase';
8  
9 13 with 'Langertha::Role::Embedding',
10 'Langertha::Role::Tools',
11 'Langertha::Role::Runtime::MetricsPoll',
12 'Langertha::Role::RuntimeKnobs';
13  
14 # llama.cpp's per-request runtime knobs: cache_prompt, n_cache_reuse, id_slot.
15 # Speculative decoding is a server-launch flag (--spec-draft-*), not a request
16 # knob — see Langertha::Runtime::Knobs.
17 2 ▶sub _build_knob_wire_format { 'llamacpp' }
Subroutine _build_knob_wire_format: called CC=1 SCAR=0.0
18  
19  
20 35 ▶sub default_model { 'default' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
21 # No fixed embedding model: the caller's model, else no model field (k297).
22 5 ▶sub default_embedding_model { undef }
Subroutine default_embedding_model: called CC=1 SCAR=0.0
Pod undocumented
23  
24 # llama-server parses no dimensions field and drops it silently (k319).
25 2 ▶sub _embedding_dimensions_field { undef }
Subroutine _embedding_dimensions_field: called CC=1 SCAR=0.0
26  
27 # LANGERTHA_LLAMACPP_API_KEY is derived from the class name; a local server
28 # needs no key, a --api-key-protected `llama-server` does.
29 3 ▶sub api_key_required { 0 }
Subroutine api_key_required: called CC=1 SCAR=0.0
Pod documented
30  
31 19 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=1 SCAR=0.0
32 19 return $ENV{LANGERTHA_LLAMACPP_API_KEY};
33 }
34  
35 26 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
36 createChatCompletion
37 createEmbedding
38 )]}
39  
40 # llama.cpp's server parses tool_choice as a std::string, so the object (named)
41 # form is SILENTLY downgraded to "auto" — the forced tool never binds, and
42 # `strict` is ignored (ggml-org/llama.cpp common/chat.cpp, verified
43 # 2026-09-01). Clear tool_choice_named so chat_f does not believe it can force a
44 # specific tool here; the string forms (auto/any->required/none) are parsed and
45 # stay.
46 # prompt_cache_key (OpenAI's cache-routing hint) is neither documented for the
47 # server's /v1/chat/completions (tools/server/README.md) nor read by it (no
48 # reference in the source; checked 2026-09-25, karr #200). The server's own
49 # prefix-cache levers are cache_prompt / n_cache_reuse / id_slot via
50 # RuntimeKnobs, so clear the flag.
51 105 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 105 my ( $orig, $self, @rest ) = @_;
53 105 my $caps = $self->$orig(@rest);
54 105 delete @{$caps}{ qw( tool_choice_named prompt_cache_key ) };
55 105 return $caps;
56 13 };
57  
58 # image_input (k266, ADR 0019): self-hosted: the served model is launch state
59 # the client cannot see, so no static claim. A layer-3 catch-all rather than a
60 # layer-2 delete, so a fact probed from /props (modalities.vision) can answer
61 # (ADR 0032). The server has one model, so the fact is stored under whatever
62 # id the probe was asked about (chat_model, 'default' unless set).
63 105 ▶sub model_capability_corrections {
Subroutine model_capability_corrections: called CC=1 SCAR=0.0
Pod undocumented
64 105 return ( qr/\A/ => { image_input => 0 } );
65 }
66  
67 11 ▶sub model_metadata_format { 'llamacpp' }
Subroutine model_metadata_format: called CC=1 SCAR=0.0
Pod undocumented
68 7 ▶sub model_metadata_url {
Subroutine model_metadata_url: called CC=1 SCAR=0.0
Pod undocumented
69 7 return Langertha::ModelProbe->server_root_url( $_[0]->url ) . '/props';
70 }
71  
72 13 __PACKAGE__->meta->make_immutable;
73  
74  
75 13 1;
76  
77 __END__
78  
79 =pod
80  
81 =encoding UTF-8
82  
83 =head1 NAME
84  
85 Langertha::Engine::LlamaCpp - llama.cpp server
86  
87 =head1 VERSION
88  
89 version 0.503
90  
91 =head1 SYNOPSIS
92  
93 use Langertha::Engine::LlamaCpp;
94  
95 my $llama = Langertha::Engine::LlamaCpp->new(
96 url => 'http://localhost:8080/v1',
97 system_prompt => 'You are a helpful assistant',
98 );
99  
100 print $llama->simple_chat('Hello!');
101  
102 my $embedding = $llama->simple_embedding('Some text');
103  
104 =head1 DESCRIPTION
105  
106 Provides access to llama.cpp's built-in HTTP server, which exposes an
107 OpenAI-compatible API. Composes L<Langertha::Role::OpenAICompatible>.
108  
109 Only C<url> is required. The URL must include the C</v1> path prefix
110 (e.g., C<http://localhost:8080/v1>). Since llama.cpp serves exactly one
111 model (loaded at server startup), no model name or API key is needed.
112  
113 Supports chat, streaming, embeddings, and MCP tool calling.
114  
115 L<Langertha::Role::Embedding/embedding_dimensions> is not sent (it carps
116 once): llama.cpp's server does not parse a C<dimensions> field, drops it
117 silently and returns the full vector.
118  
119 See L<https://github.com/ggml-org/llama.cpp/blob/master/examples/server/README.md>
120 for server setup.
121  
122 B<THIS API IS WORK IN PROGRESS>
123  
124 =head1 SEE ALSO
125  
126 =over
127  
128 =item * L<https://github.com/ggml-org/llama.cpp> - llama.cpp project
129  
130 =item * L<Langertha::Engine::vLLM> - Another self-hosted OpenAI-compatible engine
131  
132 =item * L<Langertha::Engine::OllamaOpenAI> - Ollama's OpenAI-compatible API
133  
134 =back
135  
136 =head1 SUPPORT
137  
138 =head2 Issues
139  
140 Please report bugs and feature requests on GitHub at
141 L<https://github.com/Getty/langertha/issues>.
142  
143 =head2 IRC
144  
145 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
146  
147 =head1 CONTRIBUTING
148  
149 Contributions are welcome! Please fork the repository and submit a pull request.
150  
151 =head1 AUTHOR
152  
153 Torsten Raudssus <getty@cpan.org>
154  
155 =head1 COPYRIGHT AND LICENSE
156  
157 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
158  
159 This is free software; you can redistribute it and/or modify it under
160 the same terms as the Perl 5 programming language system itself.
161  
162 =cut
« blib/lib/Langertha/Input.pm ↑ Summary blib/lib/Langertha/Engine/OllamaOpenAI.pm »