How to Build a Product Description Generator with LLMs

How to Build a Product Description Generator with LLMs

TL;DR:

  • This tutorial teaches you how to use large language models (LLMs) to generate product descriptions for ads quickly and at scale.

This article's title and TL;DR have been generated with Cohere.
Get started with text generation.

Marketing and advertising are crucial to getting your products in front of your target audience. But it’s not enough to call attention to your products through an ad. You also have to describe their composition, features, benefits, and so on if you want to:

  • Improve visibility in search engines and social media platforms
  • Generate better leads and boost conversion rates
  • Connect with your target audience and build an excellent brand reputation

Creating new content for each product description can be time-consuming and repetitive. So, instead of writing these descriptions manually, you can use natural language processing (NLP) to do them automatically. Using NLP helps you keep up with changing trends, quickly generate content for new and updated products, and easily manage large product catalogs.

This tutorial teaches you how to use large language models (LLMs) to generate advertising content quickly, specifically product descriptions. Using the artificial intelligence (AI) capabilities of the Cohere Platform and its Generate endpoint, you can create a simple web application that uses keywords in a short phrase to generate multiple product descriptions and automate this repetitive copywriting task.  

How to Interpret NLP Models

The Cohere Platform provides access to Cohere’s LLMs through an easy-to-use API and endpoints like Cohere Generate. Cohere’s LLMs have been trained using extensive text-based data from a wide variety of sources. When given a prompt or a phrase as input, the model can use what it has learned to paraphrase or create new content.

To build our product description generator using Cohere Generate, we can take either the prompt engineering or the fine-tuning approach. Prompt engineering involves writing inputs in a way that achieves the best generations, while fine-tuning consists of creating a custom model using your dataset, which makes the generated text more adaptable for your use case. In this tutorial, we’ll use the prompt engineering approach.

Check out the final project code on Github.

Prerequisites

To follow this tutorial, ensure you have the following:

  • Node.js v14.x or above installed on your machine. Refer to the Node.js documentation for instructions on how to do so by using a package manager.
  • Angular CLI v14 or above installed. You’ll use Angular to create the web app. To install the CLI, run npm install -g @angular/cli@14.1.3 in your terminal.
  • A Cohere account. Start by logging in or creating an account if you don’t already have one.
  • A Cohere API key. The next section explains how to generate your API key.

Generating Your API Key

On the left menu, click API Keys and create a trial API key. You use this key to gain access to the Cohere API and Generate endpoint. It’s important to note that trial keys are free, but they’re rate-limited, meaning it’s not advisable to use them in production applications.

Creating Your Web Application

To get started, open a terminal window and navigate to your working directory using the command below.

cd /path/to/directory

Create a new Angular application by running the following command to bootstrap your web application.

ng new ad-generator —style=scss

Next, install these package dependencies:

  • process
  • stream-http
  • Url
  • https-browserify

The cohere-ai package needs them to work in a browser.

Enter the commands below in your terminal window.

cd ad-generator/
npm i -s cohere-ai process stream-http url https-browserify

Then, add some configurations to make the cohere-ai package work using the Angular framework. Open the tsconfig.json file. Add the following "paths" code block to the existing "compilerOptions" code block. This addition allows the Angular compiler to resolve packages needed by the cohere-ai package.

{
    “compilerOptions”: {
 …
        "paths": {
            "https": ["node_modules/https-browserify"],
            "url": ["node_modules/url"],
            "http": ["node_modules/stream-http"]
        }
    }
}

Finally, open the src/polyfill.ts file and add the following code block at the end of the file. This code attaches the required global variables to the windows object.

