Build Advanced Machine Learning Models With OpenAI’s API

Transition towards machine learning with a few lines of code

Ahmad Albarqawi
Better Programming
Published in
4 min readMar 26, 2022

scalability — designed image

OpenAI released an API that allows researchers and developers to build advanced GPT 3 machine learning models with few lines of code based on the paper “Language Models are Few-Shot Learners”.

The API has an effortless setup to start interacting with the state-of-the-art GPT3 model. It is scalable to advanced users to fine-tune the models based on business use cases or the user’s custom language.

Let’s build a model to answer NASA solar system facts

source: nasa.gov — license in references

I tried to build a machine learning model using GPT3 to answer questions from NASA website about “10 Need-to-Know Things About the Solar System”. The Model results were exciting, and this blog will work with you to build step by step advanced question answering model.

An example of the model exciting results: I asked the model how many planets in our solar system, and it managed to return “8” by looking at this paragraph: “Our solar system is made up of a star, eight planets, and countless smaller bodies such as dwarf planets, asteroids, and comets”.

Although it might look like intuitive information, the model learned the answer by itself from the given paragraph, which includes multiple details.

When asked the model how many years to complete an orbit around galactic? it returned “230 million years” from this paragraph:

It takes our solar system about 230 million years to complete one orbit around the galactic center.

This is the link for the target NASA article. I updated the blog on October 2022 to cope with the latest openai API changes.

Open AI Setup

To start using the Open AI model, go to this website and create an account:

Get the API key to start building models, the “key” available under your profile/view API keys.

View API Keys

The API is available using python or node.js; this article will focus on the python code. To download Open AI using python run:

pip install openai

The Parameters

There is no need to have a deep knowledge of model building to interact with Open AI GPT 3 — the API democratizes the model for everyone to allow the interaction using basic parameters. However, it comes with fine-tuning capabilities for data science experts.

Model type

First, you need to select your model type; the API comes with four engines:

ada, babbage, curie, or davinci

The engines ordered from ada, the fastest and least costly model, to the most advanced model, davinci.

For more details: https://beta.openai.com/docs/engines/gpt-3

Other parameters for completion API:

open AI completion parameters

Full code example to build the solar system answers model

source: unsplash.com

Import openai and set the key value:

import openai
openai.api_key = "enter_your_key"

Prepare the input template file that includes the context paragraph for the model to answer your questions; using files will make it easier to format the lines and draft the examples:

file — example of the model input

Load the input file using python:

with open('answer_ahmadai_template.txt') as f:
prompt_input = ''.join(f.readlines())

Call the openai completion API and store the response:

code — to call openai question answering

Now you can print the returned answer as follows:

response["choices"][0]["text"]

Printed response: ‘8’

Parameters explanation:

  • model: the engine for your answering model.
  • prompt: the input text including the documents and the question.
  • max_tokens: max tokens to generate the answers.
  • temperature: higher values means the model will take more risks
  • stop: mark the encoding to not contain in the response.

Enhancement

The input prompt for the model is limited, and you cannot send extended context paragraphs to provide answers, like full website content.
To adapt to this limitation, you have to apply a semantic search over your database and return all the content that is more similar to the user question. Then you can send the selected n content to openAI with your question to provide the answer.

Summary

Open AI augments your business using advanced machine learning models; you can start building ML models using the basic setup then advance the models with the custom parameters and engine options.

By default, the API uses the standard GPT3 model and data. For competitive advantage, upload your custom data as a file and use the fine-tuning feature; this is helpful if you have unique data for your business or language. The follow-up article will focus more on the fine-tuning code and results.

I prefer to interact directly with the model, not through an API. However, OpenAI decided not to release the model itself due to safety concerns. The released API looks flexible enough to build many fabulous apps.

References

Quotation: NASA content (images, videos, audio, etc) is generally not copyrighted and may be used for educational or informational purposes without needing explicit permissions.

Ahmad Albarqawi
Ahmad Albarqawi

Written by Ahmad Albarqawi

Master’s data science scholar at UIUC. ahmadai.com

No responses yet

What are your thoughts?