DEV Community

Cover image for Automating Product Description Generation with Lyzr Automata and OpenAI
harshit-lyzr
harshit-lyzr

Posted on

Automating Product Description Generation with Lyzr Automata and OpenAI

In the competitive world of e-commerce, an appealing product description can make all the difference in attracting customers. Crafting engaging and SEO-friendly descriptions, however, can be a time-consuming task. But what if you could automate this process?

With advancements in artificial intelligence and natural language processing, generating product descriptions has become more efficient than ever. In this blog post, we’ll explore how to harness the power of Lyzr Automata and OpenAI to automate the creation of compelling product descriptions.

Lyzr.ai

Check out the Lyzr.ai community on Discord – hang out with 331 other members and enjoy free voice and text chat.

favicon discord.com

Lyzr Automata
Lyzr Automata is a versatile toolkit for building intelligent agents that automate various tasks. From generating text to analyzing data, Lyzr Automata simplifies complex workflows through automation.

The Product Description Generator App
We begin by creating a Streamlit web application for generating product descriptions. The app allows users to input the product name and specifications, leveraging Lyzr Automata and OpenAI in the backend to generate descriptive text.

Imports and Environment Setup:

pip install lyzr_automata streamlit
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent,Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from PIL import Image
from dotenv import load_dotenv
import os
from prompt import example

load_dotenv()
api = os.getenv("OPENAI_API_KEY")
Enter fullscreen mode Exit fullscreen mode

Imports necessary libraries like streamlit, dotenv, PIL, and custom libraries for Lyzr Automata.
Loads environment variables using load_dotenv (likely containing the OpenAI API key).
load_dotenv() is called to load environment variables from a .env file (likely containing your OpenAI API key).
api = os.getenv("OPENAI_API_KEY") retrieves the API key from the environment variable.

OpenAI Model Configuration:

open_ai_text_completion_model = OpenAIModel(
    api_key=api,
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.2,
        "max_tokens": 1500,
    },
)
Enter fullscreen mode Exit fullscreen mode

Defines an OpenAIModel object using the provided API key.
Sets model parameters including the powerful “gpt-4-turbo-preview” model, temperature, and maximum token limit.

User Input Fields:

name = st.sidebar.text_input("Enter Your Product Name", placeholder="Samsung Galaxy S24")
specification = st.sidebar.text_area("Enter your Product Specification", placeholder="Google",height=200)
Enter fullscreen mode Exit fullscreen mode

name = st.sidebar.text_input creates a text input field in the sidebar for users to enter their product name (with a placeholder "Samsung Galaxy S24").
specification = st.sidebar.text_area creates a text area field in the sidebar for users to enter product specifications (with a placeholder "Google" and a height of 200 pixels).

Example Product Description:

example=f"""
    Split AC with inverter compressor: Variable speed compressor which adjusts power depending on heat load. Powered with 23000 microholes, you can enjoy powerful and gentle cooling. With Convertible 5in1 modes, you can change according to your mood and requirement.
    Capacity (1.5Ton): Suitable for medium sized rooms (111 to 150 sq ft)
    Energy Rating: 5 Star BEE Rating with Power Saving Mode | ISEER rating of 5.16 W/W (better than industry benchmarks | Electricity Consumption : 749.48 Units Per Year
    Warranty: 1 Year Standard Warranty on Product, 1 Year Warranty on PCB, 10 Years Warranty on Digital Inverter Compressor
    Copper Condenser Coil: Better cooling and requires low maintenance
    Key Features: Convertible 5in1, 4 way swing, 3 Step Auto Clean, Easy to Clean Filter, Copper Anti-Bacterial Filter, Coated Copper tubes
    Special Features: Windfree Cooling, Windfree Good Sleep, Durafin Ultra, Triple Protection Plus, Digital Inverter Technology
    Refrigerant Gas: R32 - Environmental Friendly - No Ozone Depletion Potential. The air conditioner uses the next generation R32 refrigerant, which helps conserve the ozone layer and has a low impact on global warming
    Indoor Unit Dimensions (mm) WxHxD): 1055 X 299 X 215 mm | Outdoor Unit Dimensions (mm) WxHxD): 880 X 638 X 310 mm | Indoor Weight 11.4 | Outdoor Weight 37
    Included In The Box: 1 Indoor Unit, 1 Outdoor Unit, Copper Pipe (3 meter standard) 1 unit , 1 Remote, 1 Manuals , 2 Batteries
"""
Enter fullscreen mode Exit fullscreen mode

A multi-line string variable example holds a sample product description with various details and formatting for reference.

product_description function:

def product_description(product_name,specs):

    pd_agent = Agent(
            role="Product Description expert",
            prompt_persona=f"You are an Expert Ecommerce Expert.Your Task Is to write SEO Friendly Product Description."
        )

    prompt = f"""Write a Product Description for below product with attached specification.
    product: {product_name}
    specification: {specs}

    Follow Below Instruction:
    1/ Understand who your target customers are and tailor the description to their needs, preferences, and interests.
    2/ Identify the most important features of the product and emphasize them in your description. Focus on what sets the product apart from others.
    3/ Incorporate relevant keywords into your description to improve search engine visibility and attract organic traffic to your product page.
    4/ Write in a clear and straightforward manner, avoiding jargon or technical language that might confuse customers. Keep sentences and paragraphs short for easy reading.
    5/ Write Description With Bullet Points
    6/ Do not include any internet information for that product

    Example Description:
    {example}
    """

    pd_task = Task(
        name="Generate Product Description",
        model=open_ai_text_completion_model,
        agent=pd_agent,
        instructions=prompt,
    )

    output = LinearSyncPipeline(
        name="Product Description Pipline",
        completion_message="Product Description Generated!!",
        tasks=[
              pd_task
        ],
    ).run()

    answer = output[0]['task_output']

    return answer
Enter fullscreen mode Exit fullscreen mode

This function takes product_name and specs (specifications) as arguments.
An Agent object (pd_agent) is created within the Lyzr Automata framework, defining its role as a "Product Description expert" and setting the prompt persona for the task.
A multi-line string variable prompt defines the instructions for the agent, including:
Highlighting the product name and specifications provided by the user.
Listing specific guidelines for the product description (target audience, key features, SEO keywords, clarity, bullet points, and avoiding internet information).
Providing an example product description (example) as a reference.
A Task object (pd_task) is created, specifying the task name ("Generate Product Description"), the model to be used (open_ai_text_completion_model), the agent (pd_agent), and the instructions (prompt).
A LinearSyncPipeline object (pipeline) is created, defining the pipeline name ("Product Description Pipeline"), a completion message ("Product Description Generated!!"), and a list of tasks containing the previously defined pd_task.
The run method of the pipeline is called, executing the defined task.
The answer variable stores the task output, which is the generated product description.
The function returns the generated product description (answer).

Output:


if st.sidebar.button("Generate", type="primary"):
    solution = product_description(name, specification)
    st.markdown(solution)
Enter fullscreen mode Exit fullscreen mode

An “Generate” button is created in the sidebar using st.sidebar.button with the button type set to "primary" for better visual distinction.
When the button is clicked, the if statement checks if the button is pressed.
If pressed, the product_description function is called with the user-provided `

try it now: https://lyzr-ecommerce-description.streamlit.app/

For more information explore the website: Lyzr

Top comments (0)