| 1 | package Langertha::Engine::LlamaCpp; | ||
| 2 | # ABSTRACT: llama.cpp server | ||
| 3 | 17 | our $VERSION = '0.505'; | |
| 4 | 17 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 17 | ▶ | use Carp qw( croak ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 17 | extends 'Langertha::Engine::OpenAIBase'; | |
| 8 | |||
| 9 | # StreamUsage: the server's request schema has stream_options.include_usage | ||
| 10 | # (tools/server/server-schema.cpp; the README does not list it). Older builds | ||
| 11 | # ignore the unknown key (ADR 0038). | ||
| 12 | 17 | with 'Langertha::Role::Embedding', | |
| 13 | 'Langertha::Role::Tools', | ||
| 14 | 'Langertha::Role::Runtime::MetricsPoll', | ||
| 15 | 'Langertha::Role::RuntimeKnobs', | ||
| 16 | 'Langertha::Role::StreamUsage'; | ||
| 17 | |||
| 18 | # llama.cpp's per-request runtime knobs: cache_prompt, n_cache_reuse, id_slot. | ||
| 19 | # Speculative decoding is a server-launch flag (--spec-draft-*), not a request | ||
| 20 | # knob — see Langertha::Runtime::Knobs. | ||
| 21 | 2 | ▶ | sub _build_knob_wire_format { 'llamacpp' } |
|
Subroutine
_build_knob_wire_format: called CC=1 SCAR=0.0
| |||
| 22 | |||
| 23 | # Thinking on/off goes through the chat template on this server: | ||
| 24 | # chat_template_kwargs.enable_thinking (+ reasoning_effort when on), karr k382. | ||
| 25 | 34 | ▶ | sub _build_reasoning_wire_format { 'chat_template_kwargs' } |
|
Subroutine
_build_reasoning_wire_format: called CC=1 SCAR=0.0
| |||
| 26 | |||
| 27 | |||
| 28 | 60 | ▶ | sub default_model { 'default' } |
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||
| 29 | # No fixed embedding model: the caller's model, else no model field (k297). | ||
| 30 | 5 | ▶ | sub default_embedding_model { undef } |
|
Subroutine
default_embedding_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 31 | |||
| 32 | # llama-server parses no dimensions field and drops it silently (k319). | ||
| 33 | 2 | ▶ | sub _embedding_dimensions_field { undef } |
|
Subroutine
_embedding_dimensions_field: called CC=1 SCAR=0.0
| |||
| 34 | |||
| 35 | # LANGERTHA_LLAMACPP_API_KEY is derived from the class name; a local server | ||
| 36 | # needs no key, a --api-key-protected `llama-server` does. | ||
| 37 | 3 | ▶ | sub api_key_required { 0 } |
|
Subroutine
api_key_required: called CC=1 SCAR=0.0
Pod
documented
| |||
| 38 | |||
| 39 | 44 | ▶ | sub _build_api_key { |
|
Subroutine
_build_api_key: called CC=1 SCAR=0.0
| |||
| 40 | 44 | return $ENV{LANGERTHA_LLAMACPP_API_KEY}; | |
| 41 | } | ||
| 42 | |||
| 43 | 52 | ▶ | sub _build_supported_operations {[qw( |
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||
| 44 | createChatCompletion | ||
| 45 | createEmbedding | ||
| 46 | )]} | ||
| 47 | |||
| 48 | # llama.cpp's server parses tool_choice as a std::string, so the object (named) | ||
| 49 | # form is SILENTLY downgraded to "auto" — the forced tool never binds, and | ||
| 50 | # `strict` is ignored (ggml-org/llama.cpp common/chat.cpp, verified | ||
| 51 | # 2026-09-01). Clear tool_choice_named so chat_f does not believe it can force a | ||
| 52 | # specific tool here; the string forms (auto/any->required/none) are parsed and | ||
| 53 | # stay. | ||
| 54 | # prompt_cache_key (OpenAI's cache-routing hint) is neither documented for the | ||
| 55 | # server's /v1/chat/completions (tools/server/README.md) nor read by it (no | ||
| 56 | # reference in the source; checked 2026-09-25, karr #200). The server's own | ||
| 57 | # prefix-cache levers are cache_prompt / n_cache_reuse / id_slot via | ||
| 58 | # RuntimeKnobs, so clear the flag. | ||
| 59 | 195 | ▶ | around engine_capabilities => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 60 | 195 | my ( $orig, $self, @rest ) = @_; | |
| 61 | 195 | my $caps = $self->$orig(@rest); | |
| 62 | 195 | delete @{$caps}{ qw( tool_choice_named prompt_cache_key ) }; | |
| 63 | 195 | return $caps; | |
| 64 | 17 | }; | |
| 65 | |||
| 66 | # image_input (k266, ADR 0019): self-hosted: the served model is launch state | ||
| 67 | # the client cannot see, so no static claim. A layer-3 catch-all rather than a | ||
| 68 | # layer-2 delete, so a fact probed from /props (modalities.vision) can answer | ||
| 69 | # (ADR 0032). The server has one model, so the fact is stored under whatever | ||
| 70 | # id the probe was asked about (chat_model, 'default' unless set). | ||
| 71 | 195 | ▶ | sub model_capability_corrections { |
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 72 | 195 | return ( qr/\A/ => { image_input => 0 } ); | |
| 73 | } | ||
| 74 | |||
| 75 | 11 | ▶ | sub model_metadata_format { 'llamacpp' } |
|
Subroutine
model_metadata_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 76 | 7 | ▶ | sub model_metadata_url { |
|
Subroutine
model_metadata_url: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 77 | 7 | return Langertha::ModelProbe->server_root_url( $_[0]->url ) . '/props'; | |
| 78 | } | ||
| 79 | |||
| 80 | 17 | __PACKAGE__->meta->make_immutable; | |
| 81 | |||
| 82 | |||
| 83 | 17 | 1; | |
| 84 | |||
| 85 | __END__ | ||