| 1 | package Langertha::Engine::OpenAIBase; | ||
| 2 | # ABSTRACT: Base class for OpenAI-compatible engines | ||
| 3 | 30 | our $VERSION = '0.502'; | |
| 4 | 30 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 30 | ▶ | use Carp qw( croak ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 30 | ▶ | use Module::Runtime qw( use_module ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 30 | extends 'Langertha::Engine::Remote'; | |
| 9 | |||
| 10 | 30 | with map { 'Langertha::Role::'.$_ } qw( | |
| 11 | OpenAICompatible | ||
| 12 | OpenAPI | ||
| 13 | Models | ||
| 14 | Temperature | ||
| 15 | ResponseSize | ||
| 16 | SystemPrompt | ||
| 17 | ResponseFormat | ||
| 18 | Streaming | ||
| 19 | Chat | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub _build_openapi_operations { | ||
| 23 | 42 | ▶ | return use_module('Langertha::Spec::OpenAI')->data; |
|
Subroutine
_build_openapi_operations: called CC=1 SCAR=0.0
| |||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | 1 | ▶ | sub default_model { croak "".(ref $_[0])." requires model to be set" } |
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||
| 28 | |||
| 29 | |||
| 30 | 30 | __PACKAGE__->meta->make_immutable; | |
| 31 | |||
| 32 | |||
| 33 | 30 | 1; | |
| 34 | |||
| 35 | __END__ | ||
| 36 | |||
| 37 | =pod | ||
| 38 | |||
| 39 | =encoding UTF-8 | ||
| 40 | |||
| 41 | =head1 NAME | ||
| 42 | |||
| 43 | Langertha::Engine::OpenAIBase - Base class for OpenAI-compatible engines | ||
| 44 | |||
| 45 | =head1 VERSION | ||
| 46 | |||
| 47 | version 0.502 | ||
| 48 | |||
| 49 | =head1 SYNOPSIS | ||
| 50 | |||
| 51 | package My::CompatibleEngine; | ||
| 52 | use Moose; | ||
| 53 | |||
| 54 | extends 'Langertha::Engine::OpenAIBase'; | ||
| 55 | |||
| 56 | has '+url' => ( default => 'https://api.example.com/v1' ); | ||
| 57 | |||
| 58 | sub _build_api_key { | ||
| 59 | return $ENV{MY_API_KEY} || die "MY_API_KEY required"; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub default_model { 'my-model-v1' } | ||
| 63 | |||
| 64 | __PACKAGE__->meta->make_immutable; | ||
| 65 | |||
| 66 | =head1 DESCRIPTION | ||
| 67 | |||
| 68 | Intermediate base class for all engines that speak the OpenAI | ||
| 69 | C</chat/completions> API format. Extends L<Langertha::Engine::Remote> and | ||
| 70 | composes the full set of OpenAI-compatible roles: | ||
| 71 | L<Langertha::Role::OpenAICompatible>, L<Langertha::Role::OpenAPI>, | ||
| 72 | L<Langertha::Role::Models>, L<Langertha::Role::Temperature>, | ||
| 73 | L<Langertha::Role::ResponseSize>, L<Langertha::Role::SystemPrompt>, | ||
| 74 | L<Langertha::Role::Streaming>, and L<Langertha::Role::Chat>. | ||
| 75 | |||
| 76 | Subclasses must override C<default_model> to return their default model name. | ||
| 77 | They also typically override C<_build_api_key> to read from an environment | ||
| 78 | variable, and C<has '+url'> to supply a default API endpoint. | ||
| 79 | |||
| 80 | Concrete engines that extend this class: | ||
| 81 | |||
| 82 | =over 4 | ||
| 83 | |||
| 84 | =item * Cloud providers — L<Langertha::Engine::OpenAI>, L<Langertha::Engine::DeepSeek>, | ||
| 85 | L<Langertha::Engine::Groq>, L<Langertha::Engine::Mistral>, | ||
| 86 | L<Langertha::Engine::Cerebras>, L<Langertha::Engine::MiniMax>, | ||
| 87 | L<Langertha::Engine::NousResearch>, L<Langertha::Engine::OpenRouter>, | ||
| 88 | L<Langertha::Engine::Replicate>, L<Langertha::Engine::HuggingFace>, | ||
| 89 | L<Langertha::Engine::Perplexity>, L<Langertha::Engine::AKIOpenAI>, | ||
| 90 | L<Langertha::Engine::TSystems>, L<Langertha::Engine::Scaleway> | ||
| 91 | |||
| 92 | =item * Self-hosted — L<Langertha::Engine::OllamaOpenAI>, | ||
| 93 | L<Langertha::Engine::vLLM>, L<Langertha::Engine::SGLang>, | ||
| 94 | L<Langertha::Engine::LlamaCpp>, L<Langertha::Engine::LMStudioOpenAI> | ||
| 95 | |||
| 96 | =back | ||
| 97 | |||
| 98 | For transcription-only engines (Whisper-style) see | ||
| 99 | L<Langertha::Engine::TranscriptionBase>; that base does I<not> | ||
| 100 | compose Chat/Tools/Embedding/ImageGeneration so callers get a focused | ||
| 101 | audio-transcription handle. | ||
| 102 | |||
| 103 | =head2 default_model | ||
| 104 | |||
| 105 | Abstract. Subclasses must override this to return the default model name | ||
| 106 | string. The base implementation croaks with a descriptive error message. | ||
| 107 | |||
| 108 | sub default_model { 'gpt-4o-mini' } | ||
| 109 | |||
| 110 | =head1 SEE ALSO | ||
| 111 | |||
| 112 | =over | ||
| 113 | |||
| 114 | =item * L<Langertha::Engine::Remote> - Parent base class | ||
| 115 | |||
| 116 | =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format (chat, embeddings, tools, streaming) | ||
| 117 | |||
| 118 | =item * L<Langertha::Role::Chat> - C<simple_chat>, C<simple_chat_f>, streaming methods | ||
| 119 | |||
| 120 | =item * L<Langertha::Role::Models> - C<model>, C<models>, C<list_models> | ||
| 121 | |||
| 122 | =item * L<Langertha::Role::Temperature> - C<temperature> attribute | ||
| 123 | |||
| 124 | =item * L<Langertha::Role::ResponseSize> - C<response_size> / C<max_tokens> | ||
| 125 | |||
| 126 | =item * L<Langertha::Role::SystemPrompt> - C<system_prompt> attribute | ||
| 127 | |||
| 128 | =item * L<Langertha::Role::Streaming> - SSE stream parsing | ||
| 129 | |||
| 130 | =item * L<Langertha::Engine::OpenAI> - Canonical OpenAI engine | ||
| 131 | |||
| 132 | =item * L<Langertha::Engine::Groq> - Groq ultra-fast inference | ||
| 133 | |||
| 134 | =item * L<Langertha::Engine::DeepSeek> - DeepSeek reasoning models | ||
| 135 | |||
| 136 | =item * L<Langertha::Engine::OllamaOpenAI> - Ollama OpenAI-compatible endpoint | ||
| 137 | |||
| 138 | =item * L<Langertha::Engine::vLLM> - vLLM high-throughput inference server | ||
| 139 | |||
| 140 | =item * L<Langertha::Engine::SGLang> - SGLang OpenAI-compatible endpoint | ||
| 141 | |||
| 142 | =back | ||
| 143 | |||
| 144 | =head1 SUPPORT | ||
| 145 | |||
| 146 | =head2 Issues | ||
| 147 | |||
| 148 | Please report bugs and feature requests on GitHub at | ||
| 149 | L<https://github.com/Getty/langertha/issues>. | ||
| 150 | |||
| 151 | =head2 IRC | ||
| 152 | |||
| 153 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||
| 154 | |||
| 155 | =head1 CONTRIBUTING | ||
| 156 | |||
| 157 | Contributions are welcome! Please fork the repository and submit a pull request. | ||
| 158 | |||
| 159 | =head1 AUTHOR | ||
| 160 | |||
| 161 | Torsten Raudssus <getty@cpan.org> | ||
| 162 | |||
| 163 | =head1 COPYRIGHT AND LICENSE | ||
| 164 | |||
| 165 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||
| 166 | |||
| 167 | This is free software; you can redistribute it and/or modify it under | ||
| 168 | the same terms as the Perl 5 programming language system itself. | ||
| 169 | |||
| 170 | =cut | ||