Integrating GPT‑4 into MediaWiki: A Step‑by‑Step Guide

Overview

Large‑language models (LLMs) such as OpenAI’s GPT‑4 can turn a plain MediaWiki installation into an intelligent knowledge‑assistant. This guide walks you through the entire process – from acquiring an API key to deploying a reusable extension that embeds a GPT‑4 chat widget on every page.

Prerequisites

  • MediaWiki ≥ 1.39 (the current LTS branch). You can verify your version on Special:Version.
  • Shell access to the server (SSH) and the ability to edit LocalSettings.php.
  • An OpenAI account with a valid GPT‑4 API key. The key is a string that looks like sk-… and must be kept secret.
  • PHP ≥ 7.4 with the curl extension enabled (check with php -m | grep curl).

Choosing an Integration Path

There are three common ways to hook GPT‑4 into MediaWiki:

  1. Simple API‑call extension – a lightweight extension that sends the user’s query to OpenAI and displays the answer. The Chatbot extension on mediawiki.org already provides a generic hook for any LLM; you only need to adjust the configuration.
  2. Special‑page assistant – a dedicated Special:GPT4Assistant page that offers a full‑screen chat UI. This pattern is used by the EditGPT extension (originally built for GPT‑3.5) and can be upgraded to GPT‑4.
  3. Iframe‑based front‑end – host a separate web app (e.g. osw‑chatbot) and embed it via the Iframe extension. This is the most flexible approach if you need custom UI components.

For most wikis the first option – extending the existing Chatbot extension – offers the best balance of simplicity and maintainability.

Step 1: Install the Base Extension

cd /path/to/mediawiki/extensions
git clone https://github.com/OpenSemanticWorld/mediawiki-extensions-Chatbot.git Chatbot

After cloning, add the loader line to LocalSettings.php:

wfLoadExtension( 'Chatbot' );

Navigate to Special:Version to confirm that the extension appears under “Experimental extensions”.

Step 2: Configure the GPT‑4 Backend

The Chatbot extension reads a JSON configuration variable named $wgChatbotPopupAssistentConfig. Create a small JSON file (e.g. extensions/Chatbot/config.json) with the following content:

{
  "wgChatbotPopupAssistentConfig": {
    "value": {
      "iframe_src": "https://api.openai.com/v1/chat/completions",
      "model": "gpt-4",
      "api_key": "YOUR_OPENAI_API_KEY"
    },
    "description": "OpenAI GPT‑4 endpoint used by the Chatbot popup."
  }
}

Then tell MediaWiki to read the file:

$wgChatbotPopupAssistentConfig = json_decode( file_get_contents( __DIR__ . '/extensions/Chatbot/config.json' ), true );

**Important security note** – never commit the API key to version control. Keep the file readable only by the web‑server user (chmod 600).

Step 3: Add a Parser Hook (optional)

If you want the chatbot widget to appear automatically on every article, add a parser hook that injects the markup. The extension already provides ParserFirstCallInit – you only need to enable it:

$wgHooks['ParserFirstCallInit'][] = function ( Parser $parser ) {
    $parser->setHook( 'gpt4chat', function ( $input, array $args, Parser $parser, PPFrame $frame ) {
        return "";
    } );
    return true;
};

Now you can place <gpt4chat/> anywhere in wikitext and the popup will be attached to that element.

Step 4: Front‑end JavaScript

Create extensions/Chatbot/modules/ext.chatbot.js (the extension’s default module name is ext.chatbot) with a minimal client that forwards the user’s prompt to the OpenAI endpoint:

mw.loader.using( 'mediawiki.api' ).then( () => {
    const button = document.createElement( 'button' );
    button.textContent = 'Ask GPT‑4';
    button.className = 'gpt4-button';
    document.body.appendChild( button );

    button.addEventListener( 'click', async () => {
        const prompt = prompt( 'Enter your question:' );
        if ( !prompt ) return;
        const resp = await fetch( mw.config.get( 'wgChatbotPopupAssistentConfig' ).value.iframe_src, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + mw.config.get( 'wgChatbotPopupAssistentConfig' ).value.api_key
            },
            body: JSON.stringify({
                model: mw.config.get( 'wgChatbotPopupAssistentConfig' ).value.model,
                messages: [{ role: 'user', content: prompt }]
            })
        } );
        const data = await resp.json();
        alert( data.choices[0].message.content );
    } );
} );

Register the module in extension.json (the file already exists; just ensure the resourceModules entry points to the script you created).

Step 5: Test the Integration

  1. Clear MediaWiki’s cache (php maintenance/update.php or the “Purge all caches” button in Special:Version).
  2. Visit any article, click the “Ask GPT‑4” button, and type a question.
  3. If everything is configured correctly you will see the model’s answer in an alert box. For production you would replace the alert with a nicer modal dialog – the extension already ships a CSS‑styled popup that can be reused.

Step 6: Harden the Setup

  • Rate limiting – add a per‑user token bucket using ApiCheckCanExecute to prevent a single user from exhausting your quota.
  • Content filtering – run the response through AbuseFilter to block disallowed words or unsafe HTML.
  • Logging – enable $wgDebugLogFile for the Chatbot namespace to keep an audit trail of API calls.

Alternative: Using the EditGPT Extension

If you prefer a “write‑assist” workflow (e.g. auto‑summarise or polish article text), the EditGPT extension provides a ready‑made UI inside the edit form. The upgrade steps are:

  1. Clone the repository into extensions/EditGPT.
  2. Load the extension with wfLoadExtension( 'EditGPT' );.
  3. After a page reload, the edit toolbar shows a “GPT‑4 Assist” button that opens a modal where you can paste a paragraph and ask the model to rewrite, translate, or summarize it.

Set the following variables in LocalSettings.php:

$wgEditGPTAPIKey = 'YOUR_OPENAI_API_KEY';
$wgEditGPTModel = 'gpt-4';
$wgEditGPTMaxTokens = 2048;
$wgEditGPTTemperature = 0.7;

Both the Chatbot and EditGPT extensions share the same security concerns, so apply the hardening steps described above.

Deploying to Production

  1. Move the configuration file outside the web root (e.g. /etc/mediawiki/gpt4‑config.json) and load it with an absolute path.
  2. Set display_errors = Off in PHP and enable HTTPS on the wiki – the OpenAI API rejects plain‑HTTP requests.
  3. Monitor usage via the OpenAI dashboard and adjust the per‑user quota in LocalSettings.php accordingly.
  4. Document the feature for editors (e.g. a help page Help:GPT‑4 Assistant) so they know the model’s limits and the privacy policy.

Conclusion

Integrating GPT‑4 into MediaWiki is now a matter of installing a small extension, providing a secure API key, and wiring a few configuration variables. The approach described above leverages existing, community‑maintained extensions (Chatbot, EditGPT) and follows MediaWiki’s best‑practice guidelines for extensions, hooks, and resource modules. With rate‑limiting, logging, and content filters in place, you can safely give your community the power of a state‑of‑the‑art language model while keeping the wiki’s core principles intact.

Further Reading

Subscribe to MediaWiki Tips and Tricks

Don’t miss out on the latest articles. Sign up now to get access to the library of members-only articles.
jamie@example.com
Subscribe