Creating Chat App with AI Model

OpenAI Model

I. Creating a chat application using the OpenAI API with ES6

Step 1 : Set Up Your Project

  1. Create a Project Directory: Open your terminal and run the following commands to create a new directory and navigate into it:

// bash
mkdir openai-chat-app
cd openai-chat-app
  1. Initialize the Project: Initialize a new Node.js project by running: This will create a package.json file to manage your project dependencies.

// Bash
npm init -y
  1. Enable ES6 Modules: Open the package.json file and add the following line to enable ES6 module syntax:

// package.json
{
   "type": "module"
}

Step 2: Install Dependencies

  1. Install OpenAI SDK: Use npm to install the OpenAI SDK:

  1. Create and export an API key to Environment Variables: Create a .env file in your project root and add your OpenAI API key:

  • pre-setting of OPENAI-API_KEY to system variable (You have to open OpenAI API account beforehand to generate your_api_key) : https://platform.openai.com/api-keys

  • Close and reopen your command prompt or PowerShell window for the changes to take effect.

To ensure that the environment variable has been set correctly, you can execute the following command in your terminal.

Save the File `.env`

Step 3: Create the Chat Application

  1. Create Main JavaScript File: Create a file named index.mjs using VS code in your project directory: open VS code and create a file "index.mjs"

  1. Set Up Code in index.mjs: Open index.mjs and add the following code:

Step 4: Run Your Application

  1. Run Your Chat Application: In your terminal, execute the following command to start the application:

Result

  1. Interact with the Chatbot: You can now type messages into the terminal, and the AI will respond based on your inputs. Type exit to close the application.

Explanation of Key Components

  • Environment Configuration: The dotenv package loads environment variables from a .env file, keeping sensitive information like API keys secure.

  • OpenAI SDK: The openai package is used to interact with OpenAI's API, specifically for generating chat completions.

  • Readline Interface: The readline module allows for easy interaction via the command line, enabling users to input text and receive responses dynamically.

  • Check Node.js Version: Ensure you are using a compatible version of Node.js that supports ES6 modules and dotenv.

Challenge for API Security

Task: Loading check for API Key &

  • Modify the version 1 code by adding a function to read OPENAI_API_KEY from .env or throw an error: Change AI model gpt-3.5-turbo to gpt-4o-mini

  • Handling Rate Limits in Code: To manage rate limits effectively within your application, consider implementing a retry mechanism with exponential backoff:

II. Creating a chat application using the Claude Anthropic API with ES6

Go through steps described in I. Creating a chat application using the OpenAI API with ES6. As in the previous step , create an account Claude AI to generate and get the ANTHROPIC_API_KEY .

https://www.anthropic.com/claude

Step 3: Create the Chat Application: create a file named index.mjs using VS code in your project directory: open VS code and create a file "index.mjs"

Result

Last updated