Skip to main content

Overview

The RAG (Retrieval-Augmented Generation) Copilot provides an intelligent chatbot that answers questions based on course content. It supports both course-specific and organization-wide modes.

Chat with Copilot

curl -X POST "https://api.learnhouse.app/api/v1/ai/rag/chat" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "message": "What are the key concepts in photosynthesis?",
    "course_uuid": "course_abc123",
    "mode": "course_only"
  }'
Send a chat message and receive an AI-generated response with source references.

Request Body

message
string
required
User’s question or message
course_uuid
string
UUID of the course to search within. If omitted, searches across all courses in the org
aichat_uuid
string
Existing chat session UUID. If omitted, creates a new session
mode
string
default:"course_only"
Chat mode: course_only (grounded in course content) or general (broader knowledge)
org_slug
string
Organization slug (used for cross-course mode when course_uuid is omitted)

Response (SSE Stream)

Returns Server-Sent Events with multiple event types:
type
string
Event type: start, chunk, sources, done, follow_ups, session_title, or error

Event Types

start Event

data: {"type": "start", "aichat_uuid": "chat_xyz"}
Indicates streaming has begun.

chunk Event

data: {"type": "chunk", "content": "Photosynthesis is the process..."}
Streaming content chunk.

sources Event

data: {
  "type": "sources",
  "sources": [
    {
      "activity_uuid": "activity_123",
      "activity_name": "Chapter 3: Photosynthesis",
      "course_uuid": "course_abc",
      "course_name": "Biology 101",
      "chunk_text": "...relevant excerpt..."
    }
  ]
}
Source references from course content.

follow_ups Event

data: {
  "type": "follow_ups",
  "follow_up_suggestions": [
    "What role does chlorophyll play?",
    "How does light intensity affect photosynthesis?"
  ]
}
AI-generated follow-up questions.

session_title Event

data: {"type": "session_title", "title": "Photosynthesis Basics"}
Auto-generated title for new sessions.

done Event

data: {"type": "done", "aichat_uuid": "chat_xyz"}
Streaming complete.
The chat session UUID (aichat_uuid) should be included in subsequent messages to maintain conversation history.

Manually Index Course

curl -X POST "https://api.learnhouse.app/api/v1/ai/rag/index" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "course_uuid": "course_abc123"
  }'
Manually trigger re-indexing of a course’s content for RAG.

Request Body

course_uuid
string
required
UUID of the course to index

Authorization

Requires admin or maintainer role on the course’s organization.

Response

status
string
Always success on completion
chunks_indexed
integer
Number of content chunks indexed

Example Response

{
  "status": "success",
  "chunks_indexed": 142
}
Indexing is automatic when courses are created/updated. Manual indexing is only needed for troubleshooting or forcing re-indexing.

Session Management

List Chat Sessions

curl "https://api.learnhouse.app/api/v1/ai/rag/sessions" \
  -H "Authorization: Bearer YOUR_TOKEN"
Retrieve all chat sessions for the current user.

Response

sessions
array
Array of session objects with metadata

Get Session Messages

curl "https://api.learnhouse.app/api/v1/ai/rag/sessions/chat_xyz/messages" \
  -H "Authorization: Bearer YOUR_TOKEN"
Load message history for a specific session.

Response

messages
array
Array of message objects with role, content, and sources

Delete Session

curl -X DELETE "https://api.learnhouse.app/api/v1/ai/rag/sessions/chat_xyz" \
  -H "Authorization: Bearer YOUR_TOKEN"
Delete a chat session.

Response

{"status": "deleted"}

Update Session Metadata

curl -X PATCH "https://api.learnhouse.app/api/v1/ai/rag/sessions/chat_xyz" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "My Photosynthesis Notes",
    "favorite": true
  }'
Update session title and/or favorite status.

Request Body

title
string
New session title
favorite
boolean
Mark session as favorite

Feature Availability

RAG Copilot can be disabled at the organization level via organization config:
{
  "features": {
    "ai": {
      "enabled": true,
      "copilot_enabled": true
    }
  }
}
If disabled, chat requests return a 403 error.

Implementation Details

  • Embedding Model: Content is chunked and embedded for semantic search
  • Session Storage: Chat history stored in Redis
  • AI Model: Uses gemini-2.5-flash for all plans
  • Credits: Each chat message deducts 1 AI credit
  • Source Code: apps/api/src/routers/ai/rag.py:127

Build docs developers (and LLMs) love