/
SELECT.API: How to use LLM (AI) To Generate SEO Keywords

SELECT.API: How to use LLM (AI) To Generate SEO Keywords

This example shows how to utilize Context Actions, a dynamic EXECUTE.ACTION.LOOP and the SELECT.API command to ask a Large Language Model (LLM) for a marketing description of a product based on the product information you have

The example here uses Open AI, but any other language model with an API service available could be used if the Action is modified accordingly.

Download the Action example here:

This example is provided as inspiration only. It must be adapted to your Perfion environment to work. Perfion Support does not assist with such adaptations. Please contact your Perfion Partner or your Perfion consultant if you need assistance.

Introduction

AI language models have a wide range of usages in relation to product information. You could use it to automatically generate descriptions of a product to a specific audience. It could also be used to automatically group products into meaningful categories for a webshop.

For this example, we will see how it can be used to generate search engine optimization keywords for products based on the information on the products we have in Perfion for that item.

This example is composed of two Actions: one that is added to the Context of Products which passes the information to the second Action via the EXECUTE.ACTION.LOOP command.

The second Action queries the language model through an SELECT.API and then returns the answer to the first Action, which then imports the joined result.

0. Prerequisites

In order to get the Actions working you need the following in your Perfion installation:

  1. A Remote connection Open AI with the API URI and bearer token, see Open AIs own documentation and the Remote Connection section for more information on this.

  2. Two specific features:

    1. Text type feature named Prompt

    2. String type feature SEOKeywords created as Allow Multiple

Additional changes might be needed to adapt the Action to your specific installation.

1. Context Action For Products

From Perfion 5.2 and onwards, it is possible to make Actions available from the Grid's toolbar. This allows you to execute Action on specific Items.

Make the Action available

To make an Action available in the Context of Products, add “Product” to the list of Context on the Action, on the right-hand side of the Action Section

image-20240314-114043.png
The AI - Generate SEO Words is the first Action. It can be executed on Products and passes the item IDs of those products to the second Action.

This will make the Action available in the Product Section

image-20240314-114848.png
The Action can be execute via the toolbar button or through the right-click menu

The Action

The first Action receives the Items data source containing the ItemIDs of the chosen Products and then passes it to an EXECUTE.ACTION.LOOP. This makes sure that each of the chosen products will be queried against Open AI one at a time.

In the loop, the Open AI API will be queried and then the result will be passed and joined to a global data source named $EnrichedProducts. This data source is then imported.

2. Action to Query Remote API

This Action receives input from the EXECUTE.ACTION.LOOP, which will be the Perfion Item ID of one Product at a time.

This Action needs to be heavily modified depending on the LLM service you wish to use, the input you wish to provide to the model and what the expected result of the query should be.

The first part of the Action takes the input row from the EXECUTE.ACTION.LOOP and stores the ItemID in the global variable $ItemID. This is used later to query the product from Perfion itself.

The next SET Command defines all the variables needed to query the Open AI API. This will differ greatly depending on the service you wish to use.

The $prompt is the format we will use to query Open AI for SEO Keywords. Note that inside the prompt we use another global variable $ProductPrompt. We will build this later.

The Open API expects a body in the JSON POST call in the format defined in $body. Please refer to the Open AI API documentation for more details.

In the SELECT.PERFION we query the Product, finding the Features we wish to use in our prompt to Open AI by using the $ItemID we defined earlier. This will be the one exact Product we’re working on right now.

In the next SELECT, we build the $ProductPrompt variable. It is important to reset the $ProductPrompt to make sure it is defined anew each time the loop runs. In this example, we use the Brand, ItemName and the material of the Product as input for Open AI to generate SEO Keywords.

In the EXECUTE.SCRIPT, we prepare the JSON body, inserting replacing the placeholders in the $body variable the variables we defined. We also replace the $ProductPrompt placeholder in the $prompt variable.

With the body json prepared, we use SELECT.API to query the Open AI service to execute a POST call to the service with our body, using the remote connection named Open AI. The response from the service is stored in the Result data source.

Then a SELECT.JSON is used on the Result data source to unwrap it.

The unwrapped JSON needs some formatting which is done in the SELECT afterwards. Here the Result is also formatted for importing into Perfion.

For optimization, the result is not imported yet but joined to a global data source $EnrichedProducts. This data source is imported in the first Action, after the EXECUTE.ACTION.LOOP has completed.

 

3. Result

When the Actions have finished running, you will be able to see the resulting SEO keywords on the products and see what the prompt used to query Open AI was.