# AI Setup

> Turn on AI features in self-hosted LearnHouse with Gemini, OpenAI, Anthropic, Bedrock, Ollama or another provider, and set models and RAG embeddings.

LearnHouse includes AI-powered features for learning assistance and content editing. The AI
layer is **provider-agnostic**: point it at any major LLM provider by setting a provider name
and an API key. These features are optional and disabled by default.

Under the hood LearnHouse uses [Pydantic AI](https://ai.pydantic.dev/) for generation, and
stores embeddings in PostgreSQL with **pgvector** for RAG (Retrieval-Augmented Generation), so
AI assistants can answer questions using your course content as context. Course content is
indexed in the background inside the API process (see the background job variables in the
[environment variable reference](https://docs.learnhouse.app/self-hosting/configuration/environment-variables#background-jobs-and-media-processing)).

## Supported providers

<div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(150px, 1fr))', gap: '12px', marginTop: '1.25rem' }}>
{[
{ name: 'Google Gemini', id: 'google', logo: 'google' },
{ name: 'OpenAI', id: 'openai', logo: 'openai' },
{ name: 'Anthropic', id: 'anthropic', logo: 'anthropic' },
{ name: 'DeepSeek', id: 'deepseek', logo: 'deepseek' },
{ name: 'Moonshot (Kimi)', id: 'moonshot', logo: 'moonshot' },
{ name: 'Mistral', id: 'mistral', logo: 'mistral' },
{ name: 'OpenRouter', id: 'openrouter', logo: 'openrouter' },
{ name: 'AWS Bedrock', id: 'bedrock', logo: 'bedrock' },
{ name: 'Ollama (local)', id: 'ollama', logo: 'ollama' },
].map((p) => (
<div key={p.id} style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '10px', padding: '18px 12px', border: '1px solid var(--lh-border)', borderRadius: '12px', textAlign: 'center' }}>
      <img src={`/img/logos/ai-providers/${p.logo}.svg`} alt={p.name} height="34" style={{ height: '34px', width: 'auto', objectFit: 'contain' }} />
      <div style={{ fontWeight: 600, fontSize: '0.9rem' }}>{p.name}</div>
      <code style={{ fontSize: '0.72rem' }}>{p.id}</code>
</div>
))}
</div>

Any OpenAI-compatible endpoint (Azure OpenAI, Together, local servers) also works with
`LEARNHOUSE_AI_PROVIDER=openai` plus a custom `LEARNHOUSE_AI_BASE_URL`.

## Enabling AI

Set the provider, an API key, and the feature flag in your `.env` file:

```env
LEARNHOUSE_IS_AI_ENABLED=true
LEARNHOUSE_AI_PROVIDER=google           # google | openai | anthropic | deepseek | moonshot | mistral | openrouter | bedrock | ollama
LEARNHOUSE_AI_API_KEY=your-api-key      # the single key, whatever the provider
```

After updating your `.env` file, apply it (this recreates the containers whose configuration changed):

```bash
npx learnhouse start
```

The setup wizard only asks for a Gemini key. For any other provider, add these variables to `.env` yourself.

> **Upgrading from a Gemini-only setup.** No config changes are required: if you only set
> `LEARNHOUSE_GEMINI_API_KEY`, `provider` defaults to `google` and that key is used automatically
> (for generation **and** embeddings). The one difference is that the default models are now the
> **Gemini 3 family** (`gemini-3.5-flash`, `gemini-3.1-flash-lite`, `gemini-3.1-pro-preview`)
> rather than Gemini 2.5, so your key/project must have Gemini 3 access (the Pro tier uses a
> *preview* model). To stay on the previous models, pin them explicitly:
> 
> ```env
LEARNHOUSE_AI_MODEL_FAST=gemini-2.0-flash-lite
LEARNHOUSE_AI_MODEL_STANDARD=gemini-2.5-flash
LEARNHOUSE_AI_MODEL_PRO=gemini-2.5-pro
```

> AI usage is billed directly by your chosen provider based on the models and volume used.

## Per-provider examples

**Google Gemini**

