NAME

OpenAI::API::Request::Chat - chat endpoint

SYNOPSIS

use OpenAI::API::Request::Chat;

my $request = OpenAI::API::Request::Chat->new(
    model    => "gpt-3.5-turbo",
    messages => [
        { "role" => "system",    "content" => "You are a helpful assistant." },
        { "role" => "user",      "content" => "Who won the world series in 2020?" },
        { "role" => "assistant", "content" => "The Los Angeles Dodgers won the World Series in 2020." },
        { "role" => "user",      "content" => "Where was it played?" }
    ],
);

my $res = $request->send();

my $message = $res->{choices}[0]{message};

DESCRIPTION

Given a chat conversation, the model will return a chat completion response (similar to ChatGPT).

METHODS

new()

  • model

    ID of the model to use.

    See Models overview for a reference of them.

  • messages

    The messages to generate chat completions for, in the chat format.

  • max_tokens [optional]

    The maximum number of tokens to generate.

    Most models have a context length of 2048 tokens (except for the newest models, which support 4096.

  • temperature [optional]

    What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

  • top_p [optional]

    An alternative to sampling with temperature, called nucleus sampling.

    We generally recommend altering this or temperature but not both.

  • n [optional]

    How many completions to generate for each prompt.

    Use carefully and ensure that you have reasonable settings for max_tokens and stop.

  • stop [optional]

    Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.

  • frequency_penalty [optional]

    Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far.

  • presence_penalty [optional]

    Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.

  • user [optional]

    A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.

send()

Sends the request and returns a data structured similar to the one documented in the API reference.

SEE ALSO

OpenAI API Reference: Chat