Skip to main content
Home

latest

A tiny (1.18kB), tree-shakeable OpenAI client. Optionally supports response streaming in all JavaScript runtimes.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
82%
Published
5 months ago (0.0.8)
class Client

Examples

Example 1

let client = new OpenAI.Client({
  apikey: '...',
});

// NOTE: All methods can throw!
let reply = await client.completion({
  model: '...',
  prompt: '...',
});

console.assert(reply.object === 'text_completion');

// NOTE: All methods can throw!
let stream = await client.completion({
  model: '...',
  prompt: '...',
  stream: true, // << required!
});

for await (let event of stream) {
  console.assert(event.object === 'text_completion')
}

Constructors

new
Client(options: AuthOptions)

Methods

Dispatch a Chat Completions request.

When input includes stream: true, then a ReadableStream is returned for message streaming. Otherwise the full Chat.Output JSON object is returned.

Important: Will throw the Response on 4xx or 5xx status!

Dispatch a Completions (legacy) request.

When input includes stream: true, then a ReadableStream is returned for message streaming. Otherwise the full Completion.Output JSON object is returned.

Important: Will throw the Response on 4xx or 5xx status!

The fetch implementation.

NOTE: You may override method with a custom definition.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@agent/openai

Import symbol

import { Client } from "@agent/openai";
or

Import directly with a jsr specifier

import { Client } from "jsr:@agent/openai";

Add Package

pnpm i jsr:@agent/openai
or (using pnpm 10.8 or older)
pnpm dlx jsr add @agent/openai

Import symbol

import { Client } from "@agent/openai";

Add Package

yarn add jsr:@agent/openai
or (using Yarn 4.8 or older)
yarn dlx jsr add @agent/openai

Import symbol

import { Client } from "@agent/openai";

Add Package

vlt install jsr:@agent/openai

Import symbol

import { Client } from "@agent/openai";

Add Package

npx jsr add @agent/openai

Import symbol

import { Client } from "@agent/openai";

Add Package

bunx jsr add @agent/openai

Import symbol

import { Client } from "@agent/openai";