1.0.4 • Published 1 year ago

@bytemain/openai-fetch v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

Create Completion

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

See: OpenAI docs | source code

Create Embedding

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

See: OpenAI docs | source code

Create Edit

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

See: OpenAI docs | source code