OpenAI Node.js Library instance initialization and encapsulation for the Fastify framework.
Install the package:
# npm
npm i fastify-openai
# yarn
yarn add fastify-openai
# pnpm
pnpm add fastify-openai
# bun
bun add fastify-openai
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.
// ESM
import fastifyOpenAI from "fastify-openai";
// CJS
const fastifyOpenAI = require("fastify-openai");
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 });
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;
}
}
-
apiKey
[ required ]: Your account's secret key wich is available in your OpenAI Dashboard -
name
[ optional ]: Through this optionfastify-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 tohttps://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.
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;
}
}
}
See the Node OpenAI API docs.
This project is kindly sponsored by @timmywheels.
Licensed under MIT