Build an AEM Content Fragment Process with ChatGPT

Using ChatGPT to process Content Fragments through Workflows

Victor Serrato
Globant
12 min readOct 24, 2023

--

Image generated by DALL·E for “Puzzle pieces of a letter by an impressionist painter”

Before, we explained how ChatGPT technology can help AEM authors complete ideas. Today, we use OSGi components to construct our Content Fragment Process. This process uses Workflows and ChatGPT to summarize and translate Content Fragments.

The article has five parts. In the first part, we will illustrate how to request Open AI endpoints using the Postman platform. In the second, we will introduce Content Fragment and Workflow technologies. In the third, we will create several OSGi components. In the fourth, we will create and configure a Workflow using AEM authoring tools. Finally, we will use the Workflow to test our code.

Chat Completions endpoint

Chat Completions endpoint is compatible with GPT chat models. According to the OpenAI documentation:

Chat models take a list of messages as input and return a model-generated message as output. Although the chat format is designed to make multi-turn conversations easy, it’s just as useful for single-turn tasks without any conversation.

This endpoint supports the parameters listed below. These definitions were taken from the OpenAI documentation as well:

  • model: ID of the model to use. On the OpenAI API, not all models are available on all endpoints. You should review the model endpoint compatibility before setting this parameter.
  • max_tokens: The maximum number of tokens to generate in the chat completion.
  • temperature: Sampling temperature to use. This parameter can vary between 0 and 2. Higher values like 0.8 will make the output more random. Lower values like 0.2 will make it more focused and deterministic.

Additionally, we defined a new parameter named content. This parameter represents the messages sent to ChatGPT.

Get an OpenAI API key

Requests to OpenAI endpoints must be authorized through the Bearer Authorization mechanism. OpenAI users can create, edit, or delete keys through the API keys page. For more details about API keys, see the Authentication section on the API Reference page.

Use Postman to request the Chat Completions endpoint

You can use the Postman API platform to configure, tune, and test the requests to the Chat Completions endpoint.

Postman requests can access the OpenAI API key using variables. In our case, the OPENAI_API_KEY global variable. To authenticate the requests, the Authorization HTTP header’s value must be set to Bearer {{OPENAI_API_KEY}}.

To test the Chat Completions endpoint:

  1. Create a POST Postman request to https://api.openai.com/v1/chat/completions.
  2. In the Body tab, input the following text:

The previous variables can be set using the Pre-request Script tab. For example, the following configuration translates some HTML content to Spanish:

Key AEM Technologies

Let’s introduce a couple of AEM technologies.

  • Content Fragments enable AEM authors to design and publish page-independent pieces of content. These pieces of content are ready for use on many channels using the JSON format. Because of that, Content Fragments are ideal for headless delivery.
  • A Content Fragment Model predefines the structure of a Content Fragment. AEM authors edit Content Fragments for use on specific channels through Variations.

In our purpose, we defined the FAQ Content Fragment Model to represent FAQs. But, the Content Fragment Process can also work with other Content Fragment Models.

The FAQ Content Fragment Model has the following properties:

  • question: Textfield for the question.
  • answer: Rich text for the answer to the question.
  • summary: Rich text for the summary of the question.

AEM authors set the question and answer properties during the content creation process. The summary property is set by the Content Fragment Process during Workflow executions.

Also, the Content Fragment Process uses Variations to store translations. For example, the spanish Variation contains the Spanish translation.

Workflows

Workflows allow AEM authors to automate processes and publish content in AEM environments. Workflows are composed of a series of steps. AEM provides some steps out of the box and an API to create custom steps.

Articles about using and configuring Workflows on AEM are available here and here.

Create the Content Fragment Process

Let’s see how to create our Content Fragment Process.

Create model classes

Chat Completions endpoint’s request and response bodies contain JSON documents. We use the Jackson library to convert these JSON documents into POJOs (Plain Old Java Objects).

Our POJO classes only contain the fields required by our Content Fragment Process. You can review the OpenAI-Java library’s code for a more complete mapping.

Now, we will describe the POJO classes utilized in our code. The first class is called ChatMessage. This class represents the messages used on the OpenAI requests and responses. A message contains a role and a content:

The ChatCompletionRequest class represents a request to the Chat Completions endpoint. This class contains some configuration parameters, such as model, maxTokens, and temperature. Additionally, it contains a list of messages:

