Photo by Tyler Nix on Unsplash

Observations From Beta Testing OpenAI Codex

And How It’s Fusing Natural Language & Coding

Cobus Greyling

--

Introduction

What is Codex?

In its essence, OpenAI Codex translates natural language into code. And, can translate code (back) into natural language…or at least explain what the software does.

How might Codex be used?

There are a few practical implementations for Codex…

Date calculator calculates the days difference between two dates created with a date picker. Created with Codex in JavaScript using natural language.
  1. As a coding tutor, helping developers in their coding endeavors. Software principles and functionality can be illustrated & explained. Functions and applications can be created from natural language input.
  2. Codex can be implemented as an AI powered Stack Overflow where users can ask questions in natural language, and Codex responds with code examples.
  3. Or code can be explained.
  4. Codex can act as a general resource for a company’s internal development team. Code can be generated or data insights can be gleaned etc.
  5. For individual use, of course. For any developer this is the ultimate Google for anything code related.
  6. Quality assessment.
  7. Automation of code validation.

What will Codex not be used for?

The brilliance of Codex is really evident and undisputable. The code generated is accurate and works. But…

  1. Codex will not replace developers and software engineers. Not now, at least. This is AI assisting & augmenting developers, not replacing developers.
  2. Codex does well in affording you various options, anticipating what your next question might be. Context is maintained and questions from users need not always be explicit. It is not autonomous and does not perform orchestration. Typos are absorbed. But…it is not autonomous, problems need to be broken down into smaller pieces and those presented to Codex. You need to know what you want to achieve, and how to get there. Basically define your algorithm.

The best approach to take when building an application using Codex…

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.
  • Break a problem down into simpler problems or modules, and…
  • …then convert those simpler problems into code segments which can be combined and executed.
  • As a user, you need to know what you want to achieve; have an idea of what the best software is for the application.
  • You need to be able break your algorithm into into smaller tasks or modules.
  • If you have an idea, you can ask Codex for best practice. For instance, when a data frame is loaded via Python, you can ask, give me visualizing suggestions. Or, how can Crosstab be implemented, and Codex takes the initiative.

More about Codex…

Codex is the model that powers GitHub Copilot, which OpenAI built and launched in partnership with GitHub.

Codex is proficient in more than a 12 programming languages. Codex takes simple commands in natural language and execute them on the user’s behalf.

OpenAI Codex is based on GPT-3. According to OpenAI, Codex’s training data contains both natural language and billions of lines of source code from publicly available sources, including code in public GitHub repositories.

OpenAI Codex is most proficient in Python, but also incorporates languages like:

  • JavaScript, Go, Perl, PHP, Ruby, Swift, TypeScript, Shell.

What surprised me about Codex?

  • How natural and coherent the comments added tot the code are. The Natural Language Generation (NLG) is obviously on par with GPT-3. The cadence of comments in code is also tight.
  • Context is maintained within the conversation and where user input is not comprehensive or explicit, accurate implicit assumptions are made by Codex.
  • The code works. Users can copy it out of the Codex preview, paste it into a Notebook and execute. I did not have an instance where the code did not execute.
  • The process of generating code is very modular and a request is broken up in to separate sequential steps. Even if your request is quite encompassing, Codex with break it down and present the code in a modular fashion.
  • Codex is AI with which users most probably will interact on a daily basis.
  • Mistakes in spelling and grammar are absorbed by Codex with surprising resilience.

A Practical Example: JavaScript Chatbot

This is an implementation of a JavaScript chatbot. Users can ask the chatbot any question regarding JavaScript. A user can ask the chatbot a practical question and the chatbot responds in natural language actually explaining the implementation in natural language.

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

As with the other offerings from OpenAI, the natural language generation is smooth, coherent and truly natural. Should you ask for a practical implementation, the Codex JavaScript chatbot will return working code.

Below is a practical example of a simplistic Python implementation of the JavaScript chatbot with the prompt input.

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)

You can see the typos in the user input, but it does not cause any fallback proliferation.

Below is the JSON 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"
}

The JavaScript chatbot is diverse in responses understanding if a user wants code or an explanation. This a complete API ready for use.

Practical Example: Python Notebook

This is an example of a user asking Codex basic questions about Python implementations of data frames and analyzing data.

This is the playground view where the user asks Codex to create a test data frame.

This is the playground view where the user asks Codex to create a test data frame.

An implementation in a Notebook of the Python code created by Codex.

An implementation in a Notebook of the code created by Codex.

The complete data frame printed out in the Notebook…

The complete data frame printed out in the Notebook.

When Codex is asked to provide a few examples of visualizing the data frame data, the following code is generated by Codex.

# 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')

The rendering from the Notebook.

Some of the data visualization options suggested by Codex.

Python To Natural Language

A simple three line implementation of Python is provided and and Codex gives a detailed natural language generated explanation of the code.

Codex is given three lines of Python code and an augmented natural language explanation is given.

In this example below, the Python code generated by Codex earlier in the notebook example is submitted back to Codex. The explanation is again very accurate.

In this example, the Python code generated by Codex is submitted back to Codex. The explanation is again very accurate.

The paragraph wit the explanation from Codex below…

# 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

A Python example with buggy code and the fixed version from Codex:

##### 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")

To boot, Codex adds a tutorial on Python loops. This is good initiative Codex takes, seeing my buggy code is a very basic piece of code.

# 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

There has been much discussion on Low Code versus handcrafted or traditional coding. It is not a case of the one or the other. There is a place and an application for both. With advantages and disadvantages.

I discuss that topic in detail here.

And the same holds true for Codex. Will enterprise systems be built this way, most probably not. Will Fortune 500 companies go the Codex route in principle…no.

But, there are some defernite niche applications, these can include:

  • Solving coding challenge and problems in certain routines.
  • Establishing best practice.
  • Quality assurance.
  • Interactive Learning
  • Generating specific components for subsequent human review.

--

--