Photo by Tyler Nix on Unsplash

Observations From Beta Testing OpenAI Codex

And How It’s Fusing Natural Language & Coding

Introduction

Date calculator calculates the days difference between two dates created with a date picker. Created with Codex in JavaScript using natural language.
Time difference in seconds between the two times are calculated. The times are selected with time pickers. Created with Codex in JavaScript using natural language.

A Practical Example: JavaScript Chatbot

In this playground example, the user questions are marked in red blocks, with the Codex answer.
pip install openai
import openai

openai.api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
response = openai.Completion.create(
engine="davinci-codex",
prompt="JavaScript chatbot\n\n\nYou: How do I combine arrays?\nJavaScript chatbot: You can use the concat() method.\nYou: How do make an alert appear after 10 seconds?\nJavaScript chatbot: You can use the setTimeout() method.\nYou: Show me a practical implementiton of concat();",
temperature=0,
max_tokens=60,
top_p=1,
frequency_penalty=0.5,
presence_penalty=0,
stop=["You:"]
)
print (response)
{
"choices": [
{
"finish_reason": "length",
"index": 0,
"logprobs": null,
"text": "\nJavaScript chatbot: You can use the following code:\n\n

var arr1 = [1, 2, 3];\n
var arr2 = [4, 5, 6];\n
var arr3 = arr1.concat(arr2);\n
console.log(arr3);"
}
],
"created": 1630678929,
"id": "cmpl-3e4Mz3L6Tfk0vc5",
"model": "davinci-codex:2021-08-03",
"object": "text_completion"
}

Practical Example: Python Notebook

This is the playground view where the user asks Codex to create a test data frame.
An implementation in a Notebook of the code created by Codex.
The complete data frame printed out in the Notebook.
# Scatter plot
df.plot(kind='scatter', x='A', y='B')
# Hexbin plot
df.plot(kind='hexbin', x='A', y='B')
# Box plot
df.plot(kind='box')
# Histogram
df.plot(kind='hist', alpha=0.5)
# Bar plot
df.plot(kind='bar')
# Line plot
df.plot(kind='line')
# Area plot
df.plot(kind='area')
Some of the data visualization options suggested by Codex.

Python To Natural Language

Codex is given three lines of Python code and an augmented natural language explanation is given.
In this example, the Python code generated by Codex is submitted back to Codex. The explanation is again very accurate.
# The code above creates a dataframe with 20 rows and 5 columns. The columns are named A, B, C, D, and E. The dataframe is then filled with random integers from 0 to 100. The product column is created by concatenating the string 'product_' with a random integer from 0 to 100. The product column is then set as the index.

Fixing Code

##### Fix bugs in the below function

### Buggy Python
a=20
b=10
if a==b:
print ("equal")
else:
print "unequal"

### Fixed Python
a=20
b=10
if a==b:
print ("equal")
else:
print ("unequal")
# Python Loops
#
# Python has two primitive loop commands:
#
# * while loops
# * for loops
#
# The while Loop
# With the while loop we can execute a set of statements as long as a condition is true.
#
# **while** *expression*:
#
# * statement(s)
#
# The **while** loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.
i = 1
while i < 6:
print(i)
i += 1
# **Note: remember to increment i, or else the loop will continue forever.**
#
# The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.
#
# The **break** statement can be used to stop the loop even if the while condition is true:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
# The **continue** statement can be used to stop the current iteration, and continue with the next:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
# The for loop
# A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
#
# This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.
#
# With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
#
# **for** *variable* **in** *expression*:
#
# * statement(s)
#
# **Note:** The expression is executed only once.
#
# Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
# Looping Through a String
# Even strings are iterable objects, they contain a sequence of characters

Conclusion