> For the complete documentation index, see [llms.txt](https://ispectra.gitbook.io/mutamad/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ispectra.gitbook.io/mutamad/site-building/ai.md). Agents can also search and read this documentation through its [MCP server](https://ispectra.gitbook.io/mutamad/~gitbook/mcp).

# AI

AI help for editors and a visitor chatbot, on the entity's own model server only.

Mutamad's AI is built on one principle: **the AI suggests, a person decides** — and it runs on the entity's own server, nowhere else.

{% hint style="info" %}
**Why the entity's own server?** At the time of release, no provider with in-Kingdom data residency is confirmed for government use. So the entity runs a server that speaks the OpenAI API (Ollama, vLLM, LiteLLM) and names it in `settings.php`. Drupal CMS's default provider, outside the Kingdom, is not used.
{% endhint %}

## AI for editors (`mutamad_ai`)

| Feature                         | Where                                                                               |
| ------------------------------- | ----------------------------------------------------------------------------------- |
| **Alternative text** for images | The image field: generate, then check                                               |
| **Translation drafts**          | The Translate tab: the draft goes through the approval workflow                     |
| **Writing help**                | The editor's AI menu: spelling and grammar, summary, rewrite, translate a selection |
| **Fix accessibility**           | The editor's AI menu, on the selected text                                          |
| **Suggest** buttons             | Beside the SEO title and description, Topics and Audiences of every content type    |

The *Suggest* buttons fill the form only when pressed; they never save, and saving asks the AI nothing.

### Guardrails

* Every other AI provider is hidden.
* A request with no server named is refused before it is sent.
* A national ID, Iqama or card number in a request stops it.
* Every request is logged, without its text.
* House-style rules are in the prompts.

### For the administrator only

**Canvas AI** (the assistant in the page builder) and **AI Agents** come with `mutamad_ai`; **AI Figma** (pages from a Figma design) is its own recipe, `mutamad_ai/figma`. No job role has their permissions. What Canvas AI builds is a draft, published by a reviewer. Its code-component agent is removed: a code component is JavaScript on the public site.

### Set it up

{% stepper %}
{% step %}

### Run a model server

**Ollama** for a pilot or a small entity:

```bash
curl -fsSL https://ollama.com/install.sh | sh
ollama pull qwen2.5:14b      # writes and translates
ollama pull qwen2.5vl:7b     # describes images
```

The API is then at `http://<server>:11434/v1`. A GPU with 24 GB of memory runs both. For several editors at once, use **vLLM**, with LiteLLM in front.
{% endstep %}

{% step %}

### Name it in settings.php

```php
$settings['mutamad_ai'] = [
  'endpoint' => 'https://ai.example.gov.sa/v1',
  'chat_model' => 'qwen2.5:14b',
  'vision_model' => 'qwen2.5vl:7b',
  // Only with the chatbot:
  'embeddings_model' => 'bge-m3',
];
```

Put the server's key, if it asks for one, in the `MUTAMAD_AI_KEY` environment variable (any value for a server that asks for none). The key is in neither the database nor the code.
{% endstep %}

{% step %}

### Apply the recipe

```bash
drush recipe modules/contrib/mutamad_ai
```

{% endstep %}
{% endstepper %}

{% hint style="warning" %}
Mutamad does not check where the server is. A server outside the Kingdom is a transfer of personal data abroad (PDPL Art. 29): the entity's data protection officer decides first.
{% endhint %}

## The visitor chatbot (preview)

<figure><img src="https://171036656-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQSekKpxOCXC1q6G2NRy9%2Fuploads%2Fgit-blob-68a206fd70787333e7e9e5c769ecef6e485141c0%2Fchatbot-en.jpg?alt=media" alt="The site assistant answering a question"><figcaption><p>The site assistant, after DGA's Chatbot Template.</p></figcaption></figure>

`mutamad_ai_chatbot`, after DGA's Chatbot Template:

* Answers in Arabic or English, in the page's language, **only from published content** the visitor may see — Knowledge articles, Services, FAQ and Pages — with links to its sources.
* When the site has no answer, it says so and offers the contact form.
* It says it is AI.
* Stops national ID, Iqama and card numbers; limits questions per visitor per hour; **stores no question**.
* Search by meaning with AI Search and a SQLite vector store in a private file — no extra server.

### The knowledge base

`mutamad_knowledge_base` adds the **Knowledge article**: a short, approved answer, the other ways people ask it, the service and regulation it rests on, and a **review date**. Editors get the list of overdue articles, and the chatbot stops citing an article once it is due for review — an assistant must not repeat an outdated answer.

### Set it up

1. Set up `mutamad_ai` with an `embeddings_model` (`bge-m3` reads Arabic best; `qwen3:8b` answered best as the chat model).
2. Install the `sqlite-vec` extension for PHP, and have a private file path.
3. Add and apply the knowledge base and the chatbot (see [Optional features](https://ispectra.gitbook.io/mutamad/site-building/optional-features.md#applying-one)).
4. Write knowledge articles, and let cron index them.

{% hint style="info" %}
**Why a preview?** AI Search, which the chatbot needs, is still alpha and outside drupal.org's security coverage. Everything else it uses is stable.
{% endhint %}
