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
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-appInitialize the Project: Initialize a new Node.js project by running: This will create a
package.jsonfile to manage your project dependencies.
// Bash
npm init -yEnable ES6 Modules: Open the
package.jsonfile and add the following line to enable ES6 module syntax:
// package.json
{
"type": "module"
}Step 2: Install Dependencies
Install OpenAI SDK: Use npm to install the OpenAI SDK:
Create and export an API key to Environment Variables: Create a
.envfile 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
Create Main JavaScript File: Create a file named
index.mjs using VS codein your project directory: open VS code and create a file "index.mjs"
Set Up Code in
index.mjs: Openindex.mjsand add the following code:
Step 4: Run Your Application
Run Your Chat Application: In your terminal, execute the following command to start the application:
Result

Interact with the Chatbot: You can now type messages into the terminal, and the AI will respond based on your inputs. Type
exitto close the application.
Explanation of Key Components
Environment Configuration: The
dotenvpackage loads environment variables from a.envfile, keeping sensitive information like API keys secure.OpenAI SDK: The
openaipackage is used to interact with OpenAI's API, specifically for generating chat completions.Readline Interface: The
readlinemodule 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