How to integrate generative AI into your applications
You can give your apps AI powers with the OpenAI API. Learn how to use web API calls or the OpenAI library to add generative AI to your applications.
Mar 25, 2024 • 9 Minute Read
Every time I check the news or social media, there’s a new article or post about generative AI performing a task that replaces someone’s work. And an important question pops into my mind: “Will AI take my job?”
The answer isn't as straightforward or complex as you might think. Each technological breakthrough has made certain roles irrelevant. For example, being the town blacksmith in the Middle Ages was a vital job—you were in charge of crafting swords and weapons for defense. But the invention of gunpowder eventually led to more efficient weaponry, and blacksmiths are now pretty hard to find.
So, what’s my answer? “AI won’t replace me, but someone using AI might.”
If we want to be future ready and thrive in this new era, we need to embrace generative AI, incorporate GenAI into applications, and make it part of our daily workflow. Let me show you how.
In this post, I share a high-level overview of how to connect your application to the OpenAI API. If you want a deeper dive, check out my course Developing Generative AI Applications with Python and Open AI.
Table of contents
What is text-based generative AI?
In a nutshell, text-based GenAI refers to AI tools that can produce text that looks and feels like a human produced it. You can learn about several of these tools in our post, AI tools your employees need to learn. Or you can ask ChatGPT directly. Check it out.
Besides answering questions, text-based GenAI systems can also create content, transcribe text, translate text into multiple languages, and perform functions like entity extraction, creative writing, data augmentation, code generation, task automation, and knowledge extraction.
Text-based GenAI platforms can help you be more productive, create new content, and perform tasks in new, innovative ways. But you still have to sit in front of a browser, provide a prompt, and receive the output.
What if you take it up a notch: Instead of giving yourself AI superpowers, you give your applications those superpowers by integrating artificial intelligence with an API.
How to add generative AI to your applications with the OpenAI API
Generative AI has powerful features. You can add these features to your applications by allowing the use of natural language as input.
But you can’t do this directly with ChatGPT or another platform. Instead, you need to connect your application to an API, like the OpenAI API. Here’s how.
Before you begin: Get started with the OpenAI API
Before you can connect your app with the OpenAI API, you’ll want to follow these three steps.
Step 1: Get access to the OpenAI API
You’ll find details on how to access the OpenAI API at platform.openai.com. You usually receive an initial credit to get started which will be more than enough to begin testing.
Step 2: Get an API secret key
Your API secret key is how you’ll authenticate on every call to the API. You’re responsible for any usage with your secret key, so safeguard it. Never share it with anyone and never commit it to a source control repository.
Note: If you belong to multiple organizations, you can have keys for each org. This is important as each organization will be billed individually.
Step 3: Decide on functionality
Finally, decide what functionality you’re going to use with your API secret key. The functionality will tell you the endpoint and models you need.
For example, if you’re going to integrate a general-purpose “text in, text out” generative AI into your application, you need the Chat Completions endpoint. This endpoint has several pre-trained models from the GPT-4 and GPT-3.5 model families.
You can choose from several different model families. If you’re tight on time or budget, pick GPT-3.5 Turbo. Why? It works well, it’s great for getting started, and it’s cheaper than GPT-4.
Now that you have access to the platform, a secret key, and a model, it’s time to start coding.
Using web API calls to connect your app with OpenAI’s API
Before you start coding, figure out the coding language your application uses. The OpenAI API supports several languages, or you can make web API calls directly. Let me show you an example of each
Show me the code: Using cURL to test API calls
I recommend using cURL (short for Client URL) as your first test. cURL is a command line tool for transferring data using various network protocols, and it’s easy to use for testing calls to APIs.
This is the command you should use:
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Why is Costa Rica the best travel destination? Answer in two lines."}],
"temperature": 0.7
}'
Note: The cURL command uses $OPEN_AI_KEY, which you need to set as an environment variable. Set it from the System Variables window in Windows or use the export command in Mac.
Once you make the call, the API will return a response like the one shown below. The value you should look at is content. Within message, role should be equal to assistant.
If you’re going to integrate your application using web API calls, you pretty much have to send what’s shown above: Content-Type and Authorization in the header (-H) and the parameters in data (-d). Parse the JSON response, and you’re done.
If parsing JSON responses and creating calls by concatenating (or interpolating) text aren’t your cup of tea, you might like the next option.
Using the OpenAI library to connect your app with the OpenAI API
Web API calls not your thing? Try an OpenAI library. There are several OpenAI libraries available depending on your language of choice. You can use OpenAI-supported libraries, Azure OpenAI client libraries, or community-maintained libraries. For this post, I’ll use the open source Python library, officially maintained by OpenAI and available in pypi.org.
Step 1: Install the OpenAI library
The first step is to use the command below to install the OpenAI library:
pip install openai
Step 2: Provide your key to the OpenAI client
There are several ways to provide your key to the OpenAI client. The easiest one (IMHO) is the python_dotenv library, which reads key-value pairs from a file called .env and sets them as environment variables. Install the python_dotenv library using this command:
pip install python-dotenv
Note: I’m working under the assumption you know how to create Python applications and understand best practices. If you don’t, no big deal. You can keep reading and learning!
Step 3: Create your .env file
The next step is to create a file called .env in the root where you’ll run your application. Use the following format and your key:
OPENAI_API_KEY=<insert-your-key-here>
Step 4: Import the libraries and load your key
Time to start coding. Create a Python file, for example app.py, and import the libraries you’re going to use: openai, os, and python_dotenv.
import os
from openai import OpenAI
from dotenv import load_dotenv
Add the following statement to load the key from the .env file and set it as an environment variable:
load_dotenv()
Step 5: Create the client
This step requires you to set the OPENAI_API_KEY environment variable to an API key, which you did in the previous statement. If it’s not set, an error will occur. When you create the client, the key’s validity isn’t checked. Validation takes place when the endpoint is called.
client = OpenAI()
Step 6: Make the call to the ChatCompletions endpoint
Now comes the most important step: making the call to the ChatCompletions endpoint, which you do by calling openai.ChatCompletion.create. Here’s the call you need to make with its respective parameters:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user","content": “What is one of the best things about Costa Rica that makes it great?”}],
temperature=0.7
)
Let me explain what each parameter means.
model: This parameter indicates which of the available models you’d like to use to process your request. Like I mentioned earlier, GPT-3.5 Turbo is the best one to start with because of the price and its optimization for the ChatCompletions endpoint.
messages: This parameter points to an array of objects that contains the exchange between you (or your application) and the large language model (LLM). Because the API is stateless, you need to include all messages in a conversation so the LLM can follow along and keep answering your requests with greater context.
Each message is made up of a role and content:
role: Corresponds to who or what is sending a message. There are several options:
user: The one making the request (you or your application)
assistant: Response from the LLM
system: Instructions provided to the LLM
content: The text sent to the LLM or received from the LLM
A content from user is the prompt
A content from assistant is the output
A content with system are the instructions to the LLM; for example, “You are a useful assistant who speaks like a pirate”
temperature: This parameter uses a range of 0 – 2 to indicate to the LLM how consistent or creative/random its answers should be. The value 0 is deterministic and 2 gets (slightly too) creative.
Step 7: Check the call’s response
The response received from the call is an object that has the following structure in JSON:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "The people! Costa Rica is great because of the ticos and ticas! Pura vida mae!",
"role": "assistant"
},
"logprobs": null
}
],
"created": 1677664795,
"id": "chatcmpl-7QyqpwdfhqwajicIEznoc6Q47XAyW",
"model": "gpt-3.5-turbo-0613",
"object": "chat.completion",
"usage": {
"completion_tokens": 17,
"prompt_tokens": 57,
"total_tokens": 74
}
}
The response contains several important values.
finish_reason: Why the model returned the output. Reasons include everything from it finished generating the entire response to it had to stop because of length constraints.
model: Which exact model was used. You may have requested GPT-3.5 Turbo, but the exact model version used was gpt-3.5-turbo-0613.
message: The response from the LLM to your prompt.
Additional details are included, like date, type of response object, and usage—extremely important if you want to control API consumption.
Step 8: Make a loop and start a conversation
Extract the content below from the response and send it back to your user, save it, or use it however you’d like.
answer = completion.choices[0].message.content
print(answer)
Now you can make a loop and start a conversation.
With ChatCompletions, it’s your responsibility to manage the history thread. This isn’t necessary with AI assistants that have a “thread” that stores your conversation. But that’s a different blog post and course.
Build epic applications with GenAI
Now that you know how to integrate generative AI superpowers into your application, you no longer need to code instructions to respond to user requests.
For example, if you have a travel application, a user doesn’t need to perform a search and refine the results. Instead, they can make a request like this: “Find me a hotel in Costa Rica, one weekend in March, close to the beach, for 2 adults and 2 children, for less than $150 a day.”
Wow! This is a huge improvement. I hope you’ll use what you’ve learned to build epic applications with GenAI superpowers.
Want to dig deeper? Try these courses: