Skip to content

AI API Explorer

Purpose of this module

This module provides a UI to allow admins to test various LLM prompts against different LLMs to refine the prompts. The form passes a prompt to an LLM and displays the result, but in most cases doesn't perform any further actions with the output (some of the forms that generate file, audio or image outputs do provide the ability to store the output as a Media item, but it is not intended to be used for bulk generation).

Providing your own forms

The forms themselves are provided by a Drupal plugin. To provide your own plugin, create a directory called AiApiExplorer in your module's src/Plugin folder and add a plugin file that extends the AiApiExplorerPluginBase. This implements the AiApiExplorerInterface interface, which details the required methods. Please be aware that some of these are implemented by default by the base plugin, but may be overridden.

The plugin will, as a minimum, need to:

  1. Have an id, label and description annotations. These will display within the automatically generated menu item for the plugin.
  2. Implement isActive() to determine if the plugin can be used.
  3. Implement the buildForm() method to build the form to show to users. The base plugin provides a getFormTemplate() method that will return a bare bones form to add elements to. Your form will also need to provide an AJAX submit button with the output of $this->getAjaxResponseId() as its callback.
  4. Implement the getResponse() method, which will be called during the AJAX submission and should send the form's prompt to its chosen LLM operation and rebuild the form with the response in the correct section. The method should return only the section of the form that will be replaced by the AJAX submission.

Any plugin will automatically have a route and menu item generated to show the form within the AI API Explorer module's admin pages. There is no requirement for you to add routing or menu files into your module for your form to be used.

The AI API Explorer module implements a number of plugins, which can be used as examples to aid your development:

  • The AI Search module's SearchGenerator plugin demonstrates a more complex method of deciding if the plugin should be active.
  • The AudioToAudioGenerator plugin demonstrates how to send a file to the LLM, and display a file within a media player for the user to interact with.
  • The ChatGenerator plugin demonstrates how to display the form within a three-column display instead of the default two-column.
  • The ChatGenerator plugin also demonstrates how to add debug information to the AJAX response to demonstrate to users how to code the call they have just performed, and how to handle streamed responses.

Showing text in any language

A text the user typed or the model returned can be in a right-to-left language, such as Arabic, Hebrew, Farsi or Urdu. The page is laid out in the interface language, so such a text has to be told to lay itself out.

  • Put the text in a block of its own and give it one of the shared classes, aie-run__title, aie-quote or aie-output. css/explorer-ui.css lists those, and the other blocks that hold a text, in one rule that takes the direction from the text.
  • Put a class of your own in that rule when the block cannot use a shared one.
  • When your explorer knows the language of the text, set dir on the block, with ExplorerUi::languageDirection(). That beats the first character, which gets an Arabic text starting with a Latin name wrong.
  • When a label puts a prefix in front of a text, such as "T2 ยท the text", wrap the text in <bdi>, or in ExplorerUi::isolate() where the label is also used in an attribute or an option. Without it the text turns the words around it the wrong way.