Overview
The AI Chatbot endpoint provides students with a personalized learning companion powered by Groq AI. The assistant leverages the student’s course enrollment, progress data, and quiz performance to provide contextual guidance and study recommendations.POST /api/user/ai-chat
Send a message to the AI assistant and receive a personalized response based on your learning context.Authentication
Requires JWT authentication token in the Authorization header.Request Body
The user’s message to the AI assistant. Must be a non-empty string.
Optional session ID to continue an existing conversation. If not provided, a new session will be created.
Request Example
Response
Indicates whether the request was successful.
The session ID for this conversation. Use this to continue the conversation in subsequent requests.
The AI assistant’s response to the user’s message.
Complete conversation history including all user and assistant messages.
Response Example
Error Responses
How It Works
Personalized Context
The AI assistant builds a comprehensive learning context for each user that includes:- Student Profile: Name and enrolled courses
- Course Progress: Completion percentage and lecture tracking for each course
- Quiz Performance: Recent quiz results with scores, performance levels (Needs Review, On Track, Mastered), and AI-generated study recommendations
- Learning Patterns: Analysis of strengths and areas needing improvement
System Prompt
The assistant is configured with the following capabilities:- Help with course content and technical learning questions
- Provide study guidance based on progress data
- Offer personalized recommendations using quiz performance
- Proactively suggest review for topics marked “Needs Review”
- Maintain a concise, encouraging, and educational tone
Groq AI Integration
The chatbot uses Groq’s high-performance AI inference with the following configuration:- Model:
openai/gpt-oss-120b- A 120B parameter open-source model - Temperature: 0.7 - Balanced between creativity and consistency
- Max Tokens: 5000 - Supports detailed explanations
- Top P: 1.0 - Full probability mass for response generation
- Streaming: Disabled - Returns complete responses
Context Management
- Conversation history is limited to the last 20 messages for optimal performance
- System prompt is regenerated on each request to ensure context freshness
- User data is fetched in real-time to reflect current progress
- Session IDs are auto-generated using UUID v4
Data Sources
The assistant pulls data from:Usermodel: Name, enrolled coursesCourseProgressmodel: Lecture completion trackingQuizResultmodel: Recent quiz scores and AI recommendationsCoursemodel: Course titles, chapters, and content structure
Implementation Details
Request Validation
Requests are validated using Zod schema:Session Management
- Sessions are stored in MongoDB using the
ChatSessionmodel - Each session is tied to a specific user (userId ensures ownership)
- New sessions are created automatically with UUID v4 identifiers
- Existing sessions are retrieved using both sessionId and userId for security
Context Building
ThebuildUserContext() function aggregates:
- User’s enrolled courses with populated course details
- Progress records for all enrolled courses
- Recent quiz results (limited to last 20)
- Calculated completion percentages
- Chapter and lecture information
- Latest AI study recommendations from quiz results
Best Practices
- Session Continuity: Always include the
activeSessionIdfrom previous responses to maintain conversation context - Specific Questions: The AI performs best with specific questions about courses, topics, or study strategies
- Learning Context: The assistant’s recommendations improve as students complete more quizzes and courses
- Fresh Data: User context is rebuilt on every request to ensure recommendations reflect current progress
Rate Limiting
Consider implementing rate limiting on the client side to prevent excessive API calls to Groq’s service.Related Endpoints
- Get Chat Session - Retrieve a specific conversation
- Delete Chat Session - Remove a conversation
- Previous Chats - List all user conversations