Aggregator

jofitz: Drupal AI: Custom AI Helper module

2 weeks 3 days ago

Struggling with repetitive code when using Drupal's AI module for chat operations? Discover how the AI Helper module simplifies your development by streamlining common tasks, letting you focus on unique solutions instead of boilerplate.

Having written a couple of Drush scripts that utilise the Chat operation of the Drupal AI module (e.g. see part 1 in this series of Drupal AI articles) it was clear that certain operations were repeated in each task. I wrote a module, AI Helper, to provide a service that would avoid the need for repeated code.

Code Stages

It made sense to me to separate the code into three stages, each of which will be explained below:

  1. Pre-processing
  2. Processing
  3. Post-processing
1) Pre-processing

The first stage consists of converting the input into the format required for the Chat request and obtaining the Provider and Model.

1a) Prepare input

Working back from the parameters of the Chat request to the simplest input to the service, the logic was as follows:

  • The chat() call...
Read more