A class representing an OpenAI API client but with already prompt-egineered utilities.

Hierarchy

Constructors

Properties

Defaults for API requests. Can be overriden in individual method calls.

cache?: ApiReponseCache<any>

API response cache

cacheDefaults: IResponseCacheOptions = ...

Default options for caching for api requests. Can be overriden in individual method calls.

client: OpenAIApi

API client instance

concurrencyJustReduced: boolean = false
events: EventEmitter = ...

Event emitter for cache events

queue: PromiseQueue<IQueue<(() => Promise<unknown>), IQueueAddOptions>, IQueueAddOptions>

Global queue for sending requests to the openai api.

retryDefaults: IAsyncRetryOptions = ...

Default options for async retry for api requests. Can be overriden in individual method calls.

sendRequest: (<T>(options) => Promise<T>)

Type declaration

    • <T>(options): Promise<T>
    • Generic function for sending requests to the openai api. This is used for all the API endpoints. It handles retrying, cache, hashing, and emitting events. This method is bound to the instance on initialization because it gets wrapped with a concurrency controller in the constructor.

      Type Parameters

      • T

      Parameters

      Returns Promise<T>

concurrencyDefaults: IPromiseQueueOptions<IQueue<(() => Promise<unknown>), IQueueAddOptions>, IQueueAddOptions> = ...

Options for concurrency control. These affect all API requests.

Accessors

Methods

  • Delete all options that are undefined or equal to the default value. The response cache uses hashed options to determine if the request has already been made. Removing default values and undefined values normalizes the options object so it hashes the same.

    Type Parameters

    • T extends Record<string, any>

    Parameters

    • options: T

      The options to delete from.

    • defaults: Record<string, any> = {}

      The default values to compare against.

    Returns T

  • Lower the concurrency to prevent rate limiting. Automatically raises the concurrency again after a delay.

    Parameters

    • lowerBy: number = 7

      The amount to lower the concurrency by.

    • raiseAgainBy: number = 6

      The amount to raise the concurrency by after a delay.

    • delay: number = ...

      The delay to wait before raising the concurrency again. This is randomized by +/- 5 seconds to prevent multiple requests from affecting the concurrency at the same time.

    Returns void

  • Proofread in a given language.

    Parameters

    Returns Promise<string>

    Example

    await openai.proofread('english', 'I no have more money.')
    await openai.proofread('java', `System.out.println("Hello")`)
  • Translate text from English to a given language. For short input text, use options.instruction to provide context.

    Parameters

    Returns Promise<string>

    Example

    await openai.translateEnglishTo('Spanish', 'Clear', { instruction: 'This is a color.' })
    
  • Translate text from one language to another. For short input text, use options.instruction to provide context.

    Parameters

    Returns Promise<string>

    Example

    await openai.translateFrom('English', 'Spanish', 'Clear', { instruction: 'This is a color.' })
    

Generated using TypeDoc