How to Format Inputs and call ChatGPT Models using TypeScript Node.js

ChatGPT is a powerful language model developed by OpenAI. It is powered by gpt-3.5-turbo and gpt-4, which are some of OpenAI’s most advanced models. With ChatGPT, you can build a wide range of applications, including chatbots, virtual assistants, content generators, and more.

In this guide, we will walk you through the process of formatting inputs to ChatGPT models using TypeScript/JavaScript. We will also explain how to use the OpenAI API to interact with ChatGPT models and generate AI-written responses.

ChatGPT is an AI language model that can understand and generate human-like text. It is designed to engage in conversations and can be used to build applications that require natural language understanding and generation.

Building Applications with ChatGPT

Section titled Building Applications with ChatGPT

To build applications with ChatGPT, you need to use the OpenAI API. The API allows you to send a series of messages as input to the model and receive an AI-generated message as output. You can use this capability to create chatbots, virtual assistants, and other applications that require natural language interactions.

Chat models take a series of messages as input. Each message in the series has two required fields: role and content. The role can be one of three values: “system,” “user,” or “assistant.” The content field contains the actual text of the message.

A typical conversation starts with a system message that sets the behavior of the assistant, followed by alternating user and assistant messages. However, you are not required to follow this format strictly.

Let’s dive into the details and learn how to format inputs to ChatGPT models using TypeScript/JavaScript.

Step 1: Install the OpenAI Node.js Library

Section titled Step 1: Install the OpenAI Node.js Library

To interact with the OpenAI API, you need to install the OpenAI Node.js library. You can do this using npm or yarn:

Terminal window
npm install openai
# or
yarn add openai

Step 2: Import the OpenAI Library and Set Up the API Key

Section titled Step 2: Import the OpenAI Library and Set Up the API Key

After installing the library, you need to import it into your TypeScript/JavaScript file. You also need to set up your OpenAI API key, which you can obtain from the OpenAI platform.

import { OpenAI } from 'openai';

// Set up your OpenAI API key
const openai = new OpenAI('YOUR_API_KEY');

Step 3: Create a Chat API Call

Section titled Step 3: Create a Chat API Call

To interact with ChatGPT, you need to create a chat API call. This call requires two inputs: the model name and a list of messages. Each message object must have the role and content fields.

async function chatWithGPT() {
  // Define the model name (e.g., gpt-3.5-turbo)
  const modelName = 'gpt-3.5-turbo';

  // Define the list of messages
  const messages = [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Tell me a joke.' }
  ];

  // Make the chat API call
  const response = await openai.createChatCompletion({
    model: modelName,
    messages: messages
  });

  // Extract the assistant's reply
  const assistantReply = response.data.choices[0].message.content;

  // Display the assistant's reply
  console.log(assistantReply);
}

// Call the function to chat with GPT


chatWithGPT();

Step 4: Understanding the Response

Section titled Step 4: Understanding the Response

The response from the chat API call contains several fields, including the choices field, which contains the AI-generated message. The choices field is an array, and each element represents a different completion. By default, there is only one completion, so we can extract the assistant’s reply using response.data.choices[0].message.content.

Step 5: Formatting Conversations

Section titled Step 5: Formatting Conversations

You can format conversations by adding more messages to the messages array. Each message object should have the role and content fields. The role can be “system,” “user,” or “assistant,” and the content field contains the text of the message.

async function chatWithGPT() {
  // Define the model name (e.g., gpt-3.5-turbo)
  const modelName = 'gpt-3.5-turbo';

  // Define the list of messages
  const messages = [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Tell me a joke.' },
    { role: 'assistant', content: 'Why did the scarecrow win an award? Because he was outstanding in his field!' },
    { role: 'user', content: 'That was funny! Tell me another one.' }
  ];

  // Make the chat API call
  const response = await openai.createChatCompletion({
    model: modelName,
    messages: messages
  });

  // Extract the assistant's reply
  const assistantReply = response.data.choices[0].message.content;

  // Display the assistant's reply
  console.log(assistantReply);
}

// Call the function to chat with GPT
chatWithGPT();

In this example, we added more messages to the conversation, including an assistant message and another user message. The assistant’s reply is generated based on the entire conversation history.

Step 6: Experimenting with Different Instructions

Section titled Step 6: Experimenting with Different Instructions

You can experiment with different instructions and conversation formats to achieve the desired behavior from the model. For example, you can use a system message to set the behavior of the assistant or provide specific instructions in the user message.

async function chatWithGPT() {
  // Define the model name (e.g., gpt-3.5-turbo)
  const modelName = 'gpt-3.5-turbo';

  // Define the list of messages
  const messages = [
    { role: 'system', content: 'You are a pirate assistant who speaks like a pirate.' },
    { role: 'user', content: 'Tell me a joke, matey!' }
  ];

  // Make the chat API call
  const response = await openai.createChatCompletion({
    model: modelName,
    messages: messages
  });

  // Extract the assistant's reply
  const assistantReply = response.data.choices[0].message.content;

  // Display the assistant's reply
  console.log(assistantReply);
}

// Call the function to chat with GPT
chatWithGPT();

In this example, we used a system message to instruct the assistant to speak like a pirate. The assistant’s reply is generated based on this instruction.

In this guide, we introduced ChatGPT and explained how to format inputs to ChatGPT models using TypeScript/JavaScript. We also demonstrated how to use the OpenAI API to interact with ChatGPT models and generate AI-written responses. By following these steps, you can build your own applications with ChatGPT and create engaging and interactive experiences for your users.