Skip to content

Latest commit

 

History

History
175 lines (125 loc) · 4.98 KB

README.md

File metadata and controls

175 lines (125 loc) · 4.98 KB

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