NAME

OpenAI::API - Perl interface to OpenAI API

SYNOPSIS

use OpenAI::API;

my $openai = OpenAI::API->new(); # uses OPENAI_API_KEY environment variable

my $chat = $openai->chat(
    model       => 'gpt-3.5-turbo',
    messages    => [ { "role" => "user", "content" => "Hello!" }, ],
);

my $completions = $openai->completions(
    model  => 'text-davinci-003',
    prompt => 'What is the capital of France?',
);

my $edits = $openai->edits(
    model       => 'text-davinci-edit-001',
    input       => 'What day of the wek is it?',
    instruction => 'Fix the spelling mistakes',
);

my $images = $openai->image_create(
    prompt => 'A cute baby sea otter',
    n      => 2,
    size   => '1024x1024',
);

my $models = $openai->files();
my $model  = $openai->file_retrieve( item_id => 'file-xxxxxxxxxx' );

my $models = $openai->models();
my $model  = $openai->model_retrieve( model => 'text-davinci-003' );

my $moderations = $openai->moderations(
    model => 'text-moderation-latest',
    input => 'I like turtles',
);

DESCRIPTION

OpenAI::API is a Perl module that provides an interface to the OpenAI API, which allows you to generate text, translate languages, summarize text, and perform other tasks using the language models developed by OpenAI.

To use the OpenAI::API module, you will need an API key, which you can obtain by signing up for an account on the OpenAI website.

METHODS

new()

Creates a new OpenAI::API object.

  • api_key [optional]

    Your API key. Default: $ENV{OPENAI_API_KEY}.

    Attention: never commit API keys to your repository. Use the OPENAI_API_KEY environment variable instead.

    See: Best Practices for API Key Safety.

  • api_base [optional]

    The api_base URL for the OpenAI API. Default: 'https://api.openai.com/v1/'.

  • timeout [optional]

    The timeout value, in seconds. Default: 60 seconds.

chat()

Given a chat conversation, the model will return a chat completion response.

Mandatory parameters:

  • model

  • messages

More info: OpenAI::API::Resource::Chat

completions()

Given a prompt, the model will return one or more predicted completions.

Mandatory parameters:

  • model

  • prompt

More info: OpenAI::API::Resource::Completion

edits()

Creates a new edit for the provided input, instruction, and parameters.

Mandatory parameters:

  • model

  • instruction

  • input [optional, but often required]

More info: OpenAI::API::Resource::Edit

embeddings()

Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.

Mandatory parameters:

  • model

  • input

More info: OpenAI::API::Resource::Embedding

image_create()

Creates an image given a prompt.

Mandatory parameters:

  • prompt

More info: OpenAI::API::Resource::Image::Generation

models()

Lists the currently available models, and provides basic information about each one such as the owner and availability.

More info: OpenAI::API::Resource::Model::List

model_retrieve()

Retrieves a model instance, providing basic information about the model such as the owner and permissioning.

Mandatory parameters:

  • model

More info: OpenAI::API::Resource::Model::Retrieve

moderations()

Given a input text, outputs if the model classifies it as violating OpenAI's content policy.

Mandatory parameters:

  • input

More info: OpenAI::API::Resource::Moderation

SEE ALSO

OpenAI Reference Overview

AUTHOR

Nelson Ferraz <nferraz@gmail.com>

SUPPORT

This module is developed on GitHub.

Send ideas, feedback, tasks, or bugs to GitHub Issues.

COPYRIGHT AND LICENSE

Copyright (C) 2022, 2023 by Nelson Ferraz

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.30.2 or, at your option, any later version of Perl 5 you may have available.