• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimal and opinionated OpenAI client powered by fetch.

OpenAI Fetch Client

Build Status npm version

A minimal and opinionated OpenAI client powered by fetch.

Unfortunately, the official openai-node uses Axios, which only supports Node and is bloated.

Reasons to consider using openai-fetch:

  • Supports all envs with native fetch: Node 18+, browsers, Deno, Cloudflare Workers, etc
  • Package size: openai-fetch is ~5kb and openai-node is ~180kb
  • openai-fetch includes the first choice in the response (no repetitive: response.choices[0.text])
  • You only need the completions, edits, and embeddings endpoints

Use openai-node if you need:

  • Endpoints other than completions, edits, and embeddings
  • Streaming response support (this may be added later)
  • Responses to contain more than one choice (no n support)
  • To use tokens instead of strings for input

Usage

Install openai-fetch with your favorite package manager and create an instance of the OpenAIClient class.

import { OpenAIClient } from 'openai-fetch';

const client = new OpenAIClient({ apiKey: '<your api key>' });

The apiKey is optional and will be read from process.env.OPENAI_API_KEY if present.

API

The API follows OpenAI very closely, so their reference documentation can generally be used. Everything is strongly typed, so you will know if anything is different as soon as TypeScript parses your code.

Create Completion

See: OpenAI docs | Type definitions

client.createCompletion(params: CompletionParams): Promise<{
  /** The completion string. */
  completion: string;
  /** The raw response from the API. */
  response: CompletionResponse;
}>

To get a streaming response, use the streamCompletion method.

client.streamCompletion(params: CompletionParams): Promise<
    ReadableStream<{
      /** The completion string. */
      completion: string;
      /** The raw response from the API. */
      response: CompletionResponse;
    }>
  >

Create Chat Completion

See: OpenAI docs | Type definitions

client.createChatCompletion(params: ChatCompletionParams): Promise<{
  /** The completion message. */
  message: ChatResponseMessage;
  /** The raw response from the API. */
  response: ChatCompletionResponse;
}>

Create Embedding

See: OpenAI docs | Type definitions

client.createEmbedding(params: EmbeddingParams): Promise<{
  /** The embedding for the input string. */
  embedding: number[];
  /** The raw response from the API. */
  response: EmbeddingResponse;
}>

Create Edit

See: OpenAI docs | Type definitions

client.createEdit(params: EditParams): Promise<{
  /** The edited input string. */
  completion: string;
  /** The raw response from the API. */
  response: EditResponse;
}>