Skip to content

Instantly share code, notes, and snippets.

@lpurdy01
Last active December 5, 2021 21:09
Show Gist options
  • Save lpurdy01/d820f58a6be8ecf34233a8fea4a272f6 to your computer and use it in GitHub Desktop.
Save lpurdy01/d820f58a6be8ecf34233a8fea4a272f6 to your computer and use it in GitHub Desktop.
OpenAI's GPT-3 engine is a tool you need.

TLDR;

This is a test of OpenAI’s davinci-codex engine, Natural language to python preset.

default-english-to-python playground

It’s not perfect, but really useful.


README

OpenAI has been working on AI language prediction models for a while. Language prediction models are AI that specialize in generating what comes next. So, you usually give them the start of something, and they try to finish it. Formatting a request is usually done as:

Structure of a request

  • Some kind of context:

    • A little bit of text that informs the AI of what kind of thing you want back. Example:

    “In Python”

    “A cooperative company’s email response to a customer:”

    “An amazon product listing for an electronic device”

  • A prompt:

    • A set of steps to follow, or what you use to request from someone.

    Steps to follow in the process of some code.

    “Write an email to a customer that has requested a return.”

    Steps to make up an electronic device and then write product copy text.

  • An example of a response (optional, but usually helps it get better at that you are looking for)

What I'm Talking About

The model I’m using now is a few variations of OpenAI’s GPT-3 (Generative Pre-trained Transformer). It is a language AI that was trained mostly on public internet content, and the internet contains a lot of code, so naturally it’s pretty good at coding.

You can check out: GPT-3 Wikipedia

I’ve been floored by its performance a few times now. I am using it for python programming, but it can be used to accelerate almost any text-based workflow. GPT-3 doesn’t usually come back with the exact answer you want. I think of it as a super search engine that has read a ton of stuff and will try its best to answer your question. Not usually right but will get you going in a direction fast, code it generates usually isn’t nearly the best, if it even works.

It is an amazing technology. Having a clumsy superintelligence to bounce ideas off is really helpful.

Some example tests follow:

time_binary_search_test.py commentary

  • It decided to create a random_date function before completing any of the requested steps

  • It figured out that if the list was sorted it only needed to grab the first and last element

  • It added a bit of extra code (kinda humorous) that checks if the datetime happened to be generated exactly

model link, no parameters

Engine: davinci-instruct-beta

Preset: Grammatical Standard English (except: Temperature 1, Response length 1000, no Stop sequences)


Input

An amazon product listing for an electronic device:

1. Invent a $20 electronic accessory and name it.

2. Describe the product to an investor.

3. Make an Amazon Title for the product.

4. Write a buzzword filled description for the product.


Output

  1. A Portable Battery Charger
  2. This personal battery charger is intended to be to take with you during emergencies in order to charge your devices if outside power is not available. You can charge your phone up to four times on one charge when plugged into an outlet.
  3. The COL Blinks! And Snap!
  4. Our COL is perfect for students, travelers, and anyone who wants to be able to charge their device anywhere.

model link, no parameters

Engine: davinci-instruct-beta

Preset: Grammatical Standard English (except: Response length 310, no Stop sequences, Frequency penalty 2)


Input

A cooperative company’s email response to a customer:

Write a email to a customer that has requested a return. Nicely ask them to please work through a few steps to troubleshoot their device.


Output

Dear Thank you for contacting us. We are sorry to hear that your device is not working properly and we would be happy to help troubleshoot the issue with you. Please follow these steps: 1) Turn off the device, remove the battery, wait 10 seconds and then reinsert it; 2) Press and hold down both volume buttons on top of your phone at once until a menu pops up; 3) Select "Wipe Data/Factory Reset" from this menu by using either volume button to scroll through options or power button if needed; 4) Once complete, select "Reboot System Now." If this does not work please contact our customer service team at 1-800-555-1234 so they can further assist you. Thank You!

"""
time_binary_search_test.py
10/22/2021
Contributors: 'Levi Purdy ljpurdy@(the alaska university domain).edu'
A test file to experiment with binary searching time
Most code was generated by OpenAI's davinci-codex
https://beta.openai.com/playground/p/default-english-to-python
"""
"""
AI input:
\"""
In Python
1. Create a list of datetimes
2. Sort the list
3. Find the minimum and maximum datetime in the list
4. Generate a random timestamp between the minimum and maximum datetime
5. Binary seearch the list of datetimes for the nearest timestamp
\"""
"""
"""
AI output:
"""
import random
import datetime
import bisect
def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
"""
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = random.randrange(int_delta)
return start + datetime.timedelta(seconds=random_second)
# generate a list of datetimes
start = datetime.datetime(2018, 1, 1, 1, 1, 1)
end = datetime.datetime(2019, 1, 1, 1, 1, 1)
# create a list of datetimes
datetime_list = []
for i in range(100):
datetime_list.append(random_date(start, end))
# sort the list
datetime_list.sort()
# find the minimum and maximum datetime in the list
min_datetime = datetime_list[0]
max_datetime = datetime_list[-1]
# generate a random timestamp between the minimum and maximum datetime
random_datetime = random_date(min_datetime, max_datetime)
# binary search the list of datetimes for the nearest timestamp
index = bisect.bisect_left(datetime_list, random_datetime)
if datetime_list[index] == random_datetime:
print("Found it!")
else:
print("Didn't find it!")
print(random_datetime)
print(datetime_list[index])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment