In today's fast-paced ecommerce world, it's more important than ever to have compelling product descriptions that clearly and accurately convey the value of your offerings. However, crafting effective descriptions can be time-consuming, especially if you have a large catalog of products. This is where ChatGPT comes in.
With ChatGPT, you can easily automate the writing of your product descriptions, saving you time and effort while still delivering top-notch content to your customers. What if I told you that the previous paragraph was written by ChatGPT? Did you see how ChatGPT generated human-like text that is indistinguishable?
In this tutorial, you’ll learn how to use ChatGPT to automate writing your product descriptions in Medusa. Medusa is a composable ecommerce platform that can be integrated with any third-party services, including ChatGPT.
You can find the code for the tutorial in this repository.
Here’s a demo of the end result.
What is Medusa?
Medusa is an open source composable commerce engine for developers who want to take ownership of their ecommerce stack. The platform is highly extensible and easy to customize which has made it the #1 JavaScript Ecommerce Platform on Github in record time with 16K+ stars.
Medusa is composed of three components: the headless backend, the admin dashboard, and the storefront. You can set up an entire ecommerce store within minutes by running just a couple of commands.
Since Medusa is highly extensible, it allows developers to build custom features or integrate third-party services such as ChatGPT into Medusa very easily. You can learn more about the official and community plugins here.
What is ChatGPT?
OpenAI has developed a revolutionary long-form question-answering AI called ChatGPT; it is capable of answering complex questions conversationally. This technology is groundbreaking because it has been trained to understand the intended meaning behind human questions.
ChatGPT (Generative Pre-trained Transformer) is a powerful AI tool that leverages the capabilities of OpenAI's GPT-3.5 family of large language models. It has been fine-tuned using a combination of supervised and reinforcement learning techniques to deliver optimal performance.
Different Models Offered by GPT-3
The GPT-3 language models have the ability to understand and generate natural language. Four main language models are available, each with varying levels of power and capabilities to suit different tasks.
: It is the most advanced and capable language model in the GPT-3 family. It is capable of completing any task that the other models can perform, often with higher quality, longer output, and better ability to follow instructions. Additionally, it has the ability to insert completions within the textCopy to clipboardtext-davinci-003
: It is highly capable, but faster and more cost-effective than Davinci.Copy to clipboardtext-curie-001
: It can complete straightforward tasks efficiently and at a lower cost compared to other models.Copy to clipboardtext-babbage-001
: It is capable of performing simple tasks quickly and at a low cost. It is typically the fastest model in the GPT-3 family.Copy to clipboardtext-ada-001
Out of the above four, you’ll use the
for text completion in this tutorial as it yields the best results.Copy to clipboardtext-davinci-003
Getting Your OpenAI API Key
OpenAI provides different APIs to interact with its AI models. It provides you free credit worth $18 to experiment for the first three months.
Follow the steps below to get your free OpenAI API key:
- Go to OpenAI API and sign up for a free account. Follow the instructions, and you’ll be redirected to the API Dashboard.
- Click on your profile picture to view the menu bar, and then click on View API Keys to go to the API Keys page.
- Click on Create new secret key to create a new API Key.
- Copy the API Key and store it safely to use later in the tutorial.
Prerequisites
Before you get started with the tutorial, you should have installed:
Setting up Your Medusa Development Environment
In this section, you’ll create a Medusa server where you’ll integrate ChatGPT in the next step.
Setting up Your Medusa Server
Create a new Medusa store called
and seed the test data using the following command:Copy to clipboardmy-medusa-store
Start your Medusa server using the following command:
The above two steps take a couple of minutes to set up and run your server on port 9000. You can test your server by going to
on your browser or by running the following command:Copy to clipboardhttp://localhost:9000/store/products
If you get a list of products, your server is successfully set up.
Create a Subscriber to Handle Empty Descriptions
In this section, you’ll create a subscriber in your Medusa server that will listen to the
event. The subscriber then calls a function to handle the description field of the product. If the description field is empty, another function will be called to interact with ChatGPT’sCopy to clipboardproduct.created
language model.Copy to clipboardtext-davinci-003
While you can directly interact with the models using their APIs, OpenAI also provides you with several official and community libraries for different languages.
Install the official OpenAI library in your Medusa server:
Add the
in theCopy to clipboardOPENAI_API_KEY
file in your Medusa server:Copy to clipboard.env
Note: Replace the value ofwith a valid API key that you obtained from OpenAI earlier in the tutorial.Copy to clipboardyour-api-key-here
Since you’re going to create a subscriber in your Medusa server, make sure you enable Redis in the
:Copy to clipboardmedusa-config.js
Create a
file insideCopy to clipboarddescription.js
directory in the Medusa server and add the following content:Copy to clipboardsrc/subscribers
In the above code, you import the
class from theCopy to clipboardConfiguration
module and then create a newCopy to clipboardopenai
object using theCopy to clipboardConfiguration
class. TheCopy to clipboardConfiguration
object is initialized with an API key for the OpenAI API.Copy to clipboardConfiguration
You also define a subscriber called
which includes a constructor and two methods:Copy to clipboardDescriptionSubscriber
andCopy to clipboardhandleDescription
.Copy to clipboardprepareDescription
The
method subscribes to an event calledCopy to clipboardconstructor
using theCopy to clipboardproduct.created
and specifies that theCopy to clipboardeventBusService
method should be called whenever this event is emitted.Copy to clipboardhandleDescription
The
method is an async function that is called with aCopy to clipboardhandleDescription
argument. As mentioned in the documentation, the event data payload, i.e., theCopy to clipboarddata
argument contains just theCopy to clipboarddata
of the created product. This method retrieves a product from theCopy to clipboardid
using theCopy to clipboardproductService
property of theCopy to clipboardid
object.Copy to clipboarddata
If the
of the product isCopy to clipboarddescription
, the method generates a description using theCopy to clipboardnull
method and updates the product with the new description.Copy to clipboardprepareDescription
is an async function that is used to generate product descriptions using OpenAI's Text Completion API. The function takes several parameters as inputs:Copy to clipboardprepareDescription
: a string that specifies the text prompt that the API should use as a starting point for generating text. In this case, the text that asks for a description of a product by its properties such as name, material, etc.Copy to clipboardprompt
: (optional, default:Copy to clipboardmodel
) a string that specifies the ID of the model that should be used to generate text.Copy to clipboard"text-davinci-003"
: (optional, default:Copy to clipboardtemperature
) a float that specifies the sampling temperature to use when generating text.Copy to clipboard0.7
: (optional, default:Copy to clipboardmaxTokens
) an integer that specifies the maximum number of tokens (i.e., words or word pieces) that should be generated.Copy to clipboard256
: (optional, default:Copy to clipboardtopP
) a float that specifies the "top-p" value to use when generating text. Higher values will produce more conservative and deterministic output, while lower values will allow for more randomness and diversity in the output.Copy to clipboard1
: (optional, default:Copy to clipboardfrequencyPenalty
) a float that specifies the frequency penalty to use when generating text. Higher values will produce more diverse output, while lower values will allow the model to reuse more common tokens.Copy to clipboard0
: (optional, default:Copy to clipboardpresencePenalty
) a float that specifies the presence penalty to use when generating text.Copy to clipboard0
The function creates an instance of the
class, which is used to communicate with the OpenAI API, and then calls theCopy to clipboardOpenAIApi
method on this instance to generate text.Copy to clipboardcreateCompletion
Testing the Flow
Once you’ve made the required changes in your Medusa server, you’ll need to build your project using the command:
Next, you can restart your server using the command:
Setting up Your Medusa Admin
You can test the flow both from the Medusa Admin and the Admin APIs. In this case, you’ll test using the Medusa Admin.
Medusa Admin is an intuitively designed admin dashboard for your ecommerce Medusa server. Medusa Admin is built with Vite 3 and React. It allows merchants to manage their data such as products, orders, discounts, regions, and more.
Follow the steps to set up your Medusa Admin and create products:
Clone the Medusa Admin GitHub repository and change your path to the cloned directory:
Install the dependencies using the following command:
Once all the dependencies are installed, run the Admin server using the following command:
The Medusa Admin runs on port 7000, so go to
in your browser to open your Admin Panel.Copy to clipboardhttp://localhost:7000
Since you’ve already seeded the data in your Medusa server, you can use the email
and passwordCopy to clipboardadmin@medusa-test.com
to log in.Copy to clipboardsupersecret
Creating Products in Medusa Admin
Click on the
tab on the left side and then click on theCopy to clipboardProducts
button.Copy to clipboardNew Product
Since the admin is configured to fetch the products right after the product is created, which is before the subscriber runs, you’ll need to refresh the page to see the changes. You can customize the admin to refetch the product after it’s created to resolve this.
Once you publish a product without a description, your product will initially be created with an empty description. The
event is then emitted, and theCopy to clipboardproduct.created
is triggered.Copy to clipboardDescriptionSubscriber
Since the description is empty, the subscriber uses the
function to generate a description and updates the description of the product.Copy to clipboardprepareDescription
Conclusion
In this tutorial, you learned how you can use ChatGPT to automate writing your product description. You can add more product features such as its price, variants, and more to generate a more unique description.
This integration was made easy due to Medusa’s composable architecture. You can learn more about Medusa through its documentation. Here are some documents that you can start with:
- Set up a Next.js or Gatsby storefront.
- Integrate Stripe as a payment provider.
- Create a custom endpoint.
Should you have any issues or questions related to Medusa, then feel free to reach out to the Medusa team via Discord.
Share this post
Try Medusa
Spin up your environment in a few minutes.

You may also like
On this page