---
url: 'http://workflows-of-ai.com/blog/ai-module-1'
title: 'AI module #1'
author:
  name: 'Marcus Johansson'
  url: 'http://workflows-of-ai.com/about/marcus-johansson'
date: '2024-06-21T17:52:50+02:00'
updated: '2026-10-10T12:14:04+02:00'
type: article
summary: 'Developer intro to the Drupal AI module: call any provider through one service, swap OpenAI for Groq or DreamStudio, get keys and events.'
tags:
  - 'Marcus Johansson'
  - Workflows
  - 'Drupal AI'
image: 'http://workflows-of-ai.com/sites/default/files/previews/6542cde6ff472f87.jpg'
published: true
og:
  site_name: 'Workflows of AI'
  'image:width': '1280'
  'image:height': '720'
  'image:alt': 'AI module #1'
---
Drupal AI

# AI module #1

Why developers should build on the AI module: one service for every provider, normalized calls and models you can swap in one line.

  Marcus Johansson

June 21, 2024 · 1 min read

 ## About this video

Introducing the AI module (<https://www.drupal.org/project/ai>).

I will showcase a lot of videos about the AI module. In this first video we are showcasing why you should use the AI module as a foundation layer for your private or contributed modules.

This will help you with getting access to clients, switching providers, setting up the providers and much, much more.

*This video is from before the AI Interpolator module became AI Automators, part of the [AI module](https://www.drupal.org/project/ai). Module and menu names have changed since, and some steps look different today.*

## Watch the video

[Watch on YouTube: AI module #1](https://www.youtube.com/watch?v=s1O_KjSnFqU)

### Video chapters

- 0:00 A foundation module for Drupal AI
- 1:53 Where the abstraction layer comes from
- 2:30 AI Core and the providers
- 3:27 Calling the OpenAI client directly
- 5:26 Using the AI provider service
- 7:56 Image helpers on the output
- 9:15 Swapping to Groq and DreamStudio
- 10:38 Keys, events and logging

## Transcript

YouTube's automatic captions, lightly corrected. Click a line to jump to that moment in the video.

### Transcript

- 0:00 hey there as always this is sponsored by FreelyGive FreelyGive uh pays my salary to contribute to the community in terms of AI if you ever need help with AI and Drupal that combination we are
- 0:15 experts and it's not just me there's other Consultants as well that knows this uh field inside out so please contact us if you need help with uh anything for your Enterprise or or paid
- 0:28 Solutions today I'm going to talk about something else than the AI inter interpolator I'm going to talk about a completely new module that has been in the planning for
- 0:39 a couple of weeks so a lot of the people from the AI Community came together basically uh with the idea to create a foundational module for Drupal and this is something I wish
- 0:52 existed when I was doing the AI interpolator I have like created abstraction layer in the AI interpolator but it's still very much just for the interpolator so what this is going to be is uh first
- 1:08 and maybe most important a foundation and abstraction layer for all possible AI providers for all possible types of calls you can do for AI the video I'm going to show today has is actually very
- 1:24 different from everything else I show usually I show something that anyone should be able to set up without knowing develop Vel M today I'm just going to talk to
- 1:33 developers so I'm going to Showcase why you should use this when you're either working privately with AI and Drupal or when you're creating a contributed module and why you should uh rely on us
- 1:48 so uh if you install these well well I will also say two things about the module AI interpolator is going to be built in here so it's going to be renamed to AI Automator and it's going
- 2:01 to be the version two uh also what I'm talking about today the abstraction layer that all comes from the brain of uh Michal Gow so this guy seogow that created the llm
- 2:15 abstraction layer that is now deprecated that has gone into the AI and I have just tried to uh add a lot of features on top of it so that's what I've been working on the last week so that's what
- 2:28 I'm going to showcase so now if you get this uh module and search for AI there's going to be a lot of modules but what's important is this one AI Core so that's what you need to
- 2:42 be able to use this layer I'm going to talk about today then of course you need some kind of Provider so you need to work against some Service as you can see it already comes with a lot of providers
- 2:53 that you can use uh both uh like services like OpenAI or or like model cataloges like Hugging Face but also uh LM Studio Ollama that you can actually host locally or set up on a
- 3:08 server so you can make sure that your prompts never leaves your company Network um so you just need to install one of those uh to get going with this there's small documentation here or a
- 3:22 little documentation I haven't written enough there's going to come more documentation soon um so so uh what I want to showcase in this case oh sorry about the image
- 3:33 here but this is actually connected to what I want to showcase so this is the normal article in in um Drupal you install the Drupal standard installation I've added one field called image prompt
- 3:46 and what I want to do in this uh case is that I want to uh ask via prompt about um basically ask V prompt uh to take take this article and create a image prompt from this and then take this
- 4:03 image prompt and create this image of course you can use a interpolator you can use Augmentor AI to do this uh but in this case I want to develop it myself to to understand what I'm doing so I
- 4:16 have uh the possibility to write something like this right so uh and we're talking now before the the AI module existed so you you're basically having up you know if it's article
- 4:30 create a prompt you can you generate a sentence image and uh then you have this OpenAI client OpenAI is it's actually very easy so I'm taking the easiest route now because this is a very well
- 4:45 developed client that exists right you have to figure out how you're going to sideload the API key in a secure manner uh and then you're going to write this long array that is basically how
- 4:57 you're uh creating the first call here do the call make sure you do exceptions if something fails then you save this into the field and then you have to start creating the image and exception
- 5:11 and then since it's a image you have to create that image entity I didn't even write all this code because there's so much code that you have to write and then you have solved it right
- 5:21 so when you click save this is going to go into pre-save and start doing this but with this new layer you can do this instead so it looks like this um so there's a couple of things that's
- 5:33 important here so first of all we have the AI provider service so this is always what you reference when you're going to do a eii call um and then you can uh basically take this provider create a
- 5:48 instance which is a provider instance which can be in this case OpenAI then and in this case we want to make a chat message so if you if you just make sure that you have um
- 6:00 a good ID you're going to be able to autocomplete all these different objects right so in this case we take these messages and say that we want the same thing GPT-3.5 Turbo you don't
- 6:13 necessarily need to add this here you can set a default model for every type of operation and provider so you have something default but for now we say GPT-3.5 Turbo and we want this back
- 6:27 normalized so we're basically pushing in normalized values and we're getting normalized uh values back uh and I will Showcase in a minute why this is but I can also showcase that you can
- 6:43 actually do this if you want so if I copy the messages here I could instead do this because I know this is OpenAI and I could do get raw here and then it would actually get
- 6:56 me the raw response as it is the JSON uh array that comes from OpenAI um so it is possible to use it that way you can even do but I you should not do this but you can actually do this
- 7:12 even if there is something that the whole abstraction layer can solve you can actually do this you get the OpenAI client you can't necessarily do that with every provider but with OpenAI you
- 7:25 can do it so we get this and this returns then a chat message so this looks exactly like this it's it's just like dto objects right uh that you can connect uh some values to a a getter or
- 7:41 a Setter inside there so in this case we create a chat message we create another one and in this case we get the chat message back and then we can do the get message to get the message right we just
- 7:52 set it into file image prompt here's the also Nifty thing then we do another OpenAI and I'm going to show you why I'm not reusing it here set a configuration similar to how we did here that we just
- 8:05 want one well that's actually default I don't think I have to set that uh I don't think I have to set response format either but I just wanted to show you that you can set a configuration
- 8:15 here right same again we do a normalized input send it say we want all three but instead of get the get raw which is the response from OpenAI or get normalized
- 8:28 which is a a file binary I can have helper functions here depending on what it is we're trying to create so there is a get us image reference there is a get us media
- 8:39 reference there is a get us file reference when we're talking about file fields for instance uh which means in this case I can do get us image reference uh and this takes just where I
- 8:51 should put it and then it's solved so if I do this if we just want to resave uh oh I I haven't thought about the alt text we should probably set that but uh if I
- 9:03 resave now you see it takes time because it goes in here again and uh rewrites the prompt so you can just see the prompt here uh and but it's going to rewrite that now
- 9:15 right uh and the cool thing then when this is done when we say that okay this was really bad quality because we don't trust open in AI or oh Fireworks AI it's so much cheaper or I
- 9:29 want Groq because it's so fast so say that we uh uh want to use um the default mod model from from Gro instead then it's as simple as this we take away this WR Groq and that's it so now we're using
- 9:49 Groq instead same thing here there does only exist currently one image uh text to image model model but soon dream studio is going to come so you can do DreamStudio the configuration might differ of
- 10:03 course so either you just don't give a configuration and have the default values if you really want to make it flexible but you should probably expose this as a form I will come to this in
- 10:12 another video because we help you with this as well uh and because I've used normalized input and normalized output I can just swap hot swap however I want here right well if I do DreamStudio of
- 10:25 course here it should probably like sd3 or I can take the default model as well then um and that is it that is the reason why you should be using this there are a lot
- 10:44 of other reasons we can maybe talk about them because I actually I think I um written them down because I forget about it but basically here you know it's hot swapping is one thing takes care of API
- 10:56 keys so we're using the keys module it is as good of a pattern as you can get it can still still be saved in configuration by mistake but you know it is at least making sure that people
- 11:07 thinks think about it it also makes sure that there is always a consistent place so I will show you it's always under configuration AI setting provider settings and there's these different
- 11:18 configuration where you can just set a key so it's just one place not that every person that makes a contributed module wants to set their own form uh and this is not calling anyone out I do
- 11:31 the same thing with AI interpolator right um it also triggers event so it makes sure that every call uh that is done via this is triggering event which means that we can have stuff like
- 11:50 um how we show you we can have for instance a logging module that can log prompts and these prompts are getting tagged by like um you can manually tag each of the queries so this is just a
- 12:03 parameter I can show you here you can just add a third one and do my image call so when when you're uh triggering the event you can look for my image call for instance if you want to do something
- 12:18 specific before it sends it or after it sends it uh and this make sure that you can do stuff like moderation you can use external moderation so something we can
- 12:28 do uh for for instance with uh a very sensitive provider like Anthropic is that Anthropic doesn't have moderation themselves but we can put OpenAI moderation in front of it and
- 12:40 this is triggered via the event system then um so that's all that but uh all right so this takes care of logging the logging also I'm going to show at another time you can replay your
- 12:57 prompts which means that if this is log we're going to have prompt explorers I'm going to Showcase them at another time where you can actually see uh the underlying prompt that some module does
- 13:07 because then you can uh improve that uh prompt because that's a problem with the a interpolator it's adding a lot of prompting around your prompts with asking for JSON and doing like uh few
- 13:20 shot learning in many cases where it's giving examples and if you want to really figure out what's going wrong and maybe you want to use some playground then we have that playground where you
- 13:31 can try any model try any uh provider and see the actual prompt that like it was done right um so yeah that's it first video about AI more videos to come thank you

  Filed under [Workflows](/blog?category=6) [Drupal AI](/blog?category=2)

Parts of this content were produced by AI.

This site has not assessed whether a label is required for this content.

  How was AI used?The video and the introduction are Marcus's own, first published on workflows-of-ai.com. AI wrote the subtitle, the summary and the video chapters from the transcript, and corrected misheard names in YouTube's automatic captions.

   Marcus Johansson

Tinkers with AI and Drupal

 [About Marcus](/about/marcus-johansson)[Drupal.org profile](https://www.drupal.org/u/marcus_johansson)[Drupal AI](https://www.drupal.org/project/ai)

## The post in three sentences

The AI module is a new foundation and abstraction layer for AI providers in Drupal, and the AI Interpolator will move into it as AI Automator. Marcus compares a hand-written OpenAI client call with the AI provider service, using normalized input and output and a helper that saves a generated image as an image reference. He then swaps to Groq and DreamStudio and explains the shared key handling, events and logging.