Created
April 17, 2024 16:02
-
-
Save fnando1995/33bab29d4f5debc5c22c7a93942f6209 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "b97ddf3b-d921-4169-9d82-b0ee95e9eccd", | |
"metadata": {}, | |
"source": [ | |
"# Iterative Prompt Development\n", | |
"In this lesson, you'll iteratively analyze and refine your prompts to generate marketing copy from a product fact sheet.\n", | |
"\n", | |
"## Setup" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "2800e646", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"import openai\n", | |
"import os\n", | |
"\n", | |
"from dotenv import load_dotenv, find_dotenv\n", | |
"_ = load_dotenv(find_dotenv()) # read local .env file\n", | |
"\n", | |
"openai.api_key = os.getenv('OPENAI_API_KEY')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "9fb6f2de", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n", | |
" messages = [{\"role\": \"user\", \"content\": prompt}]\n", | |
" response = openai.ChatCompletion.create(\n", | |
" model=model,\n", | |
" messages=messages,\n", | |
" temperature=0, # this is the degree of randomness of the model's output\n", | |
" )\n", | |
" return response.choices[0].message[\"content\"]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ffc2b32f-73da-4ef0-81f6-0c7bff70783d", | |
"metadata": {}, | |
"source": [ | |
"**Note**: In June 2023, OpenAI updated gpt-3.5-turbo. The results you see in the notebook may be slightly different than those in the video. Some of the prompts have also been slightly modified to product the desired results." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "71d1a1df-03c3-4f38-af6d-3962362f3dcd", | |
"metadata": {}, | |
"source": [ | |
"## Generate a marketing product description from a product fact sheet" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "5be90e47", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"fact_sheet_chair = \"\"\"\n", | |
"OVERVIEW\n", | |
"- Part of a beautiful family of mid-century inspired office furniture, \n", | |
"including filing cabinets, desks, bookcases, meeting tables, and more.\n", | |
"- Several options of shell color and base finishes.\n", | |
"- Available with plastic back and front upholstery (SWC-100) \n", | |
"or full upholstery (SWC-110) in 10 fabric and 6 leather options.\n", | |
"- Base finish options are: stainless steel, matte black, \n", | |
"gloss white, or chrome.\n", | |
"- Chair is available with or without armrests.\n", | |
"- Suitable for home or business settings.\n", | |
"- Qualified for contract use.\n", | |
"\n", | |
"CONSTRUCTION\n", | |
"- 5-wheel plastic coated aluminum base.\n", | |
"- Pneumatic chair adjust for easy raise/lower action.\n", | |
"\n", | |
"DIMENSIONS\n", | |
"- WIDTH 53 CM | 20.87”\n", | |
"- DEPTH 51 CM | 20.08”\n", | |
"- HEIGHT 80 CM | 31.50”\n", | |
"- SEAT HEIGHT 44 CM | 17.32”\n", | |
"- SEAT DEPTH 41 CM | 16.14”\n", | |
"\n", | |
"OPTIONS\n", | |
"- Soft or hard-floor caster options.\n", | |
"- Two choices of seat foam densities: \n", | |
" medium (1.8 lb/ft3) or high (2.8 lb/ft3)\n", | |
"- Armless or 8 position PU armrests \n", | |
"\n", | |
"MATERIALS\n", | |
"SHELL BASE GLIDER\n", | |
"- Cast Aluminum with modified nylon PA6/PA66 coating.\n", | |
"- Shell thickness: 10 mm.\n", | |
"SEAT\n", | |
"- HD36 foam\n", | |
"\n", | |
"COUNTRY OF ORIGIN\n", | |
"- Italy\n", | |
"\"\"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "4f656d0c", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"prompt = f\"\"\"\n", | |
"Your task is to help a marketing team create a \n", | |
"description for a retail website of a product based \n", | |
"on a technical fact sheet.\n", | |
"\n", | |
"Write a product description based on the information \n", | |
"provided in the technical specifications delimited by \n", | |
"triple backticks.\n", | |
"\n", | |
"Technical specifications: ```{fact_sheet_chair}```\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e1361d6f-6022-4b7a-8107-b2e2ff5efd32", | |
"metadata": {}, | |
"source": [ | |
"## Issue 1: The text is too long \n", | |
"- Limit the number of words/sentences/characters." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f6129532", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"prompt = f\"\"\"\n", | |
"Your task is to help a marketing team create a \n", | |
"description for a retail website of a product based \n", | |
"on a technical fact sheet.\n", | |
"\n", | |
"Write a product description based on the information \n", | |
"provided in the technical specifications delimited by \n", | |
"triple backticks.\n", | |
"\n", | |
"Use at most 50 words.\n", | |
"\n", | |
"Technical specifications: ```{fact_sheet_chair}```\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "17f658d0", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"len(response.split())" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a6de9c92-857c-4d1c-8fd1-192cf47c4763", | |
"metadata": {}, | |
"source": [ | |
"## Issue 2. Text focuses on the wrong details\n", | |
"- Ask it to focus on the aspects that are relevant to the intended audience." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "954a8a06", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"prompt = f\"\"\"\n", | |
"Your task is to help a marketing team create a \n", | |
"description for a retail website of a product based \n", | |
"on a technical fact sheet.\n", | |
"\n", | |
"Write a product description based on the information \n", | |
"provided in the technical specifications delimited by \n", | |
"triple backticks.\n", | |
"\n", | |
"The description is intended for furniture retailers, \n", | |
"so should be technical in nature and focus on the \n", | |
"materials the product is constructed from.\n", | |
"\n", | |
"Use at most 50 words.\n", | |
"\n", | |
"Technical specifications: ```{fact_sheet_chair}```\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "e9455d58", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"prompt = f\"\"\"\n", | |
"Your task is to help a marketing team create a \n", | |
"description for a retail website of a product based \n", | |
"on a technical fact sheet.\n", | |
"\n", | |
"Write a product description based on the information \n", | |
"provided in the technical specifications delimited by \n", | |
"triple backticks.\n", | |
"\n", | |
"The description is intended for furniture retailers, \n", | |
"so should be technical in nature and focus on the \n", | |
"materials the product is constructed from.\n", | |
"\n", | |
"At the end of the description, include every 7-character \n", | |
"Product ID in the technical specification.\n", | |
"\n", | |
"Use at most 50 words.\n", | |
"\n", | |
"Technical specifications: ```{fact_sheet_chair}```\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5be496d5-54c9-443b-8116-0b3d3d9dc560", | |
"metadata": {}, | |
"source": [ | |
"## Issue 3. Description needs a table of dimensions\n", | |
"- Ask it to extract information and organize it in a table." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "0384f1e4", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"prompt = f\"\"\"\n", | |
"Your task is to help a marketing team create a \n", | |
"description for a retail website of a product based \n", | |
"on a technical fact sheet.\n", | |
"\n", | |
"Write a product description based on the information \n", | |
"provided in the technical specifications delimited by \n", | |
"triple backticks.\n", | |
"\n", | |
"The description is intended for furniture retailers, \n", | |
"so should be technical in nature and focus on the \n", | |
"materials the product is constructed from.\n", | |
"\n", | |
"At the end of the description, include every 7-character \n", | |
"Product ID in the technical specification.\n", | |
"\n", | |
"After the description, include a table that gives the \n", | |
"product's dimensions. The table should have two columns.\n", | |
"In the first column include the name of the dimension. \n", | |
"In the second column include the measurements in inches only.\n", | |
"\n", | |
"Give the table the title 'Product Dimensions'.\n", | |
"\n", | |
"Format everything as HTML that can be used in a website. \n", | |
"Place the description in a <div> element.\n", | |
"\n", | |
"Technical specifications: ```{fact_sheet_chair}```\n", | |
"\"\"\"\n", | |
"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d681db67-6d59-40c1-a36b-4d76729a690d", | |
"metadata": {}, | |
"source": [ | |
"## Load Python libraries to view HTML" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "73ff0052", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"from IPython.display import display, HTML" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3465ffce", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"display(HTML(response))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8ed02a02-9e12-4f63-97bd-f52064b18666", | |
"metadata": {}, | |
"source": [ | |
"## Try experimenting on your own!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "dbe1f850", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment