A powerful AI-driven backend API for language learning applications. Generate educational content, vocabulary definitions, audio, and images using OpenAI's advanced language models.
- π€ AI-Powered Content Generation: Create educational texts, word definitions, and examples using GPT-4
- π¨ Image Generation: Generate contextual images for words and lessons using DALL-E 3
- π Text-to-Speech: Convert educational content to audio using OpenAI TTS
- π Multi-language Support: English, Spanish, and Portuguese
- π CEFR Level Adaptation: Content tailored for A1-C2 proficiency levels
- π Code-switching Examples: Natural language mixing for bilingual learners
- π Progress Tracking: Word difficulty levels and usage statistics
- π JWT Authentication: Secure user management system
- π Swagger Documentation: Complete API documentation
- π³ Docker Ready: Containerized deployment
src/
βββ app/
β βββ controllers/ # Request handlers
β βββ db/
β β βββ models/ # MongoDB schemas
β βββ middlewares/ # Authentication & validation
β βββ routes/ # API endpoints
β βββ services/ # Business logic
β β βββ ai/ # OpenAI integrations
β β βββ auth/ # Authentication services
β β βββ cloudinary/ # Image storage
β β βββ lectures/ # Educational content
β β βββ words/ # Vocabulary management
β βββ utils/ # Helper functions
βββ main.ts # Application entry point
βββ swagger/ # API documentation
- Node.js 18.x or higher
- MongoDB 6.x or higher
- OpenAI API key
- Cloudinary account (for image storage)
Create a .env
file in the root directory:
# Server Configuration
NODE_ENV=development
PORT=3000
VERSION=1.0.0
# Database
MONGO_URL=mongodb://localhost:27017/language_ai
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key_here
# Cloudinary Configuration
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
# Authentication
JWT_SECRET=your_jwt_secret_key
USER_NOVA=admin
PASSWORD_NOVA=adminpassword
-
Clone the repository
git clone <repository-url> cd language-ai-backend
-
Install dependencies
npm install # or pnpm install
-
Build the project
npm run build
-
Start the development server
npm run dev
-
Build and run with Docker Compose
docker-compose up --build
-
Access the application
- API: http://localhost:3000
- Swagger Docs: http://localhost:3000/api-docs
# Login
curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "adminpassword"}'
# Generate educational text
curl -X POST http://localhost:3000/api/ai/generate-text \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"prompt": "The importance of renewable energy",
"level": "B2",
"typeWrite": "Academic Article"
}'
# Generate word definition
curl -X POST http://localhost:3000/api/ai/generate-wordJson \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"prompt": "sustainability",
"language": "en"
}'
# Generate audio from text
curl -X POST http://localhost:3000/api/ai/generate-audio-from-text \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Learning a new language can be fun and rewarding.",
"voice": "nova"
}'
# Get words with pagination
curl -X GET "http://localhost:3000/api/words?page=1&limit=10" \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
# Create a new word
curl -X POST http://localhost:3000/api/words \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"word": "example",
"definition": "A representative form or pattern",
"language": "en",
"level": "medium"
}'
# Get lectures with pagination
curl -X GET "http://localhost:3000/api/lectures?page=1&limit=10" \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
# Create a new lecture
curl -X POST http://localhost:3000/api/lectures \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"content": "# Example Lecture\n\nThis is an example lecture content.",
"level": "B1",
"typeWrite": "Article",
"language": "en",
"time": 300
}'
interface IUser {
username: string;
email: string;
password: string;
role: "admin" | "user";
firstName?: string;
lastName?: string;
isActive: boolean;
createdAt: Date;
updatedAt: Date;
}
interface IWord {
word: string;
definition: string;
examples?: string[];
type?: string[];
IPA?: string;
seen?: number;
img?: string;
level?: "easy" | "medium" | "hard";
synonyms?: string[];
codeSwitching?: string[];
language: string;
spanish?: {
definition: string;
word: string;
};
}
interface ILecture {
time: number;
level: string;
typeWrite: string;
language: string;
img?: string;
urlAudio?: string;
content: string;
createdAt?: Date;
updatedAt?: Date;
}
- Model: GPT-4o-2024-08-06
- Features:
- CEFR level adaptation (A1-C2)
- Markdown formatting
- Customizable content types
- Streaming responses
- Model: DALL-E 3
- Features:
- Contextual word images
- Lecture illustrations
- Cloudinary integration
- Service: OpenAI Text-to-Speech
- Voices: nova, alloy, echo, fable, onyx, shimmer
- Format: WAV with SRT subtitles
# Development
npm run dev # Start development server with nodemon
npm run build # Build TypeScript to JavaScript
npm start # Start production server
# Docker
docker-compose up # Start all services
docker-compose down # Stop all services
# Database
npm run seed # Seed initial data
npm run backup # Backup database collections
βββ src/
β βββ app/
β β βββ controllers/ # Request handlers
β β βββ db/
β β β βββ models/ # MongoDB schemas
β β β βββ mongoConnection.ts
β β βββ middlewares/ # Express middlewares
β β βββ routes/ # API route definitions
β β βββ services/ # Business logic
β β β βββ ai/ # OpenAI integrations
β β β βββ auth/ # Authentication
β β β βββ cloudinary/ # Image storage
β β β βββ lectures/ # Educational content
β β β βββ words/ # Vocabulary management
β β βββ utils/ # Helper functions
β βββ main.ts # Application entry point
βββ swagger/ # API documentation
βββ public/ # Static files (images, audio)
βββ logs/ # Application logs
βββ docs/ # Project documentation
app.use(cors({
origin: [
"http://localhost:5173",
"https://languages-ai.alexandernova.pro",
],
credentials: true,
}));
- Framework: Winston
- Levels: info, error, debug
- Outputs: Console and file-based logging
- Files: app.log, errors.log, exceptions.log
-
Environment Setup
export NODE_ENV=production export PORT=3000
-
Build and Deploy
npm run build npm start
# Build production image
docker build -t language-ai-backend .
# Run with environment variables
docker run -d \
-p 3000:3000 \
-e NODE_ENV=production \
-e MONGO_URL=your_mongo_url \
-e OPENAI_API_KEY=your_openai_key \
language-ai-backend
curl http://localhost:3000/
# Response: {"success": true, "message": "Server is running", "data": {...}}
- Application logs:
logs/app.log
- Error logs:
logs/errors.log
- Exception logs:
logs/exceptions.log
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- OpenAI for providing powerful AI models
- MongoDB for the database solution
- Express.js for the web framework
- Cloudinary for image storage
- The open-source community for various tools and libraries
Made with β€οΈ for language learners worldwide