Skip to main content
In this hands-on tutorial, you’ll learn how to leverage the OpenAI ChatCompletion API inside a Jupyter Notebook to generate product descriptions, concise summaries, and HTML snippets. By the end, you’ll have a reusable Python helper function and three prompt patterns for marketing automation.

Prerequisites


1. Setup: Import Modules & Configure API Key

Begin by importing the required libraries and setting your API key as an environment variable.
Never commit your API key to source control. Use environment variables or a secrets manager to keep credentials safe.

2. Define the get_word_completion Helper Function

Encapsulate the ChatCompletion call in a function to simplify reuse:

ChatCompletion.create Parameters

Adjust temperature to control creativity; lower values yield more focused outputs, higher values produce varied text.

3. Prepare the Product Specification

We’ll use a multi-line string as our product spec for a fictional TerrainMaster Pro 2023 Mountain Bike:

4. Example 1: Generating a Full Product Description

Use the full specification to generate a detailed website description.

6. Example 3: Generating HTML with a Specifications Table

Ask GPT-3.5 Turbo to output a complete HTML snippet, including:
  • An <h1> with the product name
  • A <div> styled in 12pt FireBrick font for the description
  • A titled table (Product Specifications) with two columns: name & value
  • Table CSS: DarkSlateGray text, 100% width, 12pt font
The image shows a Jupyter Notebook interface displaying a text block that defines the context for a prompt about the "TerrainMaster Pro 2023 Mountain Bike," detailing its features, specifications, and price.

You’ve now implemented a flexible word completion demo! Next up, we’ll explore GPT-3.5 Turbo’s code-completion capabilities.

Watch Video

Practice Lab