Exploring OpenAI APIs: GPT-4o-mini Basic Demo

Whilst my last hands on post about working with Open AI offline to avoid API costs is great, not everyone wants to have the overhead of setting up a local machine like that. So this post is for those who want to get started with Open AI Apis.

Setting Up

Sign Up for an API Key

First off, sign up for an OpenAI account here. Its important to note that this is not the same as the ChatGPT Pro licence, so even if youre already paying for it, you will need to set up billing here too. You do need to have an authenticated card, so a small payment is needed to get proper access to the APIs. This isnt a bad thing, as prepaid credit (turn off top ups) means you cant accidentally run up a big bill. So just make sure you put enough in, 5-10- dollars is good for most people playing around and then make sure auto top ups are turned OFF.

Once you’re set, head over to the API section to generate your API key, which you’ll use to authenticate API requests. Any keys generated before the payment was made wont work properly, so regenerate them.

Prerequisites

To work through this, you will need the following pre-installed. Some prior knowledge of working with them is expected but if you’re new and get stuck just google the line and there should be something on google to help you over it.

  1. Python 3.7 or higher installed.
  2. An IDE, VS Code, PyCharm or similar

Install OpenAI’s Python Package

In your IDE, make a new folder and file called openai-poc.py. OpenAI provides a Python package that makes working with their API super easy. You can install it using pip:

pip install openai

And just like that, you’re ready to start coding!

Step 2: Basic Example with GPT-4-mini

Let’s start simple—using GPT-4o-mini to generate text based on a prompt. GPT-4-mini according to OpenAI is ‘Our affordable and intelligent small model for fast, lightweight tasks. GPT-4o mini is cheaper and more capable than GPT-3.5 Turbo.’ Cost effective is great when experimenting, so lets go for that.

Generating Text

Here’s a basic example where we ask GPT-40-mini to explain AI in simple terms:

from openai import OpenAI
import os

# Use your API key insert obligatory dont do this warning here, this is just for demo
os.environ["OPENAI_API_KEY"] =  "your-api-key-here"
client = OpenAI()

# Provide a prompt
prompt = "Explain what artificial intelligence is in simple terms."

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": prompt}
    ]
)

# Print the response
print(completion.choices[0].message)

This script sends a prompt to OpenAI and returns the AI’s response. Adjust the max_tokens parameter to control how long the output is.

Tweaking Parameters for Better Results

You can fine-tune how creative or focused the model is by playing with parameters:

  • temperature: A lower value (like 0.2) keeps responses predictable, while a higher value (up to 1.0) makes them more creative and random.
  • max_tokens: Limits how much text the AI generates.
  • top_p: This controls diversity. Lower values will keep the output more consistent.

Its recomended you change temperature or p but not both at once so that you can better monitor the change that is created.

For example, to make the text more creative:

completion2 = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": prompt, "temperature" : 0.7, "max_tokens" : 150, "top_p" :0.95}
    ]
)

# Print the response
print(completion2.choices[0].message)

More Advanced Uses

Summarizing Text

One of the GPT super useful features is summarization. Let’s say you have a long piece of text and want to get a quick summary.

summary_prompt = (
    "Summarize the following text: "
    "Artificial intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to think like humans and mimic their actions."
)

completion3 = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": summary_prompt}
    ]
)

print(completion3.choices[0].message)

This comes in handy when you’re dealing with large blocks of text and need to extract key points quickly. I appreciate this example is not ideal as the text is short to begin with! But heck, saves on tokens and tokens cost money.

Translation

The API is also great for translating text between languages. Here’s an example of translating English into French:

# transaltion example 
translation_prompt = "Translate this sentence into French: 'How are you doing today?'"


completion4 = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": translation_prompt}
    ]
)

print(completion4.choices[0].message)

Simple and effective for multilingual applications.

You will notice that the code here is largely the same other than the tweaking parameters we can control. The majority of this is being done within the prompt itself, which is prompt engineering. A huge topic for another day.

Tips and Common Pitfalls

Now, let’s talk about a few things that can trip you up when working with OpenAI.

Watch Out for Rate Limits

OpenAI enforces limits on how many requests you can make per minute. If you hit those limits, you’ll get a 429 error. To avoid this, make sure you spread out your requests and use exponential backoff if necessary.

Handling Long Texts

GPT-40-mini has a token limit (around 120k tokens input, 16k output). This is alot higher than the GPT 3 models which were only about 4k! But if you’re working with large texts, you may need to break them into smaller chunks or summarize them before processing. This is a future topic on vectordbs, so watch out for that blog in future.

Monitor Your Costs

Every request to OpenAI’s API uses tokens, and these can add up quickly, especially when using larger models like text-davinci-003. Keep an eye on your usage through OpenAI’s dashboard and consider switching to smaller models (like text-curie-001) for tasks that don’t need as much complexity.

Be Mindful of Bias

GPT-5o-mini, like other AI models, can reflect biases present in its training data. Be cautious when using the model for sensitive tasks, and consider adding checks or filters to ensure your outputs are neutral and fair.

Caching Repeated Responses

If you’re making requests for the same information (e.g., an FAQ bot), consider caching the responses to avoid unnecessary API calls. This will save on costs alot.

Wrapping Up

Getting started with OpenAI is a great way to bring AI into your projects, whether you’re building smart assistants, chatbots, or content generators. With just a little setup, you can start generating playing around with prompts and start to make real the ideas in your minds eye!

Happy coding!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.