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

fastify-openai

v0.1.3

Published

OpenAI Fastify plugin

Downloads

413

Readme

fastify-openai

NPM version GitHub CI Coverage Status js-standard-style

OpenAI Node.js Library instance initialization and encapsulation for the Fastify framework.

Install

Install the package:

# npm
npm i fastify-openai

# yarn
yarn add fastify-openai

# pnpm
pnpm add fastify-openai

# bun
bun add fastify-openai

Usage

The package needs to be added to your project with register and you must at least configure your account's secret key wich is available in your OpenAI Dashboard then call the OpenAI API and you are done.

Importing the package

// ESM
import fastifyOpenAI from "fastify-openai";

// CJS
const fastifyOpenAI = require("fastify-openai");

JavaScript + CJS

const fastify = require("fastify")({ logger: true });

fastify.register(require("fastify-openai"), {
  apiKey: "sk-TacO...",
});

fastify.post("/chat", async (request, reply) => {
  // create a chat completion using the OpenAI API
  const chat = await fastify.openai.chat.completions.create({
    messages: [{ role: "user", content: "Hello, Fastify!" }],
    model: "gpt-4o-mini",
  });

  return chat;
});

fastify.listen({ port: 3000 });

TypeScript + ESM

import Fastify from "fastify";
import fastifyOpenAI from "fastify-openai";

const fastify = Fastify({ logger: true });

fastify.register(fastifyOpenAI, {
  apiKey: "sk-TacO...",
});

fastify.post("/chat", async (request, reply) => {
  // create a chat completion using the OpenAI API
  const chat = await fastify.openai.chat.completions.create({
    messages: [{ role: "user", content: "Hello, Fastify!" }],
    model: "gpt-4o-mini",
  });
  return chat;
});

fastify.listen({ port: 3000 });

// typescript declaration merging
declare module "fastify" {
  interface FastifyInstance {
    openai: FastifyOpenAI;
  }
}

Options

  • apiKey [ required ]: Your account's secret key wich is available in your OpenAI Dashboard

  • name [ optional ]: Through this option fastify-openai lets you define multiple OpenAI instances, with different configurations.

  • organization [ optional ]: The organization ID to use for this request. If not provided, the request will be made with the default organization.

  • project [ optional ]: The project ID to use for this request. If not provided, the request will be made with the default project.

  • baseURL [ optional ]: The base URL for the API. Defaults to https://api.openai.com/v1.

  • timeout [ optional ]: The request timeout in milliseconds.

  • httpAgent [ optional ]: An HTTP agent used to manage HTTP(S) connections.

  • maxRetries [ optional ]: The maximum number of retries for a request.

Multiple plugin instances (TypeScript + ESM)

When using multiple plugin instances, the name property is required for each instance.

import Fastify from "fastify";
import fastifyOpenAI from "fastify-openai";

const fastify = Fastify({ logger: true });

fastify
  .register(require("fastify-openai"), {
    apiKey: "sk-Te5t...",
    name: "test",
    timeout: 28000, // in ms (this is 28 seconds)
  })
  .register(require("fastify-openai"), {
    apiKey: "sk-Pr0d...",
    name: "prod",
  });

fastify.post("/test/chat", function (request, reply) {
  // create a chat completion using the OpenAI API 'test' instance
  const testChat = await fastify.openai.test.chat.completions.create({
    messages: [{ role: "user", content: "Hello, Test!" }],
    model: "gpt-4o-mini",
  });
  return testChat;
});

fastify.post("/prod/chat", function (request, reply) {
  // create a chat completion using the OpenAI API 'prod' instance
  const prodChat = await fastify.openai.prod.chat.completions.create({
      messages: [{ role: "user", content: "Hello, Production!" }],
      model: "gpt-4o-mini",
    });
  return prodChat;
});

fastify.listen({ port: 3000 });

// typescript declaration merging
declare module "fastify" {
  interface FastifyInstance {
    openai: {
      test: FastifyOpenAI;
      prod: FastifyOpenAI;
    }
  }
}

Documentation

See the Node OpenAI API docs.

Acknowledgements

This project is kindly sponsored by @timmywheels.

License

Licensed under MIT