A modern web application that provides a chat interface powered by OpenAI's GPT-4. Built with React, TypeScript, and Express.
- Technology Stack: React, TypeScript, Vite
- Key Components:
App.tsx: Main chat interface with message history and input formapi.ts: API client for backend communication
- Features:
- Real-time message display
- Token usage tracking
- Responsive design with inline styles
- Technology Stack: Express, TypeScript, OpenAI API
- Key Components:
config.ts: Centralized configuration managementchat.ts: Chat logic and context managementindex.ts: Express server and API routes
- Features:
- Rate limiting
- CORS protection
- Token-based context management
- Error handling
- Uses a sliding window approach for conversation context
- Maintains token count below configurable limit (700 tokens)
- Preserves system message while trimming older exchanges
- API key stored securely in environment variables
- Rate limiting: 100 requests per 15 minutes per IP
- Request size limits: 10kb per request
- CORS protection for frontend-backend communication
- Efficient token counting using tiktoken
- Minimal dependencies
- No database required (in-memory context)
- Node.js (v16+)
- npm
- OpenAI API key
- Clone the repository
- Install dependencies:
npm install
- Create
.envfile:OPENAI_API_KEY=your_api_key_here VITE_API_URL=http://localhost:3000
- Start the backend server:
npm run server
- Start the frontend development server:
npm run dev
- Frontend: Deploy to Netlify
- Backend: Deploy to Render.com
- Update environment variables in deployment platforms
- Request Body:
{ message: string } - Response:
{ message: { role: 'assistant', content: string }, tokenCount: number }
chat-app/
├── src/ # Frontend code
│ ├── App.tsx # Main React component
│ ├── api.ts # API client
│ └── main.tsx # Entry point
├── server/ # Backend code
│ ├── config.ts # Configuration
│ ├── chat.ts # Chat logic
│ └── index.ts # Express server
├── package.json # Dependencies
└── .env # Environment variables
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
ISC