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 yourself with some basic tools. Here’s a step-by-step guide:
2
3 ### Tools and Materials Needed:
4 - Adjustable wrench
5 - Screwdriver (flathead or Phillips, depending on your faucet)
6 - Replacement parts (O-rings, washers, or cartridge, depending on your faucet type)
7 - Plumber's grease
8 - Towel or rag
9
10 ### Steps to Fix a Leaky Faucet:
11
12 1. **Turn Off the Water Supply**:
13 - Look for shut-off valves under the sink and turn them clockwise to close. If there are no shut-off valves, you may need to turn off the main water supply to your home.
14
15 2. **Drain the Faucet**:
16 - Open the faucet to let any remaining water drain out.
17
18 etc.
Optional Practice Problems
- System Persona: Modify text_completion.hy to accept a system prompt instructing the model to adopt a specific persona (for example, a Lisp developer explaining Python code). You will need to add a message dictionary with the
"role" "system"at the beginning of the:messageslist in the completion function. - Configurable Parameters: Update the completion function signature to support optional parameters or keyword arguments like
temperature(default0.7) andmax-tokens(default150), and pass them to the underlying call toclient.chat.completions.create. - Interactive Chat Loop: Implement an interactive loop in text_completion.hy using
whileandinputso that the user can ask questions iteratively from the terminal until they type “exit” or “quit”.