Responses from the Chat Completions endpoint can contain many choices. The ChatChoice class represents these choices. This class contains a message with the answer provided by ChatGPT:

Finally, the ChatCompletionResponse class represents the Chat Completions endpoint’s responses. For our purposes, we only used the choices list provided by the endpoint:

Create an OSGi service

We created an OSGi service to translate and summarize Content Fragments. The ContentFragmentService interface defines the operations provided by the OSGi service:

The ContentFragmentService interface defines the following methods:

  • summarize summarizes the content of a set of properties. The Content Fragment is available at fragmentPath. The property defined by the summary parameter stores the ChatGPT response.
  • translate translates the content of a set of properties to a specific language. The Content Fragment is available at fragmentPath. The Variation defined by the language parameter stores the ChatGPT response.

The ContentFragmentServiceImpl class implements the ContentFragmentService interface:

The ContentFragmentServiceImpl class:

  • Declares an OSGi service (@Component annotation) that implements the ContentFragmentService interface (line 3).
  • Declares a configuration class (@Designate annotation) for the ContentFragmentServiceImpl class (line 6).
  • Defines an inner class (@ObjectClassDefinition annotation) with configurable properties (lines 9 to 11).
  • Injects an instance (@Reference annotation) of the ResourceResolverFactory interface (lines 13 and 14).
  • Declares a method (@Activate annotation) to be executed in the activation phase (line 16).
  • Implements the execute method. This method sends a request to the Chat Completions endpoint. Then, processes the endpoint’s response and returns the ChatGPT’s answer (line 20).
  • Implements the summarize method (line 23).
  • Implements the translate method (line 27).

Let’s explore in more detail the structure defined before.

Instances of the ContentFragmentServiceImpl class can be configured through the Config inner class. This inner class defines attributes and their default values. These attributes are assigned to the component’s variables in the activate method:

The execute method requests and retrieves information from the Chat Completions endpoint. This method utilizes the Apache HttpComponents library to communicate with the endpoint:

The summarize method uses the executemethod to ask ChatGPT to sum up content:

The implementation of the summarize method:

  • Adapts the resource defined on the fragmentPath to a Content Fragment (lines 7 to 14).
  • Concatenates the content of the selected properties (lines 22 to 32).
  • Requests ChatGPT to summarize the content (line 39).
  • Stores ChatGPT answer on the element identified by the summary parameter (line 40).

The translate method uses the execute method to ask ChatGPT to translate the content:

The implementation of the translate method:

  • Adapts the resource defined on the fragmentPath to a Content Fragment (lines 7 to 14).
  • Ensures the Content Fragment has a variation that represents the language (lines 15 to 23).
  • Requests ChatGPT to translate every property defined in the properties parameter (line 33).
  • Stores the content translated by ChatGPT in the variation (line 35).

A complete implementation of the ContentFragmentServiceImpl class is available here.

Create a Custom Workflow Process

The OSGi service created in the previous section can be used on other OSGi components. In our case, the ContentFragmentProcessor class. This class implements the WorkflowProcess interface:

Let’s explore the code of the ContentFragmentProcessor class:

  • This class is defined as an OSGi component through the @Component annotation (lines 19 to 21).
  • Requests the OSGi framework to inject an instance of the ContentFragmentService class (lines 28 and 29).
  • Implements the execute method (lines 29 to 61).

The execute method is defined in the WorkflowProcess interface. In this method the ContentFragmentProcessor class:

  • Identifies the path of the Content Fragment to be processed (line 32).
  • Determines the method to be executed and its parameters (lines 33 to 50).
  • Processes the Content Fragment using the ContentFragmentService instance (lines 52 to 60).

Create a Workflow Model

We are ready to create a new Workflow Model to process FAQ Content Fragments using ChatGPT. You can follow the steps below to create and configure a new Workflow Model.

Create the Workflows Model:

  1. Navigate to the Workflows Models Console.
  2. Click on the Create button.
  3. Click on the Create Model menu option.
  4. On the Add Workflow Model dialog, input ChatGPT FAQ Processor on the Title field.
  5. Click on the Done button.

Now, you can select the ChatGPT FAQ Processor on the Workflow Models Console (see image below).

ChatGPT FAQ processor on the Workflow Models Console.

Edit the Workflow Model:

  1. Select the ChatGPT FAQ Processor card.
  2. Click on the Edit button.
  3. On the Workflow Model editor, remove the Step 1.

