NAME

Langertha::Engine::OpenAI - OpenAI API

VERSION

version 0.100

SYNOPSIS

use Langertha::Engine::OpenAI;

# Basic chat
my $openai = Langertha::Engine::OpenAI->new(
  api_key => $ENV{OPENAI_API_KEY},
  model => 'gpt-4o-mini',
  system_prompt => 'You are a helpful assistant',
  temperature => 0.7,
);

my $response = $openai->simple_chat('Say something nice');
print $response;

# Embeddings
my $embedding = $openai->embedding('Some text to embed');

# Transcription (Whisper)
my $text = $openai->transcription('/path/to/audio.mp3');

# Async with Future::AsyncAwait
use Future::AsyncAwait;

async sub ask_gpt {
  my $response = await $openai->simple_chat_f('What is Perl?');
  say $response;
}

DESCRIPTION

This module provides access to OpenAI's APIs, including GPT models, embeddings, and Whisper transcription.

Popular Models:

  • gpt-4o-mini - Fast, cost-effective (default for chat)

  • gpt-4o - Most capable GPT-4 model

  • o1 - Advanced reasoning model

  • o3-mini - Fast reasoning model

  • text-embedding-3-large - Embeddings (default)

  • whisper-1 - Audio transcription (default)

Features:

  • Chat completions with streaming

  • Text embeddings

  • Audio transcription (Whisper)

  • Response format control (JSON mode)

  • Temperature and response size control

  • System prompts

  • Async/await support via Future::AsyncAwait

  • Dynamic model discovery via API

THIS API IS WORK IN PROGRESS

LISTING AVAILABLE MODELS

You can dynamically fetch the list of available models from the OpenAI API:

# Get simple list of model IDs
my $model_ids = $engine->list_models;
# Returns: ['gpt-4o', 'gpt-4o-mini', 'o1', ...]

# Get full model objects with metadata
my $models = $engine->list_models(full => 1);
# Returns: [{id => 'gpt-4o', created => 1715367049, ...}, ...]

# Force refresh (bypass cache)
my $models = $engine->list_models(force_refresh => 1);

Caching: Results are cached for 1 hour by default. Configure the TTL:

my $engine = Langertha::Engine::OpenAI->new(
  api_key => $ENV{OPENAI_API_KEY},
  models_cache_ttl => 1800, # 30 minutes
);

# Clear the cache manually
$engine->clear_models_cache;

Deprecation Notice: The all_models() method returns a hardcoded list and is deprecated. Use list_models() for up-to-date model availability.

GETTING AN API KEY

Sign up at https://platform.openai.com/ and generate an API key.

Set the environment variable:

export OPENAI_API_KEY=your-key-here
# Or use LANGERTHA_OPENAI_API_KEY

SEE ALSO

SUPPORT

Issues

Please report bugs and feature requests on GitHub at https://github.com/Getty/langertha/issues.

CONTRIBUTING

Contributions are welcome! Please fork the repository and submit a pull request.

AUTHOR

Torsten Raudssus <torsten@raudssus.de> https://raudss.us/

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Torsten Raudssus.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.