Building a ChatGPT inspired app

Learn how to build a ChatGPT-inspired application with Aurelia 2, Node.js, and the OpenAI GPT-4o API.

This tutorial will guide you through creating a ChatGPT-like application using Aurelia 2 for the frontend, Node.js + Express for the backend, and OpenAI's GPT-4o API to generate responses. We'll demonstrate modern Aurelia 2 patterns, proper dependency injection, TypeScript types, error handling, and responsive design.


Prerequisites

Before you begin, ensure you have the following:

  • Node.js (latest LTS version recommended)

  • OpenAI API Key - You'll need to add credits to your account

  • Basic familiarity with TypeScript and Aurelia 2 concepts


1. Setting Up the Aurelia 2 Frontend

Create a New Aurelia Project

Run the following command to scaffold a new Aurelia 2 project with Vite:

npx makes aurelia

Select "Default TypeScript App" and "Vite" when prompted. Name your project chatgpt-clone.

Install Dependencies

Navigate to your project directory and install dependencies:


2. Setting Up the Node.js Backend

Initialize the Backend Project

Create a new directory for the backend and initialize a Node.js project:

Install Required Packages

Install Express and the official OpenAI Node.js client:

Create the Express Server

Create an server.js file inside the server directory:

Configure Environment Variables

Create a .env file in the server directory:

Replace your_openai_api_key_here with your actual OpenAI API key. Important: Keep this file secure and never commit it to version control.

Add Package.json Scripts

Update your server/package.json to include development scripts:


3. Connecting Frontend and Backend

Create Type Definitions

First, create type definitions for better TypeScript support. Create src/types/chat.ts:

Create Chat Service

Create a dedicated service for API communication. Create src/services/chat-service.ts:

Create the Chat Component

Create src/components/chat.ts:

Create the Chat Component View

Create src/components/chat.html:

Apply Modern CSS Styling

Replace the content of src/my-app.css with modern, responsive styles:

Update the Main App Component

Update src/my-app.ts to include the chat component:

Update src/my-app.html:


4. Running the Application

Start the Backend Server

Navigate to the server directory and run:

You should see:

Start the Aurelia Frontend

In a new terminal, navigate to your Aurelia project directory and run:

Your browser should automatically open to http://localhost:8080 with the chat application.


5. Features Implemented

Our ChatGPT-inspired app includes:

Core Features

  • Modern Aurelia 2 Architecture: Proper dependency injection, services, and TypeScript types

  • Real-time Chat Interface: Smooth messaging experience with loading states

  • Error Handling: Comprehensive error handling for API failures, rate limits, and network issues

  • Responsive Design: Mobile-friendly layout that works on all screen sizes

  • Connection Status: Visual indicator showing server connectivity

  • Message Persistence: Chat history maintained during the session

  • Typing Indicators: Visual feedback when the AI is generating a response

  • Accessibility: Keyboard navigation and screen reader support

UI/UX Enhancements

  • Modern Design: Clean, modern interface inspired by popular chat applications

  • Smooth Animations: Message slide-ins and loading animations

  • Message Timestamps: Each message includes a timestamp

  • Auto-scroll: Messages automatically scroll to the bottom

  • Clear Chat: Option to clear conversation history

6. Advanced Enhancements (Optional)

To further enhance your application, consider adding:

Streaming Responses

Implement OpenAI's streaming API for real-time text generation:

Markdown Support

Add markdown rendering for formatted responses:

Conversation History

Persist conversations in localStorage or a database:

Authentication

Add user authentication and personalized conversations:

Dark Mode

Add theme switching capability:


7. Testing Your Application

Backend Testing

Test your server endpoints:

Frontend Testing

  1. Open your browser to http://localhost:8080

  2. Type a message and press Enter

  3. Verify the AI responds appropriately

  4. Test error handling by stopping the backend server

  5. Test responsive design by resizing your browser window

8. Troubleshooting

Common Issues

"Invalid API key" error:

  • Verify your OpenAI API key is correct in the .env file

  • Ensure you have credits in your OpenAI account

"Rate limit exceeded" error:

  • You're sending too many requests. Wait a moment and try again

  • Consider implementing request throttling

CORS errors:

  • Ensure the server's CORS configuration allows your frontend domain

  • Check that both server and client are running on expected ports

Connection issues:

  • Verify both backend (port 3001) and frontend (port 8080) are running

  • Check browser console for network errors

  • Ensure firewall isn't blocking the ports

Conclusion

You've built a modern, feature-rich ChatGPT-inspired application using Aurelia 2. This project demonstrates:

Modern Architecture

  • Aurelia 2: Latest framework features with proper dependency injection using resolve()

  • TypeScript: Strong typing throughout the application

  • Service Pattern: Separation of concerns with dedicated services

  • Component-Based: Reusable and maintainable component structure

Best Practices

  • Error Handling: Comprehensive error management and user feedback

  • Responsive Design: Mobile-first approach with modern CSS

  • Performance: Optimized rendering and smooth animations

  • Accessibility: Keyboard navigation and semantic HTML

Production-Ready Features

  • Environment Configuration: Secure API key management

  • Health Checks: Server monitoring endpoints

  • Loading States: Clear user feedback during operations

  • Connection Status: Real-time connectivity monitoring

This application serves as an excellent foundation for building more complex AI-powered chat interfaces. You can extend it with additional features like conversation persistence, user authentication, file uploads, or integration with other AI services.

Next Steps:

  • Deploy your application to production using services like Vercel, Netlify, or AWS

  • Add user authentication and conversation history

  • Implement streaming responses for real-time text generation

  • Add support for image uploads and multimodal conversations

  • Create custom AI personalities or specialized assistants

Last updated

Was this helpful?