Insert a Process Step component to summarize the content. This component has the following configuration:

  • Common/Title: Summarize Content
  • Process/Process: Content Fragment Process
  • Process/Handler Advance: True
  • Arguments: summarize,question,answer,summary

Insert a Process Step component to translate the content to Spanish. This component has the next configuration:

  • Common/Title: Translate to Spanish
  • Process/Process: Content Fragment Process
  • Process/Handler Advance: True
  • Arguments: translate,question,answer,summary,spanish

Insert a Process Step component to translate the content to French. This component has the following configuration:

  • Common/Title: Translate to French
  • Process/Process: Content Fragment Process
  • Process/Handler Advance: True
  • Arguments: translate,question,answer,summary,french

After you define your Workflow Model, it should look like this:

ChatGPT FAQ Processor’s structure.

Finally, you should synchronize your Workflow Model by clicking on the Sync button.

Process a Content Fragment

To use the ChatGPT FAQ Processor to process a FAQ Content Fragment:

  1. Navigate to the Workflows Models Console.
  2. Select the ChatGPT Content Fragment Processor card.
  3. Click on the Start Workflow button.
  4. On the Run Workflow dialog, set the path to the Content Fragment to be processed.
  5. Click on the Run button.

Test the Content Fragment Process

In this section, we will use a FAQ Content Fragment to represent the What is Java technology and why do I need it? FAQ, and to show how the Content Fragment Process works on it. This is the initial FAQ Content Fragment:

Question: What is Java technology and why do I need it?

Answer: Java is a programming language and computing platform first released by Sun Microsystems in 1995. It has evolved from humble beginnings to power a large share of today’s digital world, by providing the reliable platform upon which many services and applications are built. New, innovative products and digital services designed for the future continue to rely on Java, as well.

While most modern Java applications combine the Java runtime and application together, there are still many applications and even some websites that will not function unless you have a desktop Java installed. Java.com, this website, is intended for consumers who may still require Java for their desktop applications — specifically applications targeting Java 8. Developers as well as users that would like to learn Java programming should visit the dev.java website instead and business users should visit oracle.com/java for more information.

Is Java free to download?

Yes, Java is free to download for personal use.

Java is also free for development: developers can find all the development kits and other useful tools at https://www.oracle.com/javadownload/.

Why should I upgrade to the latest Java patch each quarter when prompted?

The latest Java patches contain important enhancements to improve performance, stability and security of the Java applications that run on your machine. Installing these updates will ensure that your Java applications continue to run with the most up-to-date version.

In the first step, ChatGPT summarized the previous Content Fragment:

Summary: Java technology is a programming language and computing platform that was first released in 1995. It is widely used in today’s digital world to build reliable platforms for various services and applications. Many new and innovative products and digital services still rely on Java. While most modern Java applications combine the Java runtime and application together, there are still some applications and websites that require a desktop Java installation. Java is free to download for personal use and for development purposes. It is recommended to upgrade to the latest Java patch each quarter as it contains important enhancements for performance, stability, and security of Java applications.

In the second step, ChatGPT translated the Content Fragment into Spanish:

Question: ¿Qué es la tecnología Java y por qué la necesito?

Answer: Java es un lenguaje de programación y plataforma informática lanzado por primera vez por Sun Microsystems en 1995. Ha evolucionado desde sus humildes comienzos para alimentar una gran parte del mundo digital actual, al proporcionar la plataforma confiable sobre la cual se construyen muchos servicios y aplicaciones. Nuevos productos innovadores y servicios digitales diseñados para el futuro continúan confiando en Java también. Si bien la mayoría de las aplicaciones modernas de Java combinan el tiempo de ejecución de Java y la aplicación juntos, todavía hay muchas aplicaciones e incluso algunos sitios web que no funcionarán a menos que tenga instalado Java de escritorio. Java.com, este sitio web, está destinado a consumidores que aún pueden necesitar Java para sus aplicaciones de escritorio, específicamente aplicaciones dirigidas a Java 8. Los desarrolladores, así como los usuarios que deseen aprender programación en Java, deben visitar el sitio web dev.java en su lugar y los usuarios empresariales deben visitar oracle.com/java para obtener más información. ¿Java es gratuito para descargar? Sí, Java es gratuito para descargar para uso personal. Java también es gratuito para el desarrollo: los desarrolladores pueden encontrar todos los kits de desarrollo y otras herramientas útiles en https://www.oracle.com/javadownload/. ¿Por qué debería actualizar al último parche de Java cada trimestre cuando se me solicite? Los últimos parches de Java contienen mejoras importantes para mejorar el rendimiento, la estabilidad y la seguridad de las aplicaciones de Java que se ejecutan en su máquina. Instalar estas actualizaciones garantizará que sus aplicaciones de Java sigan funcionando con la versión más actualizada.

