After getting familiar with Codex, I’ve found quite a few techniques that may help you out!
I’m usually able to keep Codex on track during bigger completions by doing a few things. I’ll usually start a multi-line comment area written in the programming language I want Codex to output. Then within that area, I’ll state the overall task that I want Codex to accomplish including specific details when desired.
After I write the overall task out, I then provide Codex with the overall task again, but this time broken down into subtasks so that each subtask is basically instructions to create a class or function.
Once I’ve written out the subtasks, I’ll start the completion with the first subtask commented above where I want Codex to start writing the class or function. If it’s a more advanced topic, I’ll try to go into more detail and provide examples along the way, but I myself am still fine-tuning my prompt design process.
By starting the completion off with the first subtask as a comment above the code, Codex will write out the code, then upon completing that subtask, will continue the pattern and comment in the second subtask before writing out the code for that subtask. I’ve found that it sort of “reminds” Codex of the overall task while also adding in enough detail in each subtask to get fairly good code throughout the completion.
Let me provide an example of what I mean.
Python:
"""
You are a programmer tasked with writing a Python script that uses the 'Selenium' web automation module and the 'chromedriver_autoinstaller' module to scrape webpages. Here are some objects you will need to use in the script:
# Link that goes to OpenAI's API documentation:
openai_documentation_link = "https://beta.openai.com/docs/introduction"
# Xpath for any 'a' element under the 'side-nav-section' div element:
documentation_nav_section = "//div[contains(@class,'side-nav-section')]//a"
Tasks:
1. Import the necessary modules we'll need for web automation.
2. Initialize the variables referenced above.
3. Use the 'chromedriver_autoinstaller' module to install the correct chromedriver we'll need for Selenium.
4. Open a Chrome browser with Selenium and navigate the URL that 'openai_documentation_link' references.
5. Get all elements by XPath 'documentation_nav_section' and save them in an array.
6. Create a dictionary before looping through the array of elements. Use the get_attribute() function to get both the attribute 'textContent' and 'href' from each element and then append them to the dictionary where the text content is the dictionary key and the href link is the value of that key.
7. Print the entire dictionary.
"""
"""
1. Import the necessary modules we'll need for web automation.
"""
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import chromedriver_autoinstaller
"""
2. Initialize the variables referenced above.
"""
openai_documentation_link = "https://beta.openai.com/docs/introduction"
documentation_nav_section = "//div[contains(@class,'side-nav-section')]//a"
"""
3. Use the 'chromedriver_autoinstaller' module to install the correct chromedriver we'll need for Selenium.
"""
chromedriver_autoinstaller.install()
"""
4. Open a Chrome browser with Selenium and navigate the URL that 'openai_documentation_link' references.
"""
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)
driver.get(openai_documentation_link)
"""
5. Get all elements by XPath 'documentation_nav_section' and save them in an array.
"""
documentation_nav_section_elements = driver.find_elements_by_xpath(documentation_nav_section)
"""
6. Create a dictionary before looping through the array of elements. Use the get_attribute() function to get both the attribute 'textContent' and 'href' from each element and then append them to the dictionary where the text content is the dictionary key and the href link is the value of that key.
"""
documentation_nav_section_dict = {}
for element in documentation_nav_section_elements:
documentation_nav_section_dict[element.get_attribute("textContent")] = element.get_attribute("href")
"""
7. Print the entire dictionary.
"""
print(documentation_nav_section_dict)
Preset: Python Webscraper Demonstration (Fixed as of 10/10/2021)
I started the completion at “”" before starting Task 2.
I wonder if there are better methods, this is simply one of many possible methods to help keep Codex on track.
Hope I helped somewhat!
EDIT 10/8/2021: I fixed my prompt so that Codex was able to get the ‘textContent’ attribute which correctly grabbed the text from each link. The completion above correctly grabs the text and link, puts them in a dictionary, then print the dictionary!
EDIT 10/10/2021: Oh my! I thought I had fixed the prompt to generate correct output. After a couple minutes I managed to fix my prompt and Codex supplied the desired output flawlessly!! Sorry y’all (the Preset link has been updated too)!