Wednesday, April 5, 2023

How to Connect Adobe Experience Manager (AEM) with ChatGPT

 ChatGPT is an AI language model developed by OpenAI, capable of generating human-like text based on input. The model is trained on a large corpus of text data and can generate responses to questions, summarize long texts, write stories, and much more.

The OpenAI API can integrate AI models with different systems.

AEM already enables some of the Generative AI capabilities like — Content Creation, Summarization, rewriting, etc., also Adobe Integrate Adobe Firefly (a range of creative AI models to AEM DAM) to simplify content creation and management. I will explain this in more detail in another post.

Integrating AEM with ChatGPT empowers content authors to easily create content that meets the SEO requirements, e.g., create a title/description for a page, create a story on a topic, summarize the content, etc.; the ChatGPT generated content can be optimized further based on the need.

In this post, let us see how to integrate AEM with ChatGPT to empower the authors with content authoring.

As a first step, enable a servlet connecting the chatgpt model with the user prompt and returning the response.

ChatServlet.java

Change the gpt model based on your need, gpt-3.5-turbo is the latest model currently available for API access; the model details can be found Models — OpenAI API

The ‘role’ can take one of three values: ‘system’, ‘user’ or the ‘assistant’.

These roles help define the context of the conversation; even you can specify the previous conversation context by adding messages with different roles, user — the message sent by the user, and assistant — the response from API.

Not mandatory to specify all the roles; I have used only the user role to generate the response; you can use other roles to give more context to the conversation.

ChatGptRequest.java

max_tokens — specifies the maximum number of tokens the API should generate in response to a prompt. Tokens are the individual words and punctuation marks that make up the text. Adjust the max_tokens based on your prompts and the expected response.

ChatGptResponse.java

Message.java

Now Generate the OpenAI API Key through Account API Keys — OpenAI API

Replace <Open API Token> in the servlet with the actual token; ensure the token is secured, and you can keep the token as an OSGI configuration.

The API is billed based on the token usage; some free tokens are enabled for initial API testing; after that, you should enable the billing details to use the API.

Now the servlet can be invoked as http://localhost:4502/bin/chat.json?prompt=<<Prompt>> e.g., http://localhost:4502/bin/chat.json?prompt=what is AEM

The servlet can be invoked from components, pages, or toolbars to generate the required content.

In this post lets us see how to add a button on the title component edit toolbar to integrate with the above servlet to generate the content whenever required — the condition on the action can be changed to enable the chatgpt action on all the components or specific components (also the servlet can be invoked in multiple other ways).

Create a client library under your project with the below JS and add cq.authoring.editor.sites.page.hook for the categories.

ToolbarAction.CHATGPT.js

js.txt

Now the Title component edit toolbar displays the chatgpt actions.

Clicking on the action will display the dialog to generate the required content through the chatgpt integration.

Enter the prompt for generating the content and Click on Generate button.

Now the authors can use the chatgpt integration to perform the different content operations through ChatGPT —Generate the content, Summarize, generate a title, generate a description, etc.; modify the content based on the business context.

AEM already enables some of the Generative AI capabilities like — Content Creation, Summarization, rewriting, etc., also Adobe Integrate Adobe Firefly (a range of creative AI models to AEM DAM) to simplify content creation and management. I will explain this in more detail in another post.

AEM Project Reference— youttubedata/aem/chatgptintegration at master · techforum-repo/youttubedata (github.com)



Thursday, March 30, 2023

Integrate ChatGPT with Visual Studio for Faster Development

The development landscape constantly evolves, and new technologies are emerging daily. The AI models are changing the way developer's day to day work.

The ChatGPT, a language model powered by OpenAI, ChatGPT can assist developers in their day-to-day work. One of the most significant ways ChatGPT can help is by serving as a pair programmer.

As a pair programmer, ChatGPT can provide developers with valuable suggestions and insights that can make the process of programming more efficient and effective. For example, ChatGPT can suggest coding techniques, syntax, and algorithms help developers write more efficient and concise code. Additionally, it can assist in debugging code by analyzing the code and providing feedback on potential errors or bugs.

The Genie AI ChatGPT extension for Visual Studio Editor — ChatGPT — Genie AI — Visual Studio Marketplace( multiple extensions are available, but I felt this one looked good) enables the pair programming capabilities to the developers. This extension supports different models, e.g., GPT-4, GPT3.5, GPT3, etc., that the developers can use to simplify their day-to-day development works.

You should be able to optimize the code, Add Tests, Find Bugs, Explain, Add Comments, and Complete Code operations.

Install the extension — ChatGPT — Genie AI — Visual Studio Marketplace

Configure the extension — e.g., Enable Conversation History, Configure the model — reference list of OpenAI models from Models — OpenAI API

I used one of the existing Java files to test it; requested to optimize the selected code.

You should add the API Key to integrate with the ChatGPT API; executing the first-time operation will prompt the OpenAI API key.

Generate the OpenAI API key from https://platform.openai.com/account/api-keys

Add the API key and click enter.

The ChatgPT has given the optimized code block; you can copy the optimized code or even replace the existing code block with the recommended one.

These features look great; this will reduce the code review effort and helps to build the code aligned with the latest improvements.

The AI tools help the developer stop focusing on language syntax; they can focus more on building the optimal logic to solve their business problems (the AI even helps to create the business logic but some level)

We should utilize the available tools and technologies to simplify our day-to-day job; at the same time, we can spend time learning new technology trends.

Additional details using ChatGPT to generate AEM code.

#Question1 — “Create a sling servlet to export AEM tag details

The servlet generated with SCR annotations.

@SlingServlet(
paths = "/bin/exporttags",
methods = "GET",
extensions = {"csv", "json"}
)

public class ExportTagsServlet extends SlingAllMethodsServlet {

#Question2 — “Please use OSGI 6 annotations

This time the same servlet with OSGI 6 annotations; even we can request for OSGI 7 annotations.

@Component(
service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=Export Tag Details Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/exporttags",
"sling.servlet.extensions={\"csv\", \"json\"}"
}
)
public class ExportTagsServlet extends SlingSafeMethodsServlet {

No more writing the code from scratch and searching through search engines to find the optimal code, but the AI models can help to write the optimized code quickly. I look forward to learning how these AI models will affect the future development landscape.

Watch out for sharing sensitive information with AI engines; we should use AI models responsibly.