Using OpenAI GPT

I use frequently use the OpenAI APIs in my work. In this chapter we use the GPT-5 API since it works well for our examples.

OpenAI Text Completion API

OpenAI GPT (Generative Pre-trained Transformer) models like gpt-4o, gpt-4o-mini, and gpt-5 are advanced language processing models developed by OpenAI. There are three general classes of OpenAI API services:

  • GPT which performs a variety of natural language tasks.
  • Codex which translates natural language to code.
  • DALL·E which creates and edits original images.

GPT-5 is capable of generating human-like text, completing tasks such as language translation, summarization, and question answering, and much more.

Overall, the OpenAI APIs provide a powerful and easy-to-use tool for developers to integrate advanced language processing capabilities into their applications, and can be a game changer for developers looking to add natural language processing capabilities to their projects.

The following examples are derived from the official set of cookbook examples at https://github.com/openai/openai-cookbook. The first example calls the OpenAI gpt-4o-mini Completion API with a sample of input text and the model completes the text.

Here is a listing or the source file openai/text_completion.hy:

 1 (import os)
 2 (import openai)
 3 
 4 (setv openai.api_key (os.environ.get "OPENAI_KEY"))
 5 
 6 (setv client (openai.OpenAI))
 7 
 8 (defn completion [query] ; return a Completion object
 9   (setv
10     completion
11     (client.chat.completions.create
12       :model "gpt-5"
13       :messages
14       [{"role" "user"
15         "content" query
16         }]))
17   (print completion)
18   (get completion.choices 0))
19 
20 (setv x (completion "how to fix leaky faucet?"))
21 
22 (print x.message.content)

Every time you run this example you get different output. Here is one example run (output truncated for brevity):

 1 Fixing a leaky faucet can be a straightforward process, and you can often do it your\
 2 self with some basic tools. Here’s a step-by-step guide:
 3 
 4 ### Tools and Materials Needed:
 5 - Adjustable wrench
 6 - Screwdriver (flathead or Phillips, depending on your faucet)
 7 - Replacement parts (O-rings, washers, or cartridge, depending on your faucet type)
 8 - Plumber's grease
 9 - Towel or rag
10 
11 ### Steps to Fix a Leaky Faucet:
12 
13 1. **Turn Off the Water Supply**:
14    - Look for shut-off valves under the sink and turn them clockwise to close. If th\
15 ere are no shut-off valves, you may need to turn off the main water supply to your h\
16 ome.
17 
18 2. **Drain the Faucet**:
19    - Open the faucet to let any remaining water drain out.
20 
21 etc.