Best Beginners guide to OpenAI Codex, with 5 practical examples
OpenAI Codex is a new GPT-3 AI version that can convert plain English into runnable programming code. For example,
Suppose when I input this prompt to OpenAI Codex in the OpenAI GPT-3 Playground, it gives me the output of the code below the prompt.
Prompt:
# Create a Python dictionary of 6 countries and their capitals
Countries =
GPT-3 Codex Output
countries = {'Canada': 'Ottawa', 'United States': 'Washington, D.C.', 'Mexico': 'Mexico City', 'United Kingdom': 'London', 'France': 'Paris', 'Germany': 'Berlin'}
# Print the dictionary
print(countries)
# Print the keys of the dictionary
print(countries.keys())
# Print the values of the dictionary
print(countries.values())
As you can see, this is a perfectly fine and runnable python code.
It can also do other tasks related to Code, like translating code from one language to another and explaining already written code in plain English, among other things.
Codex is fine-tuned by OpenAI on a large amount of publicly available code.
Table of Contents
- Getting access to OpenAI GPT-3 Codex
- Using OpenAI GPT-3 Codex AI model
- What programming languages does Codex support?
- Interesting use cases for OpenAI Codex with prompts
- What’s the difference between Github Copilot vs OpenAI Codex?
- Where can I learn more about Codex?
- What is possible with Codex?
- I got access to OpenAI Codex. What should I build?
Getting access to OpenAI GPT-3 Codex
Codex, when released first was in Private beta and GPT-3 users are being allowed access on a case by case. Even If you already had access to GPT-3 API, you still needed to separately apply for access.
In order to use OpenAI Codex, you need to first create an account with them and get access to their GPT-3 Playground. Codex is available as one of the models or as they call it engine to be used in the Playground or via their API.
Using OpenAI GPT-3 Codex AI model
You can use OpenAI Codex AI in two ways:
- Using OpenAI Codex from the GPT-3 Playground
- Using Codex from the OpenAI API
Let’s go through them one by one.
Using Codex from the GPT-3 Playground
In order to use codex from the GPT-3 Playground,
- Open GPT-3 Playground in your browser
- From the model dropdown on the right-hand side of the page, select the model as “code-davinci-002”. code-davinci-002 is the current version for Codex
- Write your prompt in the textbox and click submit
GPT-3 Playground filled in for Codex use case (before generation)
GPT-3 Playground filled in for Codex use case (before generation)
Using Codex via OpenAI API
You can also use OpenAI codex via OpenAI API in your code. This enables you to build products that uses Codex functionality in them like code generation or code explanation, etc.
This is an example of an API call to codex API in Python:
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Completion.create( model="code-davinci-002", prompt="# Create a Python dictionary of 6 countries and their capitals\ncountries =", temperature=0, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 )
Now and an example of an API call to OpenAI Codex or code-davinci-002 in Node or JavaScript
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }); const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({ model: "code-davinci-002", prompt: "# Create a Python dictionary of 6 countries and their capitals\ncountries =", temperature: 0, max_tokens: 256, top_p: 1, frequency_penalty: 0, presence_penalty: 0, });
What programming languages does Codex support?
Codex supports the below programming languages.
- Python
- JavaScript
- Bash
- c#
- Java
- Ruby
- Rust
- SQL
- TypeScript
& a few others.
Codex is most proficient at Python but does a good enough job at other programming languages.
Interesting use cases for OpenAI Codex with prompts
Here are some interesting prompts for Codex:
Explain Code using OpenAI Codex
# Python 3 def remove_common_prefix(x, prefix, ws_prefix): x["completion"] = x["completion"].str[len(prefix) :] if ws_prefix:
keep the single whitespace as prefix
x["completion"] = " " + x["completion"] return x
Explanation of what the code does
#
Convert code from one language to another using OpenAI Codex
#JavaScript to Python: JavaScript: dogs = ["bill", "joe", "carl"] car = [] dogs.forEach((dog) { car.push(dog); }); Python:
Write code using OpenAI Codex
""" I want a program that downloads all Twitter followers list in a csv file. """
import tweepy
What’s the difference between Github Copilot vs OpenAI Codex?
Codex is also what drives Copilot, the AI Code writing tool from Github.
Github was the first company that got access to Codex API and they built a VS Code Extension called Copilot that provides the functionality of writing code using plain English right in the VSCode IDE.
Github has announced they are working on building Copilot plugins or extensions for other IDEs like Intellisense, etc.
Where can I learn more about Codex?
- Read OpenAI’s announcement
- Paper for this AI model can be accessed here in pdf format or if you want to read it in the browser itself
- Watch OpenAI’s Live Stream recording
What is possible with Codex?
I see two main uses for Codex
- People use it directly from the OpenAI tool called Playground to help write code for themselves.
- Or people with access to API building Apps and integrating Codex into it and letting others use those Apps.
Here are some cool stuff already built or being built with Codex
- Github Copilot – A VS Extension that helps convert English commands to code.
- Build Web Apps – Using Codex to build full-stack web applications.
- Meme Generator
- Create a Demo tool using Codex
- A Code refactoring tool
- Writing a program to use mailchimp APIs to send emails to 1000s of subscribers
I got access to OpenAI Codex. What should I build?
Here are a few ideas…
- Build Plugins and Extensions for Coding tools and IDEs other than V Code and do what Github Copilot has done for VS Code
- Build Template generators that let users generate new website templates by just typing plain English
- Write highly efficient Code translators.
- Boilerplate generators.
- Build No Code tools whose interface to the user is plain English rather than clicking buttons.
The possibilities are limitless.
Subscribe to my newsletter
Read articles from Harish Garg directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by