Giter Site home page Giter Site logo

openai-fetch's Introduction

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;
}>

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;
}>

openai-fetch's People

Contributors

rileytomasek avatar bytemain avatar firede avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.