Can I Use This?

This feature is available since Webiny v5.41.0.

What you will learn
  • how to integrate OpenAI with Webiny
  • how to generate SEO titles, descriptions, and tags for your articles with OpenAI

Overview
anchor

Webiny is an AI model-agnostic platform. You can integrate it with different models and craft tailored AI-experiences. You control which model and what AI-powered capabilities your users and content editors can use.

In this tutorial, we will demonstrate how to integrate OpenAI with Webiny to build a Smart SEO tool for Headless CMS. This tool will automatically generate SEO titles and tags for your articles.

You’ll create the content for your blog or article, and based on this content, OpenAI will provide SEO titles and tags to enhance your article’s search visibility.

Getting Started
anchor

Run the following command to set up this extension in your project.

The Smart SEO extension code can be found in our webiny-examplesexternal link repository.

Set OpenAI API Key in Environment Variables
anchor

Open the .env file in the root of your project and add the OpenAI API key to the WEBINY_ADMIN_OPEN_AI_API_KEY environment variable as shown below:

For detailed instructions on creating an OpenAI API key, refer to their official documentationexternal link.

Run the Project
anchor

After downloading the extension to your project, run the webiny watch command to see the Smart SEO functionality in action.

All Set!
anchor

You’re now ready to generate SEO titles and tags for your articles using OpenAI. Simply navigate to the Article - Smart SEO Model, create a new article, and click the “AI-Optimized SEO” button to generate SEO titles and tags effortlessly.

Code Walkthrough
anchor

Let’s walk through the code to understand how the Smart SEO extension operates. We’ve created a new extension called smart-seo-open-ai, and we’ll review each plugin used in this extension one by one.

Field Tracker, ContentEntry Form Decorator
anchor

In this example, we have an Article - Smart SEO Model with four fields: Content, SEO - Title, SEO - Description, and SEO - Meta Tags. We will track these fields. Once the Content field is populated, we will send it to OpenAI, which will generate SEO-friendly titles and tags for the article. The generated title and tags will then be set in the respective Title, Description and Tags fields of the model.

The concept is straightforward: we aim to track fields of interest, such as text inputs or rich text fields. The challenge lies in not knowing the exact location of these fields. They could be nested within objects, repeatable objects, or dynamic zone templates, making their placement unpredictable. Hardcoding paths to these fields is not a feasible solution. So, we will create the Field Trackerexternal link.

Field Trackerexternal link is a React context that tracks content model fields of interest. It enables you to extract data from these fields that can be send to OpenAI and update the fields with the response received.

Here is the code for the Field Trackerexternal link, here we decorated the ContentEntryFormexternal link with a FieldTracker component.

We’ve also decorated the useBind hookexternal link to integrate field tracking specifically for models. In this example, we are tracking rich-text fields and SEO-specific fields (seoTitle, seoDescription, seoMetaTags) in the Article - Smart SEO model. This allows us to trigger dynamic updates and facilitate interactions with external services.

You can customize this ContentEntryFormBind decorator fileexternal link to track fields from other models or different field types, tailoring it to your specific use case.

Extract HTML From RichTextHtml (Lexical Editor)
anchor

Webiny leverages the Lexical text editorexternal link for Rich Text Fields. However, for generating SEO-related information using OpenAI, we will send the HTML content (not the Lexical format). To achieve this, we need to extract the HTML content from the Rich Text field. For this purpose, we have created a extractFromRichText transformerexternal link to handle the conversion.

Smart SEO
anchor

This is the primary plugin (SmartSeo.tsx)external link that integrates Webiny with OpenAI. It sends the HTML content to OpenAI and retrieves SEO-related information. The received data is then used to update the corresponding fields in the model.

If you’ve noticed the AI-Optimized SEO button on the Content Entry form, it has been configured in this plugin using the ContentEntryEditorConfigexternal link.

We display the AI-OPTIMIZED SEO button on the Content Entry form for the content model with the ID article. You can easily customize it to include your content model IDsexternal link to display this button on the respective content model entry forms.

AI-optimized SEO ButtonAI-optimized SEO Button
(click to enlarge)

Article Content Model
anchor

In this example, we created an Article - Smart SEO Content Model with the ID article-smart-seo to showcase AI capabilities.This content model is generated programmatically, and you can find the corresponding Content Model code hereexternal link.

Please note that the article-smart-seo content ID is referenced in the code. Refer to the sections above for guidance on customizing the code to align with your content model.

In your real project, you can remove this content model by either deleting the corresponding api directoryexternal link or commenting out the Article import in the index fileexternal link.

For your reference, we created an Article - Smart SEO content model with the following fields to manage and store articles:

FieldField TypeField ID
ContentRich Textcontent
SEO - TitleLong textseoTitle
SEO - DescriptionLong textseoDescription
SEO - Meta TagsObject (with Tag Name and Tag Value Text Field)seoMetaTags
ArticleArticle
(click to enlarge)