| 1 | package Langertha::Engine::OllamaOpenAI; | ||
| 2 | # ABSTRACT: Ollama via OpenAI-compatible API | ||
| 3 | 58 | our $VERSION = '0.503'; | |
| 4 | 58 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 58 | ▶ | use Carp qw( croak ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 58 | extends 'Langertha::Engine::OpenAIBase'; | |
| 8 | |||
| 9 | 58 | with 'Langertha::Role::Embedding', 'Langertha::Role::Tools'; | |
| 10 | |||
| 11 | |||
| 12 | 58 | has '+url' => ( | |
| 13 | required => 1, | ||
| 14 | ); | ||
| 15 | |||
| 16 | 2 | ▶ | sub default_model { croak "".(ref $_[0])." requires model to be set" } |
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||
| 17 | 1 | ▶ | sub default_embedding_model { 'mxbai-embed-large' } |
|
Subroutine
default_embedding_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 18 | |||
| 19 | # Ollama's /v1 takes base64 images only, no image URLs | ||
| 20 | # (docs.ollama.com/api/openai-compatibility), karr k267. | ||
| 21 | 25 | ▶ | sub _content_inline_images_only { 1 } |
|
Subroutine
_content_inline_images_only: called CC=1 SCAR=0.0
| |||
| 22 | |||
| 23 | # Shares the Ollama key (derivation would name LANGERTHA_OLLAMAOPENAI_API_KEY) | ||
| 24 | # and, like the native engine, only needs it for Ollama Cloud. | ||
| 25 | 4 | ▶ | sub api_key_env { 'LANGERTHA_OLLAMA_API_KEY' } |
|
Subroutine
api_key_env: called CC=1 SCAR=0.0
Pod
documented
| |||
| 26 | 4 | ▶ | sub api_key_required { 0 } |
|
Subroutine
api_key_required: called CC=1 SCAR=0.0
Pod
documented
| |||
| 27 | |||
| 28 | 36 | ▶ | sub _build_api_key { |
|
Subroutine
_build_api_key: called CC=1 SCAR=0.0
| |||
| 29 | 36 | return $ENV{LANGERTHA_OLLAMA_API_KEY}; | |
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | 46 | ▶ | sub _build_supported_operations {[qw( createChatCompletion createEmbedding )]} |
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||
| 34 | |||
| 35 | # Ollama's OpenAI-compatible /v1 endpoint does not support tool_choice at all — | ||
| 36 | # its own compatibility checklist marks it unimplemented, and the Go server has | ||
| 37 | # no DisallowUnknownFields, so a tool_choice is accepted, IGNORED, and answered | ||
| 38 | # HTTP 200 (docs.ollama.com/api/openai-compatibility, verified 2026-09-01). A | ||
| 39 | # silently-dropped tool_choice is the dangerous case — the caller believes the | ||
| 40 | # tool was forced — so clear every tool_choice flag; tools_native stays (the | ||
| 41 | # `tools` array itself works). | ||
| 42 | # prompt_cache_key is not a field of Ollama's /v1 ChatCompletionRequest struct | ||
| 43 | # (openai/openai.go; Go's decoder drops it) -- only the /v1/responses shim | ||
| 44 | # echoes it back as null (checked 2026-09-25, karr #200). Clear it too. | ||
| 45 | # parallel_tool_calls is not a field of that struct either (same file, main | ||
| 46 | # 2026-09-24): clear parallel_tool_use (karr k241). The OpenAI envelope sends | ||
| 47 | # neither tool_choice nor parallel_tool_calls without the flag (k239, k241). | ||
| 48 | 307 | ▶ | around engine_capabilities => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 49 | 307 | my ( $orig, $self, @rest ) = @_; | |
| 50 | 307 | my $caps = $self->$orig(@rest); | |
| 51 | 307 | delete @{$caps}{ qw( | |
| 52 | tool_choice_auto tool_choice_any tool_choice_none tool_choice_named | ||
| 53 | prompt_cache_key parallel_tool_use | ||
| 54 | ) }; | ||
| 55 | 307 | return $caps; | |
| 56 | 58 | }; | |
| 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 the server's /api/show can answer per | ||
| 61 | # model (ADR 0032). | ||
| 62 | 307 | ▶ | sub model_capability_corrections { |
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 63 | 307 | return ( qr/\A/ => { image_input => 0 } ); | |
| 64 | } | ||
| 65 | |||
| 66 | 9 | ▶ | sub model_metadata_format { 'ollama' } |
|
Subroutine
model_metadata_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 67 | 6 | ▶ | sub model_metadata_url { |
|
Subroutine
model_metadata_url: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 68 | 6 | return Langertha::ModelProbe->server_root_url( $_[0]->url ) . '/api/show'; | |
| 69 | } | ||
| 70 | |||
| 71 | 58 | __PACKAGE__->meta->make_immutable; | |
| 72 | |||
| 73 | |||
| 74 | 58 | 1; | |
| 75 | |||
| 76 | __END__ | ||
| 77 | |||
| 78 | =pod | ||
| 79 | |||
| 80 | =encoding UTF-8 | ||
| 81 | |||
| 82 | =head1 NAME | ||
| 83 | |||
| 84 | Langertha::Engine::OllamaOpenAI - Ollama via OpenAI-compatible API | ||
| 85 | |||
| 86 | =head1 VERSION | ||
| 87 | |||
| 88 | version 0.503 | ||
| 89 | |||
| 90 | =head1 SYNOPSIS | ||
| 91 | |||
| 92 | use Langertha::Engine::OllamaOpenAI; | ||
| 93 | |||
| 94 | # Direct construction (url with /v1 suffix is required) | ||
| 95 | my $ollama_oai = Langertha::Engine::OllamaOpenAI->new( | ||
| 96 | url => 'http://localhost:11434/v1', | ||
| 97 | model => 'llama3.3', | ||
| 98 | ); | ||
| 99 | |||
| 100 | print $ollama_oai->simple_chat('Hello!'); | ||
| 101 | |||
| 102 | # Streaming | ||
| 103 | $ollama_oai->simple_chat_stream(sub { | ||
| 104 | print shift->content; | ||
| 105 | }, 'Tell me about Perl'); | ||
| 106 | |||
| 107 | # Preferred: create via Ollama's openai() method (appends /v1 automatically) | ||
| 108 | use Langertha::Engine::Ollama; | ||
| 109 | |||
| 110 | my $ollama = Langertha::Engine::Ollama->new( | ||
| 111 | url => 'http://localhost:11434', | ||
| 112 | model => 'llama3.3', | ||
| 113 | ); | ||
| 114 | my $oai = $ollama->openai; | ||
| 115 | print $oai->simple_chat('Hello via OpenAI format!'); | ||
| 116 | |||
| 117 | =head1 DESCRIPTION | ||
| 118 | |||
| 119 | Provides access to Ollama's OpenAI-compatible C</v1> API endpoint. Composes | ||
| 120 | L<Langertha::Role::OpenAICompatible> for the standard OpenAI format. | ||
| 121 | |||
| 122 | C<url> is required and must include the C</v1> path prefix (e.g., | ||
| 123 | C<http://localhost:11434/v1>). When using L<Langertha::Engine::Ollama/openai>, | ||
| 124 | the C</v1> suffix is appended automatically. | ||
| 125 | |||
| 126 | Authentication is optional. A local server needs none; Ollama Cloud | ||
| 127 | (C<https://ollama.com/v1>) requires a bearer token — set L</api_key> or | ||
| 128 | C<LANGERTHA_OLLAMA_API_KEY>. | ||
| 129 | |||
| 130 | Supports chat completions (SSE streaming), embeddings (default: | ||
| 131 | C<mxbai-embed-large>), MCP tool calling, and dynamic model listing. | ||
| 132 | Transcription is not supported. | ||
| 133 | |||
| 134 | For the native Ollama API with C<keep_alive>, C<seed>, C<context_size>, | ||
| 135 | NDJSON streaming, and Hermes tool calling, use L<Langertha::Engine::Ollama>. | ||
| 136 | |||
| 137 | B<THIS API IS WORK IN PROGRESS> | ||
| 138 | |||
| 139 | =head2 api_key | ||
| 140 | |||
| 141 | Optional bearer token, shared with L<Langertha::Engine::Ollama>. A local | ||
| 142 | server needs none; Ollama Cloud rejects unauthenticated requests with | ||
| 143 | HTTP 401. If not provided, reads from C<LANGERTHA_OLLAMA_API_KEY>. When | ||
| 144 | undefined, L<Langertha::Role::OpenAICompatible> sends no C<Authorization> | ||
| 145 | header. | ||
| 146 | |||
| 147 | =head1 SEE ALSO | ||
| 148 | |||
| 149 | =over | ||
| 150 | |||
| 151 | =item * L<Langertha::Engine::Ollama> - Native Ollama API (with keep_alive, seed, context_size) | ||
| 152 | |||
| 153 | =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role composed by this engine | ||
| 154 | |||
| 155 | =item * L<https://github.com/ollama/ollama> - Ollama project | ||
| 156 | |||
| 157 | =back | ||
| 158 | |||
| 159 | =head1 SUPPORT | ||
| 160 | |||
| 161 | =head2 Issues | ||
| 162 | |||
| 163 | Please report bugs and feature requests on GitHub at | ||
| 164 | L<https://github.com/Getty/langertha/issues>. | ||
| 165 | |||
| 166 | =head2 IRC | ||
| 167 | |||
| 168 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||
| 169 | |||
| 170 | =head1 CONTRIBUTING | ||
| 171 | |||
| 172 | Contributions are welcome! Please fork the repository and submit a pull request. | ||
| 173 | |||
| 174 | =head1 AUTHOR | ||
| 175 | |||
| 176 | Torsten Raudssus <getty@cpan.org> | ||
| 177 | |||
| 178 | =head1 COPYRIGHT AND LICENSE | ||
| 179 | |||
| 180 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||
| 181 | |||
| 182 | This is free software; you can redistribute it and/or modify it under | ||
| 183 | the same terms as the Perl 5 programming language system itself. | ||
| 184 | |||
| 185 | =cut | ||