A client for interacting with the OpenAI API.

See

https://beta.openai.com/docs/quickstart

Remarks

This client provides methods for calling the OpenAI API's endpoints for text completion, language generation, and other tasks.

To create an OpenAiClient, use the create method.

The OpenAiClient's methods return Promises that resolve to the JSON response returned by the OpenAI API. If the OpenAI API returns an error, the Promise will be rejected with an instance of an HttpError that includes the error message and HTTP status code.

const openai = OpenAiClient.create();

openai.getCompletion(...)
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});

TODO

Add support for organizations https://beta.openai.com/docs/api-reference/requesting-organization

Hierarchy

  • HttpOpenAiClient

Implements

Constructors

Properties

_apiKey: string

The API key to use when making requests to the OpenAI API.

_url: string

The URL of the OpenAI API that the client is configured to use.

_defaultUrl: string = OPENAPI_API_URL

The default URL to use for the OpenAI API if none is provided when creating a new OpenAiClient instance.

Methods

  • Calls the OpenAI APIs text completion endpoint to generate text based on the given prompt.

    Default values for the optional parameters are selected based on the model.

    Returns

    • A promise that resolves to the response from the OpenAI API.

    Throws

    • If the OpenAI API returns an error.

    Throws

    • If the OpenAI API returns a response in an unexpected format.

    Parameters

    • prompt: string

      The prompt to use for text completion.

    • Optional model: string

      The OpenAI API model to use for text completion.

    • Optional max_tokens: number

      The maximum number of tokens (words and punctuation) to generate in the completion.

    • Optional temperature: number

      Controls the "creativity" of the completion. A higher value means the model will take more risks.

    • Optional top_p: number

      Controls the "confidence" of the completion. A lower value means the model will be more confident in its words.

    • Optional frequency_penalty: number

      Controls the "diversity" of the completion. A higher value means the model will avoid repetition of words.

    • Optional presence_penalty: number

      Controls the "relevance" of the completion. A higher value means the model will try to match the prompt more closely.

    Returns Promise<OpenAiCompletionResponseDTO>

  • Calls the OpenAI APIs text edit endpoint to generate text based on the given input and instruction.

    Default values for the optional parameters are selected based on the model.

    Returns

    • A promise that resolves to the response from the OpenAI API.

    Throws

    • If the OpenAI API returns an error.

    Throws

    • If the OpenAI API returns a response in an unexpected format.

    Parameters

    • instruction: string

      The instruction to use for text editing.

    • Optional input: string

      The input to use for text editing.

    • Optional model: string

      The OpenAI API model to use for text completion.

    • Optional n: number

      The maximum number of tokens (words and punctuation) to generate in the completion.

    • Optional temperature: number

      Controls the "creativity" of the completion. A higher value means the model will take more risks.

    • Optional top_p: number

      Controls the "confidence" of the completion. A lower value means the model will be more confident in its words.

    Returns Promise<OpenAiEditResponseDTO>

  • Private

    Parameters

    • apiKey: string

    Returns {
        Authorization: string;
    }

    • Authorization: string
  • Factory method for creating an instance of OpenAiClient.

    Returns

    • A new instance of OpenAiClient.

    Parameters

    • apiKey: string

      The API key to use when making requests to the OpenAI API.

    • Optional url: string = HttpOpenAiClient._defaultUrl

      The base URL for the OpenAI API.

    Returns HttpOpenAiClient

  • Gets the default URL to use for the OpenAI API when creating new OpenAiClient instances.

    Returns

    The default URL.

    Returns string

  • Sets the default URL to use for the OpenAI API when creating new OpenAiClient instances.

    Parameters

    • url: string

      The URL to set as the default.

    Returns void

Generated using TypeDoc