avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

JavaScript OpenAI Client Notes

Sat Oct 28 2023

OpenAI offers a variety of APIs for natural language processing. Here are some notes on the JavaScript client for my own quick reference.

Installation

npm install openai

Usage

import OpenAI from "openai";

// Create client
const openai = new OpenAI({
  // You API key to access OpenAI API
  apiKey: "YOUR_API_KEY_HERE",
  // Optional, use a different base URL, normally it ends with /v1
  baseURL: "https://api.openai.com/v1"
});

// Write your prompt here
const prompt = `I need you to write a function that takes a list of numbers and returns the sum of those numbers.`;

const messages = [
  { "role": "system", "content": "You are a helpful assistant." },
  { "role": "user", "content": prompt }
]

// Basic completion, making a single request to the API and waiting for the complete response before proceeding
openai.chat.completions.create({
  model: "gpt-4",
  messages,
  stop: ["done", "end", "stop"]
}).then((stream) => {
  const respone = JSON.parse(res);
  const message = respone?.choices?.[0].message.content;
  console.log(message);
});

// Stream completion, initiating a request and then receiving the response in chunks as they become available, without having to wait for the entire response to be completed.
openai.chat.completions.create({
  model: "gpt-4",
  messages,
  stream: true,
  stop: ["done", "end", "stop"]
}).then(async (stream) => {
  // Handle streaming response
  for await (const chunk of stream) {
    console.log(chunk?.choices?.[0]?.delta);
  }
});