There are many ADOs in Enterprise about AI, but to make it a more efficient and productive developer, you need to learn how to make the AI model do what you want.
This tutorial explains how to use the Openai API to synthesize information from different models into a single useful response. To achieve this, work on the following tasks:
- Ask the AI model to compile and return a list of animals mentioned in William Shakespeare play Othello and Winter Stories.
- It incorporates that response data into a second prompt in another AI model, creating illustrations with animals in the list, and creating selected graphic rendering styles such as science fiction and realism.
All the concepts and techniques discussed are demonstrated in the node.js project stored in the GitHub repository.
First, we'll start by providing a quick overview of how to operate the OpenAI API.
Working with the Openai API
The OpenAI API provides artificial intelligence services to developers at the programming level. Developers can interact with OpenAI APIs in their code just as End users interact with ChatGPT websites published by OpenAI.
The OpenAI API allows developers to perform many tasks, including natural language processing, language translation, code generation and completion, and image generation. In this article, we use the OpenAI API to investigate specific data and generate images based on the results of that study.
Figure 1 shows the workflow for the demonstration use case described in this tutorial.

The first step to using the OpenAI API is to register with the website, provide profile information, and receive an API key that allows your code to access the API.

The Openai API is not a free service. You must provide your credit card information as part of the setup process. It is billed according to the volume and complexity of calls made to the API. However, the cost of experimenting with the OpenAI API is minimal. A typical experiment requires a small investment of around $10.

Once you have obtained your API key and provided your payment information, you are ready to program using various models that are accessible via the OpenAI API.
Use AI models
As mentioned earlier, developers use OpenAI to write code using one or more AI models provided by the API. For example, the GPT series model supports language processing, so developers can use it to execute and execute instructions in natural language formats such as “Show the total number 2 plus 2”. The DALL-E model is used to generate images, while the Codex model is used to perform code completion.
Developers use the OpenAI API via language-specific libraries and SDKs. When you install the OpenAI API library in your programming environment, you select the specific model to use for a particular task. Figure 5 shows some of the models available in the node.js openai npm package.

The demo code explained in the next section uses two models. One is the GPT-4 turbo, What to use to discover animals mentioned in plays Othello and Winter Stories. The other model is Dall-E 3, which generates images containing animals in the list. Use both models in the chain.
Programming using a model chain
The code block in Figure 5 shows the node.js code for the function generateImage(selectedStyle). This function encapsulates all the logic that tells the Openai model to discover a list of animals mentioned in Shakespeare plays and generate images containing those animals. (Again, all the code for this is found in the GitHub repository mentioned above.)
Note that calls to the Openai API models are chained. The first two calls gpt-4-turbo The model is line 17, and line 27 to acquire the animals in the play and convert information in JSON format into a simple animal list. Calling on line 40 dall-e-3 A model that generates images containing those animals according to a specific graphical style. The response is a URL within the OpenAI ecosystem that references the generated image.
Also note that calls to the model use natural language prompts created with lines 13, 24, and 37. An important feature of OpenaiAPI is its acceptance of programming statements expressed as natural language. This new approach to software development will have a great deal of implications as the paradigm matures.

Finally, note that the prompt for a particular call to the OpenaI API is declared within the JSON object passed as a parameter for the specific API method. The configuration of the JSON object depends on the API function that is invoked. For example, this call declares the prompt as the value of lines 17-21 messages JSON parameter properties:
let response = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages: [{"role": "user", "content": query_1}],
max_tokens: 300
});
Use lines 40-45 prompt Properties to declare prompts:
const imageResponse = await openai.images.generate({
model: 'dall-e-3',
prompt: imagePrompt,
n: 1,
size: "1024x1024"
})
Also note that the model in which each API call is used is declared in the model properties.
Run the code
The demo code uses the NPM package Readline to facilitate console interaction with applications in the terminal window. Below is a view of the first console interaction: Ask the user to declare the graphic style for the generated image.
Please select the image style according to the number.
1. Science Fiction
2. anime
3. Fantasy
4. summary
5. realism
Selected (1-5): 1
You chose science fiction and generated images…
Next is the result of urging the GPT-4 turbo model using the OpenAI API.
The animals are as follows
This is a list of all the animals mentioned in the plays *othello * and *Winter Tales *.
**Othello:**
1. horse
2. cat
3. goat
4. Monkey
5. Wolves
6. Bear
7. Flies
8. Toad
**Winter Story:**
1. sheep
2. lamb
3. Bear
4. Camel
5. deer
6. fish
7. Camelot (Note: “Camelot” is known as a castle associated with King Arthur rather than an animal, so it may be a misunderstanding. This could be an error in the original data.)
8. Sparrow
9. Lark
10. Crow
11. calf
12. OX
13. Luke
14. ULE (Note: “ule” can be a typo or transcription error unless it refers to a specific context within a generally unknown play. More generally, this may refer to an owl.)
This comprehensive list from JSON objects details the drawn animals and adds theme layering and symbolism within these Shakespeare plays.
This is the result of the prompt dall-e-3 Models that create images based on selected graphical styles and list of animals discovered in the play Othello and Winter Stories:
Get the generated image at the following URL:
https://oaidallapipprodscus.blob.core.windows.net/private/org-d0p2cqbjhbf4smwv5zs0gedo/user-bbileyffniwbn86v1mp6nml0/i MG-KTETHFD71ARKA6GRP3MERAKI.PNG? ST = 2024-07-28T15%3A48%3A18Z & SE = 2024-07-28T17%3A48%3A18Z & SP = R&SV = 2023-11-03 & SR = B&RSCD = in line & rsct = image/png & skoid = 6aaadede-4fb3-4698-a8f6-684d7786b067 & sktid = a48cca56-e6da-484e-a814-9c849652bcb3 & skt = 2024-07-1 27T22%3A34%3A35Z & SKE = 2024-07-28T22%3A34%3A35Z & SKS = B&SKV = 2023-11-03 & SIG = X4BG6202MIYR05JQR5UL7LXP0TD%2BB35F17KR3QNU5E4%3D
The URL published by the Openai API is temporary and expires in a few hours. To make the image last longer, save it on your local machine.
And finally, what's certainly important is Figure 6 is a diagram of a list of data rendered in science fiction style.

Openai API plus and minus
Working with the Openai API is easy. Accessing the API requires little minimum setup time and out-of-pocket costs.
However, programming using the OpenAI API is incomplete. At this point, the results of complex prompts are not deterministic. This means that the same complex prompts return different results at different times. For example, the prompt “Show 2 plus 2 total” will always return four, but please see all the animals mentioned in Shakespeare Othello and Winter Stories“This makes sense given the mechanism of rapid processing, and this makes sense.
However, keep in mind that AI technology is still mature. In the early days of speech recognition, users trained recognition engines to recognize the voice of a particular user, but today's speech recognition engines converted audio spoken by native speakers into text without training. In general, we can expect the same level of improvement in artificial intelligence technologies, and in particular Openai API.
The Openai API opens up scores for new possibilities in software development. The power of natural language interaction, along with the range of intelligence each model offers, enables developers to quickly create versatility and powerful applications. Checking AI will help you adjust the models together and increase the power of such applications.
The Openai API is a game changer that places the power of artificial intelligence at every developer's fingertips. This opens up a new path to computer programming that dramatically benefits developers and the developers they serve.
Bob Reselman is a software developer, system architect and writer. His expertise ranges from software development techniques to technology and culture.
