Skip to content

GPT-4 API Ultimate Guide With Examples (All Scripts Included)

OpenAI’s GPT-4 is the latest version of their text generation model.

By utilizing their API, you can access the power of GPT-4 in your own Python applications.

In this post, we will cover the following:

  • Obtaining API Access
  • Installing Required Libraries
  • Making Your First API Call
  • Fundamentals of GPT-4 Programming
  • Streamed Completions

Obtaining API Access

To use any of OpenAI’s models, you must first obtain an API access key.

Go to and click on the “Signup” button.

You will be prompted to create an account. Verify your email to complete the signup process.

Once logged in, you can find your API keys under the account dashboard.

gpt-4 api key

Be sure to keep your keys private, as anyone with access can use your OpenAI quota.

Installing Required Libraries

To use the OpenAI API from Python, you need to install the OpenAI Python library. This can easily be done using pip:

pip install openai

This will download and install the required package.

You also need to import the openai module at the top of your Python code:

import openai

Making Your First API Call

Once you have obtained your API key and installed the library, you can make your first API call.

First, load your key into the Python environment:

openai.api_key = "YOUR_API_KEY" 

Replace YOUR_API_KEY with your actual API secret key.

Now you can use the various OpenAI classes and functions to call the API. GPT-4 is accessed through the Completion endpoint.

Let’s try a simple text completion example:

response = openai.ChatCompletion.create(
        model="gpt-4", messages=[{"role": "user", "content": "Generate a 3 sentence story about friendship"}]

This will prompt GPT-4 to generate text based on your prompt.

The response will contain the generated text, along with other metadata. To access just the text, use:


Here is the full script. Try it directly here:

This should give you a basic API call workflow. From here, you can start experimenting with text generation using GPT-4!

Fundamentals of GPT-4 Programming

Now that you can make basic calls to the GPT-4 API, let’s go over some core programming concepts to help you generate high-quality responses for your applications.

GPT-4 exposes a number of parameters you can tweak to control the nature of generated responses:

  • Temperature: Controls randomness, high values mean more random.
  • Top_p: Controls likelihood of unlikely tokens.
  • Frequency_penalty: Reduces repetitive phrases.
  • Presence_penalty: Penalizes new tokens based on whether they appear in the prompt.

It takes experimentation to find the right balance for high-quality outputs. and this is your role as a prompt engineer, To Experiment!

The way you craft the prompt plays a major role in the API’s response. Here are some best practices:

  • Provide examples of desired tone/formatting
  • Simplify complex prompts into smaller steps
  • Use clear, concrete language
  • Prime GPT-4 with some initial chat to settle into conversational mode

Taking the time to engineer quality prompts will dramatically improve results.

Anyway, let’s see an example of how to use these parameters in our Python scripts:

import openai

openai.api_key = "sk-XXX"

response = openai.ChatCompletion.create(
          "role": "user",
          "content": "Generate a 3 sentence story about friendship"


You can also generate multiple results by using the “n” parameter, here is an example you can try out:

Streamed Completions

For long responses, use the stream Parameter to process the output incrementally:

Try out this:

Script Explained:

  1. Chat Creation: The openai.ChatCompletion.create the function is called with several arguments:
    • model="gpt-4" Specifies that the GPT-4 model should be used.
    • stream=True Indicates that the response should be streamed (i.e., sent in parts as they become available instead of all at once when the computation is finished).
  2. Processing the Response: The response from the OpenAI API is an iterable (due to stream=True), so you can iterate over it with a for loop (for chunk in response). In each iteration, chunk represents a piece of the response.
  3. Checking and Printing the Content: Within the loop, it checks if the ‘content’ field exists in chunk.choices[0].delta. If such a field exists, it prints its value with print(chunk.choices[0].delta.content,end=""). The end="" argument tells the print function to not add a newline at the end, so consecutive prints will appear on the same line.

This streaming option enables applications like live transcriptions. Or give you the effect as a ChatGPT Typewriter. More details can be found here.

πŸŸ₯Master the Most In-Demand Skill of the Future!

Enroll in the ‘Become a Prompt Engineer‘ program. We’ll take you from novice to expert in scripting AI workflows. Start your journey here!

What Will You Get?

  • Access to our premium Prompt Engineering Course
  • Access our private support forum to get help along your journey.
  • Access to our Premium Tested Prompts Library.

Hasan Aboul Hasan

Digital Entrepreneur with 8+ years' experience, Founder & Developer of PromoterKit. Helping Digital Marketers to achieve 10X results using AI and Automation.

2 thoughts on “GPT-4 API Ultimate Guide With Examples (All Scripts Included)”

Leave a Reply

Your email address will not be published. Required fields are marked *

Take Your Digital Marketing Skills To The Next Level

Β© 2023 LearnWithHasan | All Rights Reserved.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

πŸ”΄ New Series Started: Programming With AI - RJP Technique
This is default text for notification bar