An API (Application Programming Interface) is a set of rules and tools that allows different software applications to communicate with each other. It acts like a messenger that takes your request, tells a system what to do, and then returns the result back to you.
APIs follow a basic cycle:
These are two common API architectures:
APIs rely on HTTP methods to perform operations:
When an API responds, it includes an HTTP status code that indicates the result:
APIs are everywhere. Some common examples include:
APIs are the backbone of modern web and mobile development. They allow different apps to talk to each other, share data, and perform actions securely and efficiently. Understanding how they work opens the door to integrating rich features and external services into your own projects.
OpenAI is an artificial intelligence research and deployment company whose mission is to ensure that artificial general intelligence (AGI) benefits all of humanity. OpenAI is known for creating powerful AI tools and models that help solve real-world problems and empower developers, educators, businesses, and researchers.
Some of their most popular products include:
The OpenAI API is a cloud-based interface that allows developers to access the power of OpenAI’s models via a simple HTTP request. Using the API, you can:
ChatGPT is a conversational AI that can answer questions, write essays, summarize content, and engage in multi-turn dialogue. GPT-3.5 is fast and capable, while GPT-4 is more advanced in logic and understanding. It’s ideal for building chatbots, tutors, assistants, and more.
DALL·E can turn plain text into images. For example, you can type “a futuristic city floating in the sky,” and DALL·E will generate a unique picture. It’s popular in design, marketing, and creative projects.
Whisper is a powerful speech-to-text engine that transcribes audio into written language. It supports multiple languages and works well with real-world audio, including background noise.
Codex is a model trained to understand and generate computer code. It powers GitHub Copilot and can help you:
OpenAI provides cutting-edge models and APIs that enable machines to read, write, listen, speak, and draw. By learning how to use these tools, you can build AI-powered applications that are creative, interactive, and smart.
To use OpenAI services, start by creating a free account on the OpenAI platform. You'll need:
After signing in, you'll land on the OpenAI dashboard. Key sections include:
To use OpenAI in your code, you’ll need an API key:
OpenAI offers a free credit (usually $5–$18) to new users. Once it’s used up, you can switch to a paid tier. The differences include:
OpenAI pricing is based on:
To begin working with OpenAI, you'll need an account and API key. Make sure to understand your usage limits and protect your API key. Once set up, you're ready to start building with powerful AI tools!
Before using the API, install the OpenAI Python package using pip:
# In your terminal or command prompt:
pip install openai
This gives you access to OpenAI's API functions via Python.
After installing the SDK, you need to set your API key securely:
import openai # Import the OpenAI library
openai.api_key = "your-api-key-here" # Replace with your actual key
Tip: It's better to store your key in an environment variable:
# Set it in your environment (one time):
export OPENAI_API_KEY=your-api-key
# Then use it in Python:
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
Here’s how to call the GPT model using the `chat/completions` endpoint:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # Model name
messages=[
{"role": "user", "content": "Tell me a joke!"}
]
)
print(response["choices"][0]["message"]["content"]) # Output the response
This sends your message and gets a reply like ChatGPT does.
Responses are returned as JSON-like dictionaries. You can access fields like this:
message = response["choices"][0]["message"]["content"]
print("ChatGPT says:", message)
You can also loop through tokens or handle streaming (advanced usage).
OpenAI applies rate limits based on your plan. Always handle errors gracefully:
import openai
import time
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response["choices"][0]["message"]["content"])
except openai.error.RateLimitError:
print("Too many requests — waiting to retry...")
time.sleep(5) # Wait and try again
except openai.error.OpenAIError as e:
print("API error:", str(e))
In this chapter, you installed the OpenAI Python SDK, authenticated using your API key, and made your first call to ChatGPT. You also learned how to handle responses and basic errors. Now you're ready to build apps using AI!
Completion
endpoint. Powerful but not conversational.Chat-based models like gpt-3.5-turbo
use a list of messages to create a conversation:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What's the capital of France?"}
]
)
print(response["choices"][0]["message"]["content"])
Controls randomness. Lower = more focused, higher = more creative.
temperature=0.2 # Deterministic
temperature=0.9 # Creative
Limits how long the response can be (in tokens).
max_tokens=50 # Short answer
An alternative to temperature. Uses nucleus sampling (keep top X% likely words).
top_p=0.95
Defines where the generation should stop automatically.
stop=["\nHuman:", "\nAI:"] # Stops when those tokens appear
Example: Hello!
is 1 token, but This is a long sentence.
might be 6–8 tokens.
You now understand the key models, how to structure a conversation with the API, use role-based messages, and tune how ChatGPT responds. These tools allow you to build interactive, customized AI chat experiences!
Use the OpenAI Image API to generate visuals:
import openai
openai.api_key = "your-api-key"
response = openai.Image.create(
prompt="A futuristic city at sunset, cyberpunk style",
n=1,
size="512x512"
)
image_url = response['data'][0]['url']
print(image_url)
256x256
, 512x512
, 1024x1024
You can provide an image and a mask to edit specific parts using the Image.create_edit()
method (requires a mask PNG file).
response = openai.Image.create_edit(
image=open("base.png", "rb"),
mask=open("mask.png", "rb"),
prompt="Add a red balloon in the sky",
n=1,
size="512x512"
)
Once you have the image URL, use Python to download and save it:
import requests
image_data = requests.get(image_url).content
with open("generated_image.png", "wb") as f:
f.write(image_data)
The DALL·E API turns text into powerful visuals. By combining prompts, styles, and sizes, you can generate unique content for various applications. You also learned how to save and edit images locally!
You can use the Whisper API to send an audio file and receive text transcription:
import openai
openai.api_key = "your-api-key"
audio_file = open("meeting.mp3", "rb")
transcript = openai.Audio.transcribe(
model="whisper-1",
file=audio_file
)
print(transcript["text"])
language
parameter.
transcript = openai.Audio.transcribe(
model="whisper-1",
file=open("spanish_audio.mp3", "rb"),
language="es"
)
Whisper enables speech-to-text transcription from MP3, WAV, and M4A files. It's multilingual, accurate, and perfect for apps that require audio understanding—from automating meeting notes to making media accessible.
You can describe the code you want in plain English, and Codex will generate it:
import openai
openai.api_key = "your-api-key"
response = openai.Completion.create(
model="code-davinci-002",
prompt="Create a Python function that calculates the factorial of a number",
temperature=0,
max_tokens=100
)
print(response.choices[0].text.strip())
# Prompt to Codex: "Add comments to the following Python code"
"""
def fib(n):
if n <= 1:
return n
else:
return fib(n-1) + fib(n-2)
"""
Codex transforms how we write code—by turning natural language into programming logic. Whether you're learning, debugging, or building tools, Codex can help speed up development and improve productivity across many languages and platforms.
To write effective prompts, keep these tips in mind:
prompt = "Write a summary of the key points from the following article in bullet points."
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
max_tokens=100
)
In role-based prompting, you assign a role to the model, which influences how it responds:
prompt = "You are a math tutor. Help me solve the following algebra problem: x + 5 = 10."
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
max_tokens=100
)
Chaining involves linking multiple prompts together to guide the model through a series of tasks.
Few-shot learning involves providing a few examples to show the model how to perform a task.
prompt = """
Translate the following English sentences to French:
1. How are you? → Comment ça va ?
2. Where is the nearest restaurant? → Où est le restaurant le plus proche ?
3. What is your name? → Quel est ton nom ?
"""
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
max_tokens=100
)
Creating reusable templates for common tasks can save time and improve consistency:
prompt = "Create a summary of this product description for an e-commerce site. Include the key features and benefits."
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
max_tokens=150
)
Effective prompt engineering allows you to harness the full potential of AI models like GPT-3 by crafting prompts that drive the right results. By leveraging role-based prompting, chaining, few-shot learning, and prompt templates, you can refine your AI interactions for various tasks.
In this project, you’ll build a simple AI chatbot using Flask (a Python web framework) and GPT-4 (from OpenAI).
from flask import Flask, request, jsonify
import openai
app = Flask(__name__)
openai.api_key = "your-api-key"
@app.route("/chat", methods=["POST"])
def chat():
user_message = request.json["message"]
response = openai.Completion.create(
model="gpt-4",
prompt=user_message,
max_tokens=150
)
return jsonify({"response": response.choices[0].text.strip()})
if __name__ == "__main__":
app.run(debug=True)
This project will take a story or a description and generate corresponding images using DALL·E.
import openai
openai.api_key = "your-api-key"
def generate_image(prompt):
response = openai.Image.create(
prompt=prompt,
n=1,
size="512x512"
)
return response['data'][0]['url']
story_description = "A futuristic city with flying cars and neon lights."
image_url = generate_image(story_description)
print(image_url)
For this project, you’ll transcribe audio voice notes into text using the Whisper API.
import openai
openai.api_key = "your-api-key"
def transcribe_audio(audio_file):
response = openai.Audio.transcribe(
model="whisper-1",
file=open(audio_file, "rb")
)
return response['text']
audio_file_path = "audio_note.mp3"
transcription = transcribe_audio(audio_file_path)
print(transcription)
This project will integrate Codex with Visual Studio Code (VSCode) to generate code from natural language.
import openai
openai.api_key = "your-api-key"
def generate_code(prompt):
response = openai.Completion.create(
model="code-davinci-002",
prompt=prompt,
max_tokens=150
)
return response.choices[0].text.strip()
prompt = "Write a Python function to sort a list of integers in ascending order."
generated_code = generate_code(prompt)
print(generated_code)
In this project, you'll build a multi-modal app that combines different AI APIs to create a more advanced and interactive user experience.
import openai
openai.api_key = "your-api-key"
def process_voice_input(audio_file):
# Step 1: Transcribe audio to text using Whisper
transcription = openai.Audio.transcribe(
model="whisper-1",
file=open(audio_file, "rb")
)['text']
# Step 2: Use GPT-4 to generate a story based on the transcription
story = openai.Completion.create(
model="gpt-4",
prompt=f"Generate a short story based on this: {transcription}",
max_tokens=150
).choices[0].text.strip()
# Step 3: Generate an image for the story using DALL·E
image_url = openai.Image.create(
prompt=story,
n=1,
size="512x512"
)['data'][0]['url']
return story, image_url
audio_file_path = "voice_input.mp3"
story, image_url = process_voice_input(audio_file_path)
print("Story:", story)
print("Image URL:", image_url)
In this chapter, we explored how to combine various AI-powered technologies like GPT-4, DALL·E, Whisper, and Codex to build powerful and interactive applications. These AI-driven projects can assist in automating tasks and enhancing user experiences through natural language, images, and voice capabilities.
Prompt injection occurs when malicious input is used to manipulate the behavior of an AI model. Protecting against prompt injections ensures your models respond as intended.
# Example: Preventing prompt injection
def sanitize_input(user_input):
# Remove potentially harmful characters
sanitized_input = user_input.replace("<", "").replace(">", "")
return sanitized_input
user_input = "<script>alert('malicious code')</script>"
sanitized_input = sanitize_input(user_input)
print(sanitized_input) # This will print: alert('malicious code')
Your API key is crucial for authenticating your requests to OpenAI. If compromised, unauthorized users could gain access to your account.
# Example: Using environment variables to securely store the API key
import os
openai_api_key = os.getenv("OPENAI_API_KEY")
# Make sure the OPENAI_API_KEY is set in your environment variables
When building AI applications, it's essential to handle user data with care to maintain privacy and comply with legal regulations.
# Example: Storing and anonymizing user data
import hashlib
def anonymize_data(user_data):
# Hash user data for anonymization
anonymized_data = hashlib.sha256(user_data.encode()).hexdigest()
return anonymized_data
user_data = "user_email@example.com"
anonymized_data = anonymize_data(user_data)
print(anonymized_data)
Content moderation is essential for preventing harmful or inappropriate content from being generated or shared.
# Example: Using OpenAI's moderation API to check content
import openai
openai.api_key = "your-api-key"
def check_content_moderation(content):
response = openai.Moderation.create(input=content)
return response['results'][0]['flagged']
content = "This is some potentially harmful content."
is_flagged = check_content_moderation(content)
print(is_flagged) # This will return True or False
OpenAI imposes rate limits and usage quotas to prevent abuse and manage costs.
# Example: Monitoring usage and managing rate limits
import openai
openai.api_key = "your-api-key"
def make_api_call(prompt):
try:
response = openai.Completion.create(model="gpt-3.5-turbo", prompt=prompt, max_tokens=50)
return response.choices[0].text.strip()
except openai.error.RateLimitError:
return "Rate limit exceeded. Please try again later."
response = make_api_call("What is the weather today?")
print(response)
It's important to follow OpenAI's fair use policies to ensure the ethical use of their technology and prevent misuse.
# Example: Checking the latest fair use guidelines from OpenAI
import requests
def get_openai_fair_use_guidelines():
url = "https://openai.com/policies/usage-policies"
response = requests.get(url)
return response.text
guidelines = get_openai_fair_use_guidelines()
print(guidelines)
In this chapter, we discussed the importance of security, ethics, and managing limits when using AI technologies. By following best practices such as protecting your API key, moderating content, and adhering to OpenAI's fair use policies, you can build responsible and secure AI-powered applications.
Function calling with GPT-4 allows the model to interact with external functions or APIs to enhance its capabilities. This feature enables more dynamic and interactive applications.
# Example: Calling an external function with GPT-4
import openai
openai.api_key = "your-api-key"
def function_call_example():
response = openai.Completion.create(
model="gpt-4",
prompt="How can I help you today?",
max_tokens=100
)
return response.choices[0].text.strip()
response = function_call_example()
print(response) # Output: GPT-4's response based on the prompt
Custom GPTs allow you to build personalized models using your own tools, memory management, and integration with external APIs.
# Example: Creating a custom GPT with memory and API integration
import openai
openai.api_key = "your-api-key"
def create_custom_gpt():
response = openai.Completion.create(
model="gpt-4",
prompt="Welcome! How can I assist you today?",
max_tokens=100,
tools=["weather_api", "calendar"]
)
return response.choices[0].text.strip()
response = create_custom_gpt()
print(response)
Plugins and embeddings allow you to extend GPT’s functionality by integrating it with third-party services and databases.
# Example: Using embeddings for semantic search
import openai
openai.api_key = "your-api-key"
def create_embedding(text):
response = openai.Embedding.create(input=text, model="text-embedding-ada-002")
return response['data'][0]['embedding']
text = "How to integrate GPT-3 with APIs"
embedding = create_embedding(text)
print(embedding) # Output: The vector representation of the text
Fine-tuning allows businesses to customize models based on their specific needs and data, improving accuracy and relevance for niche tasks.
# Example: Fine-tuning a model with custom data
import openai
openai.api_key = "your-api-key"
def fine_tune_model(training_data):
response = openai.FineTune.create(
training_file=training_data,
model="gpt-3.5-turbo"
)
return response['id']
training_data = "path_to_your_training_file.jsonl"
fine_tuned_model_id = fine_tune_model(training_data)
print(fine_tuned_model_id) # Output: ID of the fine-tuned model
Web scraping allows you to gather data from the web, while summarization helps condense large amounts of information into concise summaries.
# Example: Web scraping and summarizing an article
import requests
from bs4 import BeautifulSoup
def scrape_and_summarize(url):
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
text = soup.get_text()
summary = openai.Completion.create(
model="gpt-4",
prompt="Summarize the following article: " + text,
max_tokens=150
)
return summary.choices[0].text.strip()
url = "https://example.com/article"
summary = scrape_and_summarize(url)
print(summary) # Output: GPT-4's summary of the article
Connecting GPT to external APIs enables it to fetch real-time data such as weather updates, financial information, and more.
# Example: Fetching weather data and using it in GPT-4 response
import requests
import openai
openai.api_key = "your-api-key"
def get_weather(city):
weather_api_url = f"http://api.weatherapi.com/v1/current.json?key=your_api_key&q={city}"
response = requests.get(weather_api_url)
return response.json()['current']['condition']['text']
def gpt_with_weather(city):
weather = get_weather(city)
response = openai.Completion.create(
model="gpt-4",
prompt=f"The weather in {city} is currently {weather}. How should I dress?",
max_tokens=100
)
return response.choices[0].text.strip()
weather_response = gpt_with_weather("London")
print(weather_response) # Output: GPT-4's response based on the weather information
In this chapter, we explored advanced use cases and customization techniques such as function calling, creating custom GPTs, using plugins, fine-tuning models, web scraping, and connecting to external APIs. These techniques allow you to create more sophisticated, dynamic, and context-aware AI applications.
Integrating APIs into web applications allows you to add dynamic features, such as real-time data fetching, user interactions, and third-party services, into your projects.
# Example: Integrating OpenAI API with a Flask web app
from flask import Flask, request, jsonify
import openai
app = Flask(__name__)
openai.api_key = "your-api-key"
@app.route('/generate', methods=['POST'])
def generate_response():
user_input = request.json.get('input')
response = openai.Completion.create(
model="gpt-4",
prompt=user_input,
max_tokens=100
)
return jsonify({"response": response.choices[0].text.strip()})
if __name__ == "__main__":
app.run(debug=True)
Deploying AI-powered projects involves hosting your code on platforms like Heroku, Vercel, or AWS for public access.
# Example: Deploying a Flask app to Heroku
# 1. Install Heroku CLI and log in
# 2. Create a "Procfile" for the web server
# 3. Create a "requirements.txt" for dependencies
# 4. Push the app to Heroku using git
# Procfile (specifies how to run your app on Heroku)
web: python app.py
# 5. Push the code to Heroku repository
git init
heroku create your-app-name
git add .
git commit -m "Initial commit"
git push heroku master
Webhooks allow your web application to receive real-time notifications from other services or events, making it highly responsive and interactive.
# Example: Handling a webhook in Flask
from flask import Flask, request
import json
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def handle_webhook():
data = request.json
# Process the webhook data
print(data) # Example: Log the received data
return '', 200
if __name__ == "__main__":
app.run(debug=True)
Logging, analytics, and performance tracking are essential for monitoring your app's health and user interactions in real-time.
# Example: Setting up basic logging with Python's logging module
import logging
# Configure logging
logging.basicConfig(filename='app.log', level=logging.INFO)
# Log an event
logging.info('App started')
# Example: Tracking performance with time module
import time
start_time = time.time()
# Your application code
end_time = time.time()
execution_time = end_time - start_time
logging.info(f'Execution time: {execution_time} seconds')
Monetizing your AI app involves offering premium features, subscription models, or leveraging advertisements to generate revenue.
# Example: Implementing a subscription model with Flask
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/pricing')
def pricing():
return render_template('pricing.html')
if __name__ == "__main__":
app.run(debug=True)
In this chapter, we discussed deploying AI projects by integrating APIs with web apps, deploying to cloud platforms like Heroku, Vercel, and AWS, handling webhooks for real-time interaction, and setting up logging and analytics. Finally, we explored different monetization strategies for your AI-powered app.
Multimodal AI refers to systems that can process and combine multiple forms of data such as text, audio, images, and video. This enables AI to understand richer contexts and generate more sophisticated outputs.
# Example: Using a multimodal AI system (conceptual example)
# This can include text input, image recognition, and speech analysis.
# While OpenAI is working on multimodal models like GPT-4, actual implementation would combine inputs.
# Pseudocode: Text-to-image with GPT-4 and DALL·E
text_input = "A cat playing with a ball"
image_output = openai.Image.create(prompt=text_input, model="dall-e")
Agent-based systems are AI systems that can act autonomously to perform tasks or achieve goals in dynamic environments, often without human intervention.
# Example: Conceptual pseudocode for an autonomous agent
class AutonomousAgent:
def __init__(self, environment):
self.environment = environment
self.state = self.get_initial_state()
def take_action(self, action):
self.state = self.environment.update_state(action)
def learn_from_experience(self, feedback):
# Use reinforcement learning to optimize decisions
self.state = self.optimize_action(feedback)
Long-term memory and persistent context enable AI systems to remember previous interactions and incorporate this historical data into current decision-making processes.
# Example: Using long-term memory with GPT-4 for a chatbot
conversation_history = []
def chatbot_response(user_input):
conversation_history.append(user_input)
response = openai.Completion.create(
model="gpt-4",
prompt=" ".join(conversation_history),
max_tokens=150
)
return response.choices[0].text.strip()
# Interaction example
user_input = "What's the weather today?"
response = chatbot_response(user_input)
The rapid development of AI technologies raises significant ethical concerns, including privacy, bias, and the potential for misuse.
# Example: Ethical guidelines for AI developers
# Developers should prioritize ethical principles such as fairness, transparency, and accountability.
# This might include implementing algorithmic fairness checks and ensuring data privacy.
def check_fairness(model_output, user_input):
# Ensure that AI responses are not biased or discriminatory
if "discriminatory" in model_output:
return "Response contains bias, needs review"
return "Response is fair"
The AI-powered economy presents numerous opportunities for innovation and new business models.
# Example: AI-powered recommendation system for retail
def recommend_products(user_data):
# Using AI to analyze user preferences and recommend relevant products
recommended_products = ai_model.predict(user_data)
return recommended_products
# Example business model: Offering AI-powered recommendations as a service for e-commerce platforms
In this chapter, we explored the future of OpenAI and APIs, including the evolution of multimodal AI, agent-based systems, and the integration of long-term memory. We also discussed ethical considerations in AI development and the immense opportunities AI presents in the global economy.