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
6 months ago (0.0.8)
function request
request<
T,
I extends Input = Input,
>
(
credentials: Credentials,
options: Options<I>,
): Promise<T>

Send a HTTP request and, if 2xx, parse the JSON response as T object type.

The request body (options.body) is required if I is defined, otherwise I is inferred.

NOTE: The Response is thrown if not 2xx status!

Examples

Usage

import * as OpenAI from '@agent/openai';

let ctx = OpenAI.credentials({
  apikey: '...',
});

try {
  let reply = await OpenAI.request<OpenAI.Completion.Output>(ctx, {
    method: 'POST',
    path: '/completions',
    body: {
      model: '...',
      prompt: '...',
    } satisfies OpenAI.Completion.Request,
  });

  console.assert(reply.object === 'text_completion');
} catch (err) {
  if (err instanceof Response) {
    console.log('OpenAI Error', err.status, await err.json());
  } else {
    // ...
  }
}

Type Parameters

I extends Input = Input

Parameters

credentials: Credentials
options: Options<I>

Return Type

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 { request } from "@agent/openai";
or

Import directly with a jsr specifier

import { request } 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 { request } 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 { request } from "@agent/openai";

Add Package

vlt install jsr:@agent/openai

Import symbol

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

Add Package

npx jsr add @agent/openai

Import symbol

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

Add Package

bunx jsr add @agent/openai

Import symbol

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