(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
global.process = require('process');

Integrating Cohere Generate

Now, you must integrate Cohere’s Generate endpoint. Cohere’s Node.js SDK supports TypeScript, making it easy to integrate with Angular.

Open the src/app/app.component.ts file and import the SDK at the top of the file.

const cohere = require('cohere-ai');

Next, delete the code inside the AppComponent class. Add a constructor function and initialize Cohere with the previously created API key.

constructor(){
    cohere.init('<API-KEY>');
} 	

In the AppComponent class, add the code snippet below. You’ll add a class property called generated to hold all the generated product descriptions for ads and another called prompt, which saves the product description entered by the user.

generated: string[] = [];
prompt: string = '';

Create a method for the AppComponent class called generateCopy. This method takes your product name and keywords as input and passes them to Cohere’s Generate endpoint to get the generated product descriptions.

async generateCopy(){
    const response = await cohere.generate({
      model: "large",
      prompt: this.prompt,
      max_tokens: 100,
      temperature: 1,
    });
    // concatenate the prompt and the generated text to
    this.generated.push(this.prompt + response.body.generations[0].text);
}

The Generate endpoint has parameters that allow you to shape the inputs provided and generate conditioned text results. These parameters include:

  • Model—This indicates the type of model you want to use. Options include small, medium, large, and xlarge. The larger the model, the higher the accuracy. However, this slows down the result.
  • prompt—This is the input you want for generating product descriptions.
  • temperature—This is a value between 0 and 1. The higher the value, the higher the level of randomness in the text generated. A value of 1 produces different outputs for the same input.
  • max_tokens—This represents the maximum number of tokens (or words) generated per output. This parameter dictates the length of your product descriptions.Num_generations—This is the number of output text generated by a single API call. It has a maximum of five generated outputs.

Check out the documentation for more information about configuring Cohere Generate.

Implementing the User Interface

In this section, you’ll develop the user interface with a form to take in the product name and keywords in the form of a prompt. The user interface displays the generated product descriptions in a list.

First, install bootstrap, the package you use for styling. Open your terminal, navigate to the root directory of your web application, and run the following command.

npm i bootstrap@5.2.2 -s

Make sure you have bootstrap’s styles loaded by opening the src/style.css file and adding the following import.

@import '../node_modules/bootstrap/dist/css/bootstrap.min.css';

For forms to work in the web application, open the src/app/app.module.ts and import FormsModule by adding the following code at the top of the file.

import { FormsModule } from '@angular/forms';

Next, add FormsModule in the imports array of the @NgModule decorator, as shown below.

@NgModule({
  …
  imports: [  BrowserModule, FormsModule ],
  …
})

Now, use the src/app/app.component.html file to write the markup code for your interface. Start by deleting all the code present in this file. Then create a form, two buttons, and a list using the code below.

<nav class="navbar navbar-expand-lg bg-light">
  <div class="container-fluid">
    <a class="navbar-brand">Ad Generator</a>
  </div>
</nav>
<div class="container-fluid">
  <div class="row">
    <form class="col-4">
      <div class="mb-3">
        <label for="prompt" class="form-label">What is your product?</label>
        <textarea class="form-control" name="prompt" rows="5" [(ngModel)]="prompt" #ctrl="ngModel" required></textarea>
      </div>
      <button type="submit" class="btn btn-primary" (click)="generateCopy()" [disabled]="!ctrl.valid">Submit</button>
      <button type="button" class="btn" (click)="generated = []">Clear</button>
    </form>
    <div class="col-8">
      <h4>Generated product descriptions</h4>
      <ul class="list-group" *ngIf="generated.length > 0">
        <li class="list-group-item" *ngFor="let text of generated">{{text}}</li>
      </ul>
      <span *ngIf="generated.length == 0">No product descriptions created</span>
    </div>
  </div>
</div>

Run the Web App

To test the web application, open your terminal and run the command below to start the app.

ng serve

This launches your web application, which you access on port 4200 by opening a browser and entering http://localhost:4200. You’ll see an input for the product phrase with keywords on this page. Describe your product here and click Submit to generate copy text for the product.

The screenshot below shows your web application in action. The app can help you or your team create product descriptions with ease.

To build the web application for production, run the following command. The dist/ folder contains the compiled web application ready for deployment.

ng build ad-generator

Conclusion

AI-generated content can help companies describe products quickly and effectively. It helps advertisers keep up with the ever-changing trends in digital platforms.

This tutorial explained how to use the Cohere Platform to generate product descriptions through a simple web application. The process outlined above can help you generate descriptions for hundreds of products with ease.

Ready to get started with AI-generated content? Learn more about developing content with Cohere and sign up for a free Cohere account to start building.