Fork me on GitHub

Project Notes

#308 OpenAI with Python

The basics of using ChatGPT/OpenAI APIs with Python

Notes

Ironically, I asked AI to write some OpenAI examples for python .. and it gave me outdated code that no longer works!

Here’s a quick outline and example of using the openai-python library to make basic Open API calls.

Installation

This example uses the openai python client. Install with pip:

pip install -r requirements.txt

To use the API, an API key is required from https://platform.openai.com/settings/organization/api-keys. Set the defautl environement variable for the following examples to work:

export OPENAI_API_KEY='your-key'

Once installed, the library actually provides a command line tool that can be used to make one-off queries, for example:

$ openai api chat.completions.create -m gpt-4o-mini -g user "Hello, who are you?"
Hello! I’m an AI language model created by OpenAI. I'm here to help answer your questions, provide information, and assist you with a variety of topics. How can I help you today?

Python Usage

The openai-python library provides easy access to the OpenAI aPI. Here’s a basic example:

Create a client with optional configuration details. By default, will use API key from OPENAI_API_KEY environment variable

client = OpenAI()

Call the chat completion API with a prompt, and selected model:

response = client.chat.completions.create(
  messages=[
      {
          "role": "user",
          "content": "Hello, who are you?",
      }
  ],
  model="gpt-4o-mini"
)

Parse the response and do something with it.

return response.choices[0].message.content.strip()

Note: response data will follow the JSON schema of the API being called. In this case a chat completion object.

Example Run

The chatgpt_query.py is a simple wrapper around the API calls:

$ ./chatgpt_query.py "hello world"
Hello! How can I assist you today?

Credits and References

About LCK#308 pythonAI

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader