Main Content
AI for Drupal Development

Exploring AI for Drupal Development

2023 appears to be the year that Artificial Intelligence emerged from the shadows and into the mainstream, but the potential of AI has barely scratched the surface. AI is here and its impact on life and work is developing at an exponential pace

As this disruptive technology is generating quick answers, streamlining processes, and creating vast new efficiencies, hundreds of possibilities for AI – ranging from healthcare diagnoses, to cybersecurity threat detection, government services, Drupal development, API development, and many, many more – are taking shape.

This post will explore how an AI large language model (LLM), such as ChatGPT, can be used for writing code – specifically, a Drupal module.


Getting Started with an LLM: Prompt Engineering

An LLM is essentially a massive, simulated brain trained on an even more massive amount of data. LLMs such as ChatGPT are extremely good at identifying the "next best word" in a conversation to the point where human-like intelligence emerges.

Interacting with an LLM is done conversationally, beginning with a "prompt." Think of this process like writing a software program, with conversational English being the programming language.  

The first step in leveraging an LLM to give it context. Even if the context of what is being asked appears obvious, stating it explicitly can increase the quality of ideas and insights that are returned.  

For the purpose of generating code, a good example of a starting prompt could begin with: "You are an expert Drupal 10 developer..."  Since the answer is built one word at a time, an explicit prompt such as this helps to create a framework for success.  

What is the “Chain of Thought” Technique in AI?

Another technique called "Chain of Thought" can be very helpful to increase the quality of an AI response. The idea is to list out the steps the AI should take when thinking about a solution. This is similar to writing a software program except communication is at a higher level of abstraction.   

For example, rather writing a prompt that reads: "As an expert chef, please give me an apple pie recipe," the Chain of Thought technique would provide deeper, step-by-step instructions such as:

"As an expert chef, please think about different, popular apple pie recipes, specifically looking at the crust and the filling. Identify the best aspects from the different recipes. Consider how they could be combined to make an even better recipe. Finally, generate a final recipe."

For more complex topics, the Chain of Thought technique can be broken into multiple steps in a dialogue with an AI tool such as ChatGPT.

Refining the Prompt

Because an LLM can act as a natural language program interpreter, giving it rules to follow will help it to better achieve the right results. Here’s an example of writing a prompt for the purpose of help in writing prompts: 

"You are an expert Prompt Engineer. Your goal is to craft the best possible prompt to be used by ChatGPT. Please follow the process below:"

  • "Your first response will be to ask me what the prompt should be about. I will provide my answer, but we will need to improve it through continual iterations by going through the next steps."
  • "Based on my input, you will generate two sections: a) Revised prompt (provide your rewritten prompt. It should be clear, concise, and easily understood by you), and b) Ask any relevant questions pertaining to what additional information is needed from me to improve the prompt."
  • "We will continue this iterative process with me providing additional information to you, and you refining the original prompt in the 'Revised prompt' section until I decide this process is complete."
  • "Do not actually process the prompt once it is ready. That will be done in a separate conversation."


AI for Code Creation

Now that we've covered various aspects of Prompt Engineering, let's look at using LLM prompts for code generation. Here are some tips:

  • Generate fully completed code. If the solution you're generating is relatively straightforward, it may be easier to ask the AI to generate fully completed code rather than using placeholder comments. gpt-4 has a tendency to use placeholders which may or may not be helpful depending on the task at hand.
  • Let it know that it is okay to generate a long response. Your prompt could read something like: "It is okay if your response stops abruptly; I will say 'continue.'"
  • Be sure to give it context at the beginning, such as: "You are an expert Drupal 10 developer."
  • Use Chain of Thought to help guide it. By "holding its hand" and providing more detailed steps, the end result is more likely to work correctly.


Sample Chain of Thought Prompt for Creating a Drupal Module

  • You are an expert Drupal 10 developer. 
  • You will be writing a module called "awesome_csv_importer"
  • This module will provide a form where a CSV file can be uploaded. This CSV file will contain data for creating article nodes. Each row will contain a "title" and "body" field.
  • The author for each node should be user 1.
  • Before proceeding, think about Drupal best practices for this CSV import.
  • If there is an issue, an error should be output as a Drupal message.
  • For each file in this module, please note the file's path within the module.
  • Please generate fully completed code; do not use placeholder comments.
  • Please be sure to properly comment on your code, following Drupal code standards.
  • It is okay if your response stops abruptly. "I will say 'continue.'"

Here's a screenshot of the beginning of the gpt-4's response to this prompt:

ChatGPT prompt to create Drupal module


After the initial code is generated, there is often an iterative process as issues are identified by reviewing or running the code, and then the AI tool is asked to make corrections. If errors are encountered it is sufficient to just paste the error into a follow up message.

For more complex solutions, it may be more appropriate to generate sub-sections of the module via different conversations rather than using a single conversation to generate the whole module.


