Skip to content

Latest commit

 

History

History
233 lines (170 loc) · 7.95 KB

Build-a-Nodejs-Chatbot-with-OpenAI.md

File metadata and controls

233 lines (170 loc) · 7.95 KB

Building a Chatbot with Node.js and OpenAI: A Step-by-Step Guide

By Lenora Porter • April 3, 2024

Introduction:

In this tutorial, we'll walk through the process of creating a chatbot using Node.js and the OpenAI API. Chatbots are becoming increasingly popular for automating customer support, providing assistance, or simply engaging users in conversation. By the end of this tutorial, you'll have a basic chatbot that can hold conversations with users.

Prerequisites:

  • Basic knowledge of JavaScript and Node.js
  • An OpenAI API key (sign up at https://openai.com/ to get one)

Step 1: Set Up Your Environment

  • Install Node.js if you haven't already (https://nodejs.org/)
  • Create a new directory for your project and navigate into it using your terminal or command prompt.
  • Initialize a new Node.js project by running npm init -y.

Step 2: Install Dependencies

Install the required dependencies:

npm install openai readline-sync dotenv colors

Step 3: Initialize OpenAI

Initialize OpenAI by providing your API key:

Create a file named .env, generate an API key from OpenAI and add the following:

OPENAI_API_KEY=YOURKEY

Step 4: Update package.json

Open your package.json file and add the following lines:

"type": "module",
"scripts": {
  "start": "node chatbot.js"
}

Step 5: Create the OpenAI Module

Create a new JavaScript file (e.g., openai.js) in your project directory.

Import the necessary dependencies (OpenAI and dotenv), loads environment variables from a .env file using dotenv, initializes the OpenAI instance with the API key stored in the environment variables, and exports the initialized OpenAI instance for use in other parts of the project.

import OpenAI from "openai";
import dotenv from "dotenv";
dotenv.config();

const openai = new OpenAI({
	apiKey: process.env.OPENAI_API_KEY,
});

export default openai;
  • openai: A JavaScript tool that connects our chatbot to OpenAI's language models for understanding user input and generating responses.
  • dotenv: A Node.js module that loads environment variables from a .env file into process.env, simplifying the management of configuration settings such as API keys or database credentials. It allows you to keep sensitive information separate from your codebase and easily switch between different environments by modifying the .env file.

Step 6: Write the Chatbot Logic

Create a new JavaScript file (e.g., chatbot.js) in your project directory. Write the main logic for your chatbot. This includes prompting the user for input, sending the input to the OpenAI API, and displaying the response.

  1. Initialize OpenAI API and Required Libraries:

    Import the necessary modules (openai, readline-sync, colors) at the beginning of the script.

    import openai from "./openai.js";
    import readlineSync from "readline-sync";
    import colors from "colors";
    • openai: A JavaScript tool that connects our chatbot to OpenAI's language models for understanding user input and generating responses.
    • readline-sync: A Node.js module that enables real-time interaction with users by handling input/output operations directly in the terminal.
    • colors: A module that adds color and style to our chatbot's messages in the terminal, enhancing readability and user experience.
  2. Create a Main Function for the Chatbot:

    Define an asynchronous function named main() to contain the main logic of the chatbot.

    async function main() {
        // Chatbot logic will go here
    }
  3. Start the Chatbot Logic with a Welcome Message:

    Display a welcome message to the user when they start interacting with the chatbot.

    console.log(colors.bold.green('Welcome to the Chatbot Program!'));
    console.log(colors.bold.green('You can start chatting with the bot.'));
  4. Chat History Management:

    Create a chatHistory array to store the interaction history:

    const chatHistory = [];
  5. User Interaction Loop:

    The chatbot operates within an indefinite loop, facilitating ongoing interaction with the user:

    while (true) {
    		// Prompt the user for an input using the readline-sync package
    		const userInput = readlineSync.question(colors.yellow("You: "));
    
    		try {
    			// Construct messages by mapping over the chat history
    			const messages = chatHistory.map(([role, content]) => ({
    				role,
    				content,
    			}));
    
    			// Add latest user input
    			messages.push({ role: "user", content: userInput });
    
    			// Use the **openai.completions.create** method to send the user input to the OpenAI API and retrieve a response.
    			const completion = await openai.chat.completions.create({
    				model: "gpt-3.5-turbo",
    				messages: messages,
    			});
    
    			// Check if completion and choices exist
    			if (completion && completion.choices && completion.choices.length > 0) {
    				// Get completion text/content
    				const completionText = completion.choices[0].message.content;
    
    				if (userInput.toLowerCase() === "exit") {
    					console.log(colors.green("Bot: ") + completionText);
    					return;
    				}
    
    				console.log(colors.green("Bot: ") + completionText);
    
    				// Update history with user input and assistant response
    				chatHistory.push(["user", userInput]);
    				chatHistory.push(["assistant", completionText]);
    			} else {
    				// Implement error handling to catch and log any errors that occur during the execution of the chatbot logic.
    				console.error(colors.red("Error: Empty response from the API"));
    			}
    		} catch (error) {
    			console.error(colors.red(error));
    		}
    	}
    }

Putting it all together, here's how the complete chatbot logic might look:

import openai from "./openai.js";
import readlineSync from "readline-sync";
import colors from "colors";

async function main() {
	console.log(colors.bold.green("Welcome to the Chatbot Program!"));
	console.log(colors.bold.green("You can start chatting with the bot."));

	const chatHistory = []; // Store conversation history

	while (true) {
		const userInput = readlineSync.question(colors.yellow("You: "));

		try {
			// Construct messages by iterating over the history
			const messages = chatHistory.map(([role, content]) => ({
				role,
				content,
			}));

			// Add latest user input
			messages.push({ role: "user", content: userInput });

			// Call the API with user input & history
			const completion = await openai.chat.completions.create({
				model: "gpt-3.5-turbo",
				messages: messages,
			});

			// Check if completion and choices exist
			if (completion && completion.choices && completion.choices.length > 0) {
				// Get completion text/content
				const completionText = completion.choices[0].message.content;

				if (userInput.toLowerCase() === "exit") {
					console.log(colors.green("Bot: ") + completionText);
					return;
				}

				console.log(colors.green("Bot: ") + completionText);

				// Update history with user input and assistant response
				chatHistory.push(["user", userInput]);
				chatHistory.push(["assistant", completionText]);
			} else {
				console.error(colors.red("Error: Empty response from the API"));
			}
		} catch (error) {
			console.error(colors.red(error));
		}
	}
}

main();

Step 7: Run Your Chatbot

Save your changes and run your chatbot script:

node chatbot.js

Your chatbot should start running, allowing you to interact with it in the terminal.

Conclusion:

Congratulations! You've successfully created a basic chatbot using Node.js and the OpenAI API. From here, you can further customize and expand your chatbot's functionality by experimenting with different models, adding more complex logic, or integrating with other APIs and services. Happy coding!