Get a key from [Google AI Studio](https://aistudio.google.com/apikey). Models default to Gemini, so you don't need to set them.

```env
LEARNHOUSE_AI_PROVIDER=google
LEARNHOUSE_AI_API_KEY=...
```

**OpenAI**

```env
LEARNHOUSE_AI_PROVIDER=openai
LEARNHOUSE_AI_API_KEY=sk-...
LEARNHOUSE_AI_MODEL_STANDARD=gpt-4o
LEARNHOUSE_AI_MODEL_FAST=gpt-4o-mini
```

**Anthropic**

```env
LEARNHOUSE_AI_PROVIDER=anthropic
LEARNHOUSE_AI_API_KEY=sk-ant-...
LEARNHOUSE_AI_MODEL_STANDARD=claude-sonnet-4-5
LEARNHOUSE_AI_MODEL_FAST=claude-haiku-4-5
```

**DeepSeek**

DeepSeek's OpenAI-compatible endpoint is auto-configured, so you only need to set the key.

```env
LEARNHOUSE_AI_PROVIDER=deepseek
LEARNHOUSE_AI_API_KEY=sk-...
LEARNHOUSE_AI_MODEL_STANDARD=deepseek-chat
```

**Moonshot (Kimi)**

The Moonshot AI (Kimi) endpoint is auto-configured.

```env
LEARNHOUSE_AI_PROVIDER=moonshot
LEARNHOUSE_AI_API_KEY=sk-...
LEARNHOUSE_AI_MODEL_STANDARD=kimi-k2-0905-preview
```

Other Chinese models (Alibaba **Qwen**, Zhipu **GLM**) expose OpenAI-compatible APIs. Set `LEARNHOUSE_AI_PROVIDER=openai` and their endpoint in `LEARNHOUSE_AI_BASE_URL`, for example `https://dashscope-intl.aliyuncs.com/compatible-mode/v1`.

**Mistral**

```env
LEARNHOUSE_AI_PROVIDER=mistral
LEARNHOUSE_AI_API_KEY=...
LEARNHOUSE_AI_MODEL_STANDARD=mistral-large-latest
```

**OpenRouter**

OpenRouter's base URL is configured automatically; use OpenRouter **model slugs** (`vendor/model`). One key gives access to hundreds of models.

```env
LEARNHOUSE_AI_PROVIDER=openrouter
LEARNHOUSE_AI_API_KEY=sk-or-...
LEARNHOUSE_AI_MODEL_STANDARD=anthropic/claude-sonnet-4-5
LEARNHOUSE_AI_MODEL_FAST=openai/gpt-4o-mini
```

**AWS Bedrock**

Bedrock uses the **standard AWS credential chain** (env vars, IAM role, or `~/.aws` profile) plus a region. No `LEARNHOUSE_AI_API_KEY` is required. Use Bedrock model IDs.

```env
LEARNHOUSE_AI_PROVIDER=bedrock
LEARNHOUSE_AI_MODEL_STANDARD=anthropic.claude-sonnet-4-5-20250929-v1:0
# AWS credentials & region via the standard AWS environment:
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=...
AWS_SECRET_ACCESS_KEY=...
```

**Ollama**

Run any model locally with [Ollama](https://ollama.com) (`ollama serve`). No API key is needed.

```env
LEARNHOUSE_AI_PROVIDER=ollama
LEARNHOUSE_AI_BASE_URL=http://localhost:11434/v1
LEARNHOUSE_AI_MODEL_STANDARD=qwen2.5:3b
```

## Models & tiers

LearnHouse maps features to three model tiers. Leave them unset to use the built-in Gemini 3
defaults, or override any of them with a model name valid for your provider.

| Env variable | Used for | Default (Gemini 3) |
| --- | --- | --- |
| `LEARNHOUSE_AI_MODEL_FAST` | Chat titles, follow-up suggestions, course migration | `gemini-3.1-flash-lite` |
| `LEARNHOUSE_AI_MODEL_STANDARD` | Activity chat, RAG answers, and course planning / MagicBlocks / boards / playgrounds on free & standard plans | `gemini-3.5-flash` |
| `LEARNHOUSE_AI_MODEL_PRO` | Course planning / MagicBlocks / boards / playgrounds on Pro+ plans | `gemini-3.1-pro-preview` |

## RAG embeddings

Retrieval-Augmented Generation (course Q&A grounded in your content) needs an **embedding
model**. By default embeddings **follow your chosen provider**, using a sensible default
embedding model:

| Provider | Default embedding model |
| --- | --- |
| Google | `gemini-embedding-001` |
| OpenAI (and Azure / Together / OpenAI-compatible) | `text-embedding-3-small` |
| Ollama (local) | `nomic-embed-text` |

Override the embedding provider/model independently of generation if you like:

```env
LEARNHOUSE_AI_EMBEDDING_PROVIDER=openai          # default: same as LEARNHOUSE_AI_PROVIDER
LEARNHOUSE_AI_EMBEDDING_MODEL=text-embedding-3-large
LEARNHOUSE_AI_EMBEDDING_DIMENSIONS=768           # default: 768
```

> **Two constraints to know:**
> 
> 1. **Dimensions are pinned to 768** to match the pgvector column. The defaults above all
> produce 768-dim vectors. If you set `LEARNHOUSE_AI_EMBEDDING_DIMENSIONS` to another value
> (or use a model with a fixed, different size), you must migrate the `course_embedding`
> column and re-index existing courses.
> 2. **Some providers have no embeddings API**: Anthropic, DeepSeek, Moonshot, Mistral, OpenRouter, and Bedrock. With one of those selected for generation, embeddings automatically fall back to
> **Google** when `LEARNHOUSE_GEMINI_API_KEY` is set; otherwise set
> `LEARNHOUSE_AI_EMBEDDING_PROVIDER` to `google`, `openai`, or `ollama`.

Non-RAG features (activity chat, course planning, MagicBlocks, the editor assistant) don't need
embeddings and work on any provider.

## File & video context (course planning)

Course planning can take **images, PDFs, and YouTube links** as reference material. LearnHouse
converts each attachment into the right format for your provider; what actually works depends on
the provider's own capabilities:

| Attachment | Google Gemini | OpenAI | Anthropic (Claude) | DeepSeek / Mistral / others |
| --- | --- | --- | --- | --- |
| Images | Yes | Yes | Yes | Yes, on vision models |
| PDFs | Yes | Yes, on document-capable models | Yes | Depends on the model |
| YouTube URLs | Yes | No | No | No |

Images and PDFs are widely supported across vision/document-capable models. **YouTube links are
a Gemini-only capability**. Gemini fetches the video directly, while other providers have no
equivalent. When an attachment type isn't supported, the request fails with a clear error rather
than silently dropping it, so if you rely on **YouTube** course context, keep `LEARNHOUSE_AI_PROVIDER=google`.

## Per-organization AI configuration

Once AI is enabled on the instance, each organization can toggle AI on or off and set usage
limits from its admin settings. The provider and API key are set once at deployment and shared
across organizations.

## Media generation (images & audio)

Image generation and audio / podcast generation (text-to-speech) are **Google-only** paths: they
always use the Google GenAI SDK regardless of `LEARNHOUSE_AI_PROVIDER`, resolving their key from
`LEARNHOUSE_AI_API_KEY` (when `provider=google`) or `LEARNHOUSE_GEMINI_API_KEY`. So if you already
run on Gemini, they work with no extra configuration.

Both model ids default to sensible values and only need overriding to opt into a newer/allowlisted
model:

```env
# AI image generation ("nano banana"). Default: gemini-2.5-flash-image
LEARNHOUSE_AI_IMAGE_MODEL=gemini-2.5-flash-image

# AI audio / podcast generation (Gemini TTS). Default: gemini-2.5-flash-preview-tts
# All Gemini TTS models are currently preview, so this is kept configurable.
LEARNHOUSE_AI_TTS_MODEL=gemini-2.5-flash-preview-tts
```

The audio feature powers the editor's **Generate** audio tab (text-to-speech, a two-voice podcast,
or a single-voice "Speak" talk). The spoken script for the podcast/Speak modes is written by the
standard text model (`LEARNHOUSE_AI_MODEL_STANDARD`); the narration uses the TTS model above. All of
it is metered by [AI credits](https://docs.learnhouse.app/platform/ai).

## Usage limits

You can configure a per-organization request limit to control API costs. When a user exceeds the
limit, further AI requests are rejected until the window resets.

## Environment variables reference

| Variable | Description |
| --- | --- |
| `LEARNHOUSE_IS_AI_ENABLED` | Master switch for all AI features (`true`/`false`). |
| `LEARNHOUSE_AI_PROVIDER` | `google`, `openai`, `anthropic`, `deepseek`, `moonshot`, `mistral`, `openrouter`, `bedrock`, or `ollama`. Defaults to `google`. |
| `LEARNHOUSE_AI_API_KEY` | API key for the chosen provider. Not required for `ollama` or `bedrock`. |
| `LEARNHOUSE_AI_BASE_URL` | Optional. Custom endpoint for OpenAI-compatible / local providers (e.g. Ollama). Auto-set for OpenRouter. |
| `LEARNHOUSE_AI_MODEL_FAST` / `_STANDARD` / `_PRO` | Per-tier model overrides (see table above). |
| `LEARNHOUSE_AI_EMBEDDING_PROVIDER` / `_MODEL` / `_DIMENSIONS` | Optional RAG embedding overrides. Default: same provider, per-provider model, 768 dims. |
| `LEARNHOUSE_AI_IMAGE_MODEL` | Google image-generation model. Default `gemini-2.5-flash-image`. Google-only path. |
| `LEARNHOUSE_AI_TTS_MODEL` | Google text-to-speech model for audio/podcast generation. Default `gemini-2.5-flash-preview-tts` (all Gemini TTS models are preview). Google-only path. |
| `LEARNHOUSE_GEMINI_API_KEY` | Google/Gemini key. Also the **embeddings fallback** for providers without an embeddings API, and the key used for image/TTS generation on non-Google providers. |
| `AWS_REGION` / `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` | Standard AWS credentials, used only when `provider=bedrock`. |