Limitations of AI for Drupal Development

  • Maximum input limitations. First, there are limits to the allowable size of the prompt. The specific size depends on the LLM model in use, but the limits are relatively large so should not be a concern for most tasks. It's important to note that when having a conversation with an LLM, each message sent is within the context of the entirety of the previous preceding dialog in the conversation. This means the AI can lose some context in a longer conversation since compression techniques must be used "behind the scenes" to avoid hitting the current LLM's max input limit. When this is the case, it can be appropriate to start a new conversation, with an explanation of what has been covered up to that point, along with any relevant code that it has been generated.
  • Scalability. When using LLM APIs such OpenAI's API, there are limitations on concurrent requests. Particularly for public-facing solutions, it may be necessary to implement queuing and caching mechanisms.
  • Hallucinations. Sometimes, LLMs appear to simply make things up. For example, I've had LLMs generate Drupal code using methods and hook that simply do not exist, but would be appropriate to use if they did exist. Often, if you point this out the AI will acknowledge its mistake an provide an alternative solution.
  • Bad judgment. Sometimes, LLM-generated code will work but may not be implemented in the best way. For example, I once asked for code to modify a Drupal view's results but the LLM tried to alter the view's SQL query, which is a less flexible, less maintainable solution than was needed. I've also seen instances where the LLM used pure PHP solutions rather than Drupal-specific solutions. It may also use solutions from older versions of Drupal that might work currently but will fail for future versions.
  • Security concerns. Another critical factor to keep in mind when using LLMs is to never submit sensitive information like passwords. This is because your input may be used to train future LLM models, meaning hackers could, with the right prompt, obtain an output containing this information. While LLM's like ChatGPT have been trained to behave lawfully, they can be tricked into straying from this by asking for "dis-allowed content" within the context of a fictional story.
  • Wrong answers. Sometimes, LLMs simply choose the wrong steps to follow, resulting in an incorrect result. Here's a simple example using math. (Incidentally, you'll notice this response is using a pirate persona. This is because I was experimenting with ChatGPT's custom instructions setting at the time.) In the below example, GPT gives the wrong answer to a simple math problem and then admits it doesn't know why it did that.

ChatGPT producing a math error


Prompting for the Big Picture: AI for Drupal Module Development

The above represents real world walk throughs and recent experiences for leveraging an LLM to develop a Drupal module. There’s much more. As another demonstration of the instant insights and information that  ChatGPT can provide, I recently entered a ChatGPT prompt that read: 

Can AI be leveraged to develop a module in Drupal?

Here's the answer that resulted from this prompt:

ChatGPT logo

Yes, AI can be leveraged to develop modules in Drupal. Drupal is an open-source content management system (CMS) that allows developers to create and customize websites. AI technologies can be integrated into Drupal modules to enhance various aspects of a website, such as personalization, content recommendation, natural language processing, and more.

Here are some ways AI can be incorporated into Drupal modules:

Content Personalization:

  • Use AI algorithms to analyze user behavior and preferences.

  • Develop a Drupal module that dynamically displays personalized content based on user preferences, improving user engagement.

Chatbots and Conversational Interfaces:

  • Integrate AI-powered chatbots or conversational interfaces within Drupal websites.

  • Create a module that allows users to interact with the website using natural language, providing a more user-friendly experience.

Search Optimization:

  • Implement AI-driven search algorithms to improve the accuracy and relevance of search results.

  • Develop a Drupal module that integrates advanced search capabilities powered by AI.

  • Image and Video Recognition:

  • Utilize AI for image and video recognition within Drupal.

  • Create modules that automatically tag and categorize media content based on AI analysis.

Data Analytics and Reporting:

  • Integrate AI for data analytics to extract meaningful insights from user interactions and website performance.

  • Develop Drupal modules that provide detailed analytics and reporting using AI algorithms.

Security Enhancements:

  • Implement AI-driven security measures to identify and prevent potential threats.

  • Develop modules that use AI to enhance website security by detecting and responding to malicious activities.

Automated Content Generation:

  • Use natural language processing (NLP) algorithms to automatically generate content.

  • Develop Drupal modules that assist in creating and optimizing content based on AI-generated suggestions.

When integrating AI into Drupal modules, it's important to consider factors such as data privacy, performance, and the specific requirements of the website. Additionally, leveraging existing AI libraries and frameworks can simplify the development process. Drupal's modular architecture makes it well-suited for incorporating AI functionalities into various aspects of a website.


What's Next for AI with Drupal?

As amazing and disruptive as today's LLMs are, they are only going to become faster and more capable as research continues.

It is hard to predict the rate at which LLM solutions will evolve because there may be unexpected bottleneck; it may be that availability of computational resources will be the main limiting factor for how smart these LLM can become.

However, it is safe to say that current and upcoming AI solutions will disrupt many aspects of business and technology, perhaps even more than the Internet did in the 1990s. In the same way that media companies evolved to leverage the Internet, AI will drive a similar evolution. 

While the future is not clearly mapped out, the time to embrace AI is now

Interested in architecting AI-powered solutions for your Drupal website? Promet is at the leading edge of exploring the potential and possibilities of AI for Drupal web development. Let’s talk!