| 1 | package Langertha::Engine::OpenAI; | ||||||||||||||||||||
| 2 | # ABSTRACT: OpenAI API | ||||||||||||||||||||
| 3 | 25 | our $VERSION = '0.502'; | |||||||||||||||||||
| 4 | 25 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 25 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 25 | ▶ | use Langertha::Engine::TranscriptionBase; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 25 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 25 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||
| 11 | Embedding | ||||||||||||||||||||
| 12 | Transcription | ||||||||||||||||||||
| 13 | ImageGeneration | ||||||||||||||||||||
| 14 | Tools | ||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 25 | has compatibility_for_engine => ( | |||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||
| 20 | predicate => 'has_compatibility_for_engine', | ||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | has '+url' => ( | ||||||||||||||||||||
| 25 | lazy => 1, | ||||||||||||||||||||
| 26 | 18 | ▶ | default => sub { 'https://api.openai.com/v1' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 25 | ); | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | sub _build_api_key { | ||||||||||||||||||||
| 30 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=8.1
| |||||||||||||||||||||
| 31 | return $ENV{LANGERTHA_OPENAI_API_KEY} | ||||||||||||||||||||
| 32 | 1 | ▶ | || croak "".(ref $self)." requires LANGERTHA_OPENAI_API_KEY or api_key set"; | ||||||||||||||||||
|
Condition33%1/3
$ENV{'LANGERTHA_OPENAI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
MC/DC0%0/2
$ENV{'LANGERTHA_OPENAI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
$ENV{'LANGERTHA_OPENAI_API_KEY'}
croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_OPENAI_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_OPENAI_API_KEY or api_key set')
| |||||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 2 | ▶ | sub default_model { 'gpt-4o-mini' } | ||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 25 | has whisper => ( | |||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||
| 39 | isa => 'Langertha::Engine::TranscriptionBase', | ||||||||||||||||||||
| 40 | lazy_build => 1, | ||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | sub _build_whisper { | ||||||||||||||||||||
| 44 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_build_whisper: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 45 | 0 | return Langertha::Engine::TranscriptionBase->new( | |||||||||||||||||||
| 46 | api_key => $self->api_key, | ||||||||||||||||||||
| 47 | url => $self->url, | ||||||||||||||||||||
| 48 | transcription_model => 'whisper-1', | ||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 25 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 25 | 1; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | __END__ | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =pod | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =encoding UTF-8 | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =head1 NAME | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | Langertha::Engine::OpenAI - OpenAI API | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =head1 VERSION | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | version 0.502 | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head1 SYNOPSIS | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | use Langertha::Engine::OpenAI; | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | my $openai = Langertha::Engine::OpenAI->new( | ||||||||||||||||||||
| 77 | api_key => $ENV{OPENAI_API_KEY}, | ||||||||||||||||||||
| 78 | model => 'gpt-4o-mini', | ||||||||||||||||||||
| 79 | system_prompt => 'You are a helpful assistant', | ||||||||||||||||||||
| 80 | temperature => 0.7, | ||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | my $response = $openai->simple_chat('Say something nice'); | ||||||||||||||||||||
| 84 | print $response; | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # Embeddings | ||||||||||||||||||||
| 87 | my $embedding = $openai->embedding('Some text to embed'); | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | # Transcription (Whisper) | ||||||||||||||||||||
| 90 | my $text = $openai->transcription('/path/to/audio.mp3'); | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | # Async with Future::AsyncAwait | ||||||||||||||||||||
| 93 | use Future::AsyncAwait; | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | async sub ask_gpt { | ||||||||||||||||||||
| 96 | my $response = await $openai->simple_chat_f('What is Perl?'); | ||||||||||||||||||||
| 97 | say $response; | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =head1 DESCRIPTION | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | Provides access to OpenAI's APIs, including GPT models, embeddings, and | ||||||||||||||||||||
| 103 | Whisper transcription. Composes L<Langertha::Role::OpenAICompatible> for the | ||||||||||||||||||||
| 104 | standard OpenAI API format. | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | Popular models: C<gpt-4o-mini> (default, fast), C<gpt-4o> (most capable), | ||||||||||||||||||||
| 107 | C<o1>/C<o3-mini> (reasoning), C<text-embedding-3-large> (embeddings), | ||||||||||||||||||||
| 108 | C<whisper-1> (transcription). | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | Dynamic model listing is supported via L<Langertha::Role::Models/list_models>. | ||||||||||||||||||||
| 111 | Results are cached for C<models_cache_ttl> seconds (default: 3600). | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | Get your API key at L<https://platform.openai.com/> and set | ||||||||||||||||||||
| 114 | C<LANGERTHA_OPENAI_API_KEY> in your environment. | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head2 compatibility_for_engine | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | Optional identifier of the engine this instance is acting as a compatibility | ||||||||||||||||||||
| 121 | shim for. Used internally when one engine is accessed via another's OpenAI | ||||||||||||||||||||
| 122 | endpoint. | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =head2 whisper | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | Lazy-built L<Langertha::Engine::TranscriptionBase> instance bound to | ||||||||||||||||||||
| 127 | this engine's C<api_key> and C<url>, defaulting to model C<whisper-1>. | ||||||||||||||||||||
| 128 | Useful when you have an OpenAI engine handy and want a focused | ||||||||||||||||||||
| 129 | transcription handle without re-stating credentials: | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | my $text = $openai->whisper->simple_transcription('/path/audio.mp3'); | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | The transcription engine is fully independent: configure it directly | ||||||||||||||||||||
| 134 | (C<temperature>, C<language>, etc.) via the returned object, or | ||||||||||||||||||||
| 135 | construct your own L<Langertha::Engine::TranscriptionBase> if you need | ||||||||||||||||||||
| 136 | a different model or endpoint. | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =head1 SEE ALSO | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =over | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =item * L<https://status.openai.com/> - OpenAI service status | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =item * L<https://platform.openai.com/docs> - Official OpenAI documentation | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role composed by this engine | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =item * L<Langertha::Role::Tools> - MCP tool calling interface | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =item * L<Langertha::Engine::DeepSeek> - DeepSeek (via OpenAICompatible role) | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =item * L<Langertha::Engine::Groq> - Groq (via OpenAICompatible role) | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =item * L<Langertha::Engine::Mistral> - Mistral (via OpenAICompatible role) | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =item * L<Langertha::Engine::vLLM> - vLLM inference server (via OpenAICompatible role) | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =item * L<Langertha::Engine::NousResearch> - Nous Research (via OpenAICompatible role) | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =item * L<Langertha::Engine::Perplexity> - Perplexity Sonar (via OpenAICompatible role) | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =back | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head1 SUPPORT | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =head2 Issues | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||
| 169 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =head2 IRC | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =head1 CONTRIBUTING | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =head1 AUTHOR | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 188 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||