GPT-4 API: Hands-on guide

GPT-4 API Hands-on guide (1)

In this guide, I show how to start using GPT-4 API.

Get Access to GPT-4 API

First, you need to get access. Follow this guide to get access to GPT-4 API.

Once, you have access, grab your OpenAI API Key.

Call GPT-4 API from Python code

First, install openai python package using the command

pip install openai

Next, open a python file and type in the below code.

import openai
openai.api_key = "your openai api key"
user_prompt = "your prompt here"

openai.ChatCompletion.create(
   model="gpt-4",
   messages=[
      {"role": "user", "content": user_prompt}
   ]
)
print(response.choices[0]["message"]["content"])

Make sure to replace the below things in your code before proceeding further:

  • “your openai API key” with your openai API key
  • “your prompt here” with a prompt on what you want gpt-4 to do for you. Save the .py file

Next, run the python program from the command like this:

python your_python_file_name.py

It will print out the response from the OpenAI GPT-4 API in your command line program.

You could also use the same code in a Google Colab or a Jupyter Notebook.

This was a very basic example of calling GPT-4 API from your python code. I will be posting more examples on advanced use cases. Check back again soon.