Create AI application in minutes with OpenAI API
In this tutorial, we will go though getting started with OpenAI API to create an AI powered application in minutes, without the need to know AI programming, models or mathematics.
OpenAI used to have a vetting and approval process, that I never had the time to go through — So I would procrastinate trying it out. They have not changed that, today you can go to their website and immediately register an account, get your API keys and get started with a free trial.
Get started: https://openai.com/blog/openai-api/
What is OpenAI
OpenAI is an organisation initially created and funded by Elon Musk and other investors in 2015 as a non-profit organisation that would make access to and use of AI tools more accesible and open sourced. The company later changed to for-profit in 2019 and partnered with Microsoft.
In 2020 OpenAI announced GPT-3, a language model capable of generating text, classifying text, summarising information and much more — the core models behind the current OpenAI AI — we will be looking in to in this tutorial.
OpenAI describes itself as a commercial Artificial Intelligence(AI) API that provides general purpose “text-in, text-out” tool. So anyone can register on their website, and start getting access to powerful already trained (With trillions of text data from the internet) AI models that can do the following:
- Automatic text completion (use case for blog writing, content production)
- Text translation
- Text classification
- Idea generation (for online content)
Current Use Cases for OpenAI API
Market insights analysis
You can also build real-time tweet classification engine, to follow trends and market insights in real time.
OpenAI Models and Free Trial
When you get started with OpenAI, you get a $18 credit and an API key so you can immediately start to work with the API. Before starting, it is important to understand the different models provided for by the API — as they have different capabilities, response times and costs associated with them.
The Davinci Model is the most capable and expensive, as you go down the list the models are cheaper, faster but not as capable. Noe every model can do all the asks you need. For example, the cheapest Ada -cannot generate text ideas, its great for classification tasks. If you wanted to for example “generate blog ideas” based on some description, you would need to use Davinci.
The figure above gives you a better idea of the cost based on your credits or what they call tokens.
Hello World of OpenAI — Generate Blog Topics
Now that you have your API keys and understand the pricing, you are ready for your first API call, for this we will use Python.
Start by installing the library, within your virtual environment:
pip install openai
Create a config file with your API keys, or store them in the ENV.
The code will look like:
import osimport openaiimport configopenai.api_key = config.OPENAI_API_KEYresponse = openai.Completion.create(engine="davinci",prompt="Blog topics dealing with daily life living on Mars\n\n1.",temperature=0.3,max_tokens=64,top_p=1,frequency_penalty=0.5,presence_penalty=0)print(response)
This call is asking the API to return blog topics on the subject: “dealing with daily life living on Mars” and provides the max_tokens to be returned. Its using the davinci engine — model.
Run this code and check the response.
Youtube Video OpenAI API Application
Check out the full video on Youtube below: