Package Exports
- openai-fetch
- openai-fetch/dist/index.cjs.js
- openai-fetch/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (openai-fetch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
OpenAI Fetch Client
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 andopenai-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