Summary: La tecnología Java es un lenguaje de programación y plataforma informática que fue lanzado por primera vez en 1995. Se utiliza ampliamente en el mundo digital actual para construir plataformas confiables para diversos servicios y aplicaciones. Muchos productos y servicios digitales nuevos e innovadores todavía dependen de Java. Si bien la mayoría de las aplicaciones Java modernas combinan el tiempo de ejecución de Java y la aplicación juntos, todavía hay algunas aplicaciones y sitios web que requieren una instalación de Java de escritorio. Java se puede descargar de forma gratuita para uso personal y con fines de desarrollo. Se recomienda actualizar al último parche de Java cada trimestre, ya que contiene mejoras importantes para el rendimiento, la estabilidad y la seguridad de las aplicaciones Java.

Finally, in the third step, ChatGPT translated the Content Fragment into French:

Question: Qu’est-ce que la technologie Java et pourquoi en ai-je besoin ?

Answer: Java est un langage de programmation et une plateforme informatique d’abord publiés par Sun Microsystems en 1995. Il a évolué depuis des débuts modestes pour alimenter une grande part du monde numérique d’aujourd’hui, en fournissant la plateforme fiable sur laquelle de nombreux services et applications sont construits. De nouveaux produits innovants et des services numériques conçus pour l’avenir continuent également de s’appuyer sur Java.

Alors que la plupart des applications Java modernes combinent le runtime Java et l’application ensemble, il existe encore de nombreuses applications et même certains sites Web qui ne fonctionneront pas à moins que vous n’ayez une version de bureau de Java installée. Java.com, ce site Web, est destiné aux consommateurs qui peuvent encore avoir besoin de Java pour leurs applications de bureau, en particulier les applications ciblant Java 8. Les développeurs ainsi que les utilisateurs qui souhaitent apprendre la programmation Java devraient plutôt visiter le site dev.java et les utilisateurs professionnels devraient visiter oracle.com/java pour plus d’informations.

Java est-il gratuit à télécharger ?

Oui, Java est gratuit à télécharger pour un usage personnel.

Java est également gratuit pour le développement : les développeurs peuvent trouver tous les kits de développement et autres outils utiles sur https://www.oracle.com/javadownload/.

Pourquoi devrais-je mettre à jour le dernier correctif Java chaque trimestre lorsque cela est demandé ?

Les derniers correctifs Java contiennent des améliorations importantes pour améliorer les performances, la stabilité et la sécurité des applications Java qui s’exécutent sur votre machine. L’installation de ces mises à jour garantira que vos applications Java continuent de s’exécuter avec la version la plus à jour.

Summary: La technologie Java est un langage de programmation et une plateforme informatique qui a été lancée pour la première fois en 1995. Elle est largement utilisée dans le monde numérique d’aujourd’hui pour construire des plateformes fiables pour divers services et applications. De nombreux produits et services numériques nouveaux et innovants dépendent encore de Java. Bien que la plupart des applications Java modernes combinent l’exécution de Java et l’application ensemble, il existe encore des applications et des sites web qui nécessitent une installation de Java sur ordinateur de bureau. Java est gratuit à télécharger pour un usage personnel et à des fins de développement. Il est recommandé de mettre à jour le dernier correctif Java chaque trimestre car il contient des améliorations importantes pour les performances, la stabilité et la sécurité des applications Java.

Conclusions

This article describes how OSGi components can use ChatGPT technology to automate authoring tasks. For our purposes, we used the Chat Completions endpoint that is part of the OpenAI’s API.

We used the Apache HttpComponents library to communicate with the Chat Completions endpoint. Yet, it is also possible to use other libraries like the OpenAI-Java library to achieve the same goal.

Besides, we described the construction of OSGi components to summarize and translate content. These components can be extended to perform other text-processing operations.

Finally, we found that ChatGPT responses can vary depending on the language. For example, in the Spanish translations, the HTML markup was removed. But, in the French translations, the HTML tags were processed as expected.

--

--