| 1 | package Langertha::Engine::OllamaOpenAI; | ||
| 2 | # ABSTRACT: Ollama via OpenAI-compatible API | ||
| 3 | 63 | our $VERSION = '0.505'; | |
| 4 | 63 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 63 | ▶ | use Carp qw( croak ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 63 | extends 'Langertha::Engine::OpenAIBase'; | |
| 8 | |||
| 9 | # StreamUsage: the OpenAI-compatibility page lists stream_options.include_usage | ||
| 10 | # for /v1/chat/completions; without it the stream has no usage (ADR 0038). | ||
| 11 | 63 | with 'Langertha::Role::Embedding', 'Langertha::Role::Tools', 'Langertha::Role::StreamUsage'; | |
| 12 | |||
| 13 | |||
| 14 | 63 | has '+url' => ( | |
| 15 | required => 1, | ||
| 16 | ); | ||
| 17 | |||
| 18 | 2 | ▶ | sub default_model { croak "".(ref $_[0])." requires model to be set" } |
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||
| 19 | 1 | ▶ | sub default_embedding_model { 'mxbai-embed-large' } |
|
Subroutine
default_embedding_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 20 | |||
| 21 | # Ollama's /v1 takes base64 images only, no image URLs | ||
| 22 | # (docs.ollama.com/api/openai-compatibility), karr k267. | ||
| 23 | 25 | ▶ | sub _content_inline_images_only { 1 } |
|
Subroutine
_content_inline_images_only: called CC=1 SCAR=0.0
| |||
| 24 | |||
| 25 | # Shares the Ollama key (derivation would name LANGERTHA_OLLAMAOPENAI_API_KEY) | ||
| 26 | # and, like the native engine, only needs it for Ollama Cloud. | ||
| 27 | 4 | ▶ | sub api_key_env { 'LANGERTHA_OLLAMA_API_KEY' } |
|
Subroutine
api_key_env: called CC=1 SCAR=0.0
Pod
documented
| |||
| 28 | 4 | ▶ | sub api_key_required { 0 } |
|
Subroutine
api_key_required: called CC=1 SCAR=0.0
Pod
documented
| |||
| 29 | |||
| 30 | 36 | ▶ | sub _build_api_key { |
|
Subroutine
_build_api_key: called CC=1 SCAR=0.0
| |||
| 31 | 36 | return $ENV{LANGERTHA_OLLAMA_API_KEY}; | |
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | 47 | ▶ | sub _build_supported_operations {[qw( createChatCompletion createEmbedding )]} |
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||
| 36 | |||
| 37 | # Ollama's OpenAI-compatible /v1 endpoint does not support tool_choice at all — | ||
| 38 | # its own compatibility checklist marks it unimplemented, and the Go server has | ||
| 39 | # no DisallowUnknownFields, so a tool_choice is accepted, IGNORED, and answered | ||
| 40 | # HTTP 200 (docs.ollama.com/api/openai-compatibility, verified 2026-09-01). A | ||
| 41 | # silently-dropped tool_choice is the dangerous case — the caller believes the | ||
| 42 | # tool was forced — so clear every tool_choice flag; tools_native stays (the | ||
| 43 | # `tools` array itself works). | ||
| 44 | # prompt_cache_key is not a field of Ollama's /v1 ChatCompletionRequest struct | ||
| 45 | # (openai/openai.go; Go's decoder drops it) -- only the /v1/responses shim | ||
| 46 | # echoes it back as null (checked 2026-09-25, karr #200). Clear it too. | ||
| 47 | # parallel_tool_calls is not a field of that struct either (same file, main | ||
| 48 | # 2026-09-24): clear parallel_tool_use (karr k241). The OpenAI envelope sends | ||
| 49 | # neither tool_choice nor parallel_tool_calls without the flag (k239, k241). | ||
| 50 | 350 | ▶ | around engine_capabilities => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 51 | 350 | my ( $orig, $self, @rest ) = @_; | |
| 52 | 350 | my $caps = $self->$orig(@rest); | |
| 53 | 350 | delete @{$caps}{ qw( | |
| 54 | tool_choice_auto tool_choice_any tool_choice_none tool_choice_named | ||
| 55 | prompt_cache_key parallel_tool_use | ||
| 56 | ) }; | ||
| 57 | 350 | return $caps; | |
| 58 | 63 | }; | |
| 59 | |||
| 60 | # image_input (k266, ADR 0019): self-hosted: the served model is launch state | ||
| 61 | # the client cannot see, so no static claim. A layer-3 catch-all rather than a | ||
| 62 | # layer-2 delete, so a fact probed from the server's /api/show can answer per | ||
| 63 | # model (ADR 0032). | ||
| 64 | 350 | ▶ | sub model_capability_corrections { |
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 65 | 350 | return ( qr/\A/ => { image_input => 0 } ); | |
| 66 | } | ||
| 67 | |||
| 68 | 9 | ▶ | sub model_metadata_format { 'ollama' } |
|
Subroutine
model_metadata_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 69 | 6 | ▶ | sub model_metadata_url { |
|
Subroutine
model_metadata_url: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 70 | 6 | return Langertha::ModelProbe->server_root_url( $_[0]->url ) . '/api/show'; | |
| 71 | } | ||
| 72 | |||
| 73 | 63 | __PACKAGE__->meta->make_immutable; | |
| 74 | |||
| 75 | |||
| 76 | 63 | 1; | |
| 77 | |||
| 78 | __END__ | ||