npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fern-api/openai

v0.0.32

Published

![openai image](./static/hero.png)

Downloads

1,487

Readme

openai image

OpenAI Node Library

npm shield

The OpenAI Node.js library provides access to the OpenAI API from JavaScript/TypeScript.

Documentation

API reference documentation is available here.

Installation

npm install @fern-api/openai
yarn add @fern-api/openai

Authentication

The OpenAI API uses API keys for authentication. Visit your API Keys page to retrieve the API key you'll use in your requests.

Remember that your API key is a secret! Do not share it with others or expose it in any client-side code (browsers, apps). Production requests must be routed through your own backend server where your API key can be securely loaded from an environment variable or key management service.

Usage

Try it out

import { OpenAIClient } from '@fern-api/openai';

const client = new OpenAIClient({
  token: 'YOUR_API_KEY',
});

const response = await client.edit.create({
  model: 'text-davinci-edit-001',
  input: 'What day of the wek is it?',
  instruction: 'Fix the spelling mistakes',
});

console.log(response.choices[0].text); // "What day of the week is it?"

Specifying organization

If you belong to multiple organizations, you can specify an organization when constructing the client. Usage from these API requests will count against the specified organization's subscription quota.

const client = new OpenAIClient({
  token: 'YOUR_API_KEY',
  organization: 'org-dXiZKrWii8lND9WMvqr0UoKy',
});

Handling errors

When the API returns a non-success status code (4xx or 5xx response), a subclass of OpenAIError will be thrown:

try {
  await client.edit.create({
    model: 'text-davinci-edit-001',
    input: 'What day of the wek is it?',
    instruction: 'Fix the spelling mistakes',
  });
} catch (error) {
  if (err instanceof OpenAIError) {
    console.log(err.statusCode);
    console.log(err.body);
  }
}

Error codes are as followed:

| Status Code | Cause | Solution | | --------------------------------------------------------------------------------- | -------------------------------------------- | -------- | | 401 - Invalid Authentication | Invalid Authentication | Ensure the correct API key and requesting organization are being used. | | 401 - Incorrect API key provided | The requesting API key is not correct. | Ensure the API key used is correct, clear your browser cache, or generate a new one. | | 401 - You must be a member of an organization to use the API | Your account is not part of an organization. | Contact us to get added to a new organization or ask your organization manager to invite you to an organization. | | 429 - Rate limit reached for requests | You are sending requests too quickly. | Pace your requests. Read the Rate limit guide. | | 429 - You exceeded your current quota, please check your plan and billing details | You have hit your maximum monthly spend (hard limit) which you can view in the account billing section. | Apply for a quota increase. | | 429 - The engine is currently overloaded, please try again later | Our servers are experiencing high traffic. | Please retry your requests after a brief wait. | | 500 - The server had an error while processing your request | Issue on our servers. | Retry your request after a brief wait and contact us if the issue persists. Check the status page. |

File upload

The SDK supports uploading files using the built-in File and ReadStream classes.

import * as fs from "fs";
import { OpenAIClient } from '@fern-api/openai';

const { Configuration, OpenAIApi } = require("openai");

const client = new OpenAIClient({
  token: 'YOUR_API_KEY',
  organization: 'org-dXiZKrWii8lND9WMvqr0UoKy',
});

const response = await client.file.upload(
  fs.createReadStream("mydata.jsonl"),
  { purpose: "fine-tune" }
);

Streaming

The SDK supports streaming responses from certain endpoints:

To take advantage of this feature, pass stream: true with your request and a callback to handle the events.

await client.chat.createCompletion({
  model: "gpt-3.5-turbo",
  messages: [{ role: "user", content: "Who won the world series in 2015?" }],
  stream: true, // <---
}, (data) => {
  console.log("Received a new data chunk", data);
});

You can specify additional callbacks for handling errors and completion:

await client.chat.createCompletion({
  model: "gpt-3.5-turbo",
  messages: [{ role: "user", content: "Who won the world series in 2015?" }],
  stream: true,
}, (data) => {
  console.log("Received a new data chunk", data);
}, {
  onError: (error) => {
    console.log("Received error", error);
  },
  onFinish: () => {
    console.log("Finished!");
  },
});

Beta status

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning the package version to a specific version in your package.json file. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!