openAI API#
This document provides an overview of the OpenAI API, including its features, capabilities, and how to get started.
Overview#
The OpenAI API allows developers to integrate advanced AI models into their applications. It provides access to a variety of models, including language models, image generation models, and more. The API is designed to be easy to use, with a simple RESTful interface and comprehensive documentation.
Key Features#
Language Models: Access powerful language models like GPT-4 for tasks such as text generation, summarization, translation, and more.
Image Generation: Generate images from text prompts using models like DALL·E.
Fine-tuning: Customize models to better suit specific use cases by fine-tuning them on your own datasets.
Embeddings: Create vector representations of text for tasks like semantic search and clustering.
Moderation: Use built-in tools to ensure content generated by the models adheres to safety and policy guidelines.
Getting Started#
Sign Up: Create an account on the OpenAI platform.
API Key: Obtain your API key from the OpenAI dashboard.
Install SDK: Install the OpenAI SDK for your preferred programming language (e.g., Python, Node.js).
pip install openai
Make API Calls: Use the SDK to make API calls.
Here’s a simple example in Python:
from openai import OpenAI
key = 'your-api-key' # Replace with your actual OpenAI API key
model = "gpt-4.1-mini"
max_tokens = 60
temperature = 0.7
client = OpenAI(api_key=key)
messages = [
{
"role" : "user",
"content" : "Translate the following English text to French: 'Hello, how are you?'"
}
]
response = client.chat.completions.create(model=model, \
messages=messages, \
max_tokens=max_tokens, \
temperature=temperature)
reply = response.choices[0].message.content.strip()
print(reply)
Note that in the code above, you need to replace 'your-api-key' with your actual OpenAI API key.
client object is created using the OpenAI class from the openai package, which is initialized with your API key.
The client.chat.completions.create method is then called to generate a chat completion based on the provided messages.
Model: The model parameter specifies which model to use for generating the response. In this case, we are using the gpt-4.1-mini model. This model is a smaller version of GPT-4, designed to be more efficient while still providing high-quality responses.
Temperature: The temperature parameter controls the randomness of the model’s output. A lower temperature (e.g., 0.2) makes the output more focused and deterministic, while a higher temperature (e.g., 0.8) makes it more random and creative.
Messages: This API method expects a list of messages, where each message is a dictionary containing a role (either “system”, “user”, or “assistant”) and the content of the message.
When the
roleis “system”, it sets the behavior of the assistant.When the
roleis “user”, it represents the input from the user.When the
roleis “assistant”, it represents the response from the assistant.
Running conversation#
The following example shows how to run a conversation with short-term memory using the OpenAI API. This example uses the openai Python package to interact with the GPT-4 model.
Read–Eval–Print Loop (REPL) for a conversation with short-term memory:
from openai import OpenAI
key = 'your-api-key' # Replace with your actual OpenAI API key
client = OpenAI(api_key=key)
messages = [{"role": "system", "content": "You are a helpful assistant."}]
print("Type your prompt (or 'exit' to quit):")
while True:
user_input = input("> ")
if user_input.lower() in ["exit", "quit"]:
print("Goodbye!")
break
# Add the new user message
messages.append({"role": "user", "content": user_input})
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=messages,
max_tokens=200
)
reply = response.choices[0].message.content.strip()
print(reply)
# Save assistant response into conversation history
messages.append({"role": "assistant", "content": reply})
Note that in the code above, the biggest change is the addition of a loop that continuously prompts the user for input and appends both user and assistant messages to the messages list. This allows the model to maintain context throughout the conversation.
Web (Javascript) Example#
Here is an example of how to use the OpenAI API in a web application using JavaScript.
index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenAI Chat Example</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="script.js" defer></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>OpenAI Chat Example</h1>
<div id="chat"></div>
<input type="text" id="input" placeholder="Type your message here..." />
<button id="send">Send</button>
</body>
</html>
Put the following in style.css file:
body { font-family: Arial, sans-serif; margin: 20px; }
#chat { border: 1px solid #ccc; padding: 10px; height: 300px; overflow-y: scroll; }
#input { width: 80%; }
#send { width: 18%; }
Put the following in script.js file:
const apiKey = 'your-api-key'; // Replace with your OpenAI API key
const chatDiv = document.getElementById('chat');
const inputField = document.getElementById('input');
const sendButton = document.getElementById('send');
const messages = [{ role: 'system', content: 'You are a helpful assistant.' }];
async function sendMessage() {
const userInput = inputField.value;
if (!userInput) return;
// Display user message
chatDiv.innerHTML += `<p><strong>You:</strong> ${userInput}</p>`;
messages.push({ role: 'user', content: userInput });
inputField.value = '';
try {
const response = await axios.post('https://api.openai.com/v1/chat/completions', {
model: 'gpt-4.1-mini',
messages: messages,
max_tokens: 200
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
}
});
const reply = response.data.choices[0].message.content.trim();
chatDiv.innerHTML += `<p><strong>Assistant:</strong> ${reply}</p>`;
messages.push({ role: 'assistant', content: reply });
chatDiv.scrollTop = chatDiv.scrollHeight; // Scroll to bottom
} catch (error) {
console.error('Error:', error);
chatDiv.innerHTML += `<p><strong>Error:</strong> ${error.message}</p>`;
}
}
sendButton.addEventListener('click', sendMessage);
inputField.addEventListener('keypress', function (e) {
if (e.key === 'Enter') sendMessage();
});
Images#
Using the OpenAI API to generate images from text prompts:
Text → Image#
Using the OpenAI API to generate images from text prompts. For example, you can create an image of a “A futuristic city skyline at sunset in watercolor style.”
The client.images.generate is used to generate images based on the provided prompt. The n parameter specifies the number of images to generate, and the size parameter defines the dimensions of the generated images.
The result is returned as base64-encoded strings, which can be decoded and saved as image files.
from openai import OpenAI
from PIL import Image
from io import BytesIO
import base64
client = OpenAI(api_key=key)
result = client.images.generate(
model="gpt-image-1",
prompt="A watercolor painting of a futuristic city skyline at sunset",
size="1024x1024"
)
# Get the base64 string
image_base64 = result.data[0].b64_json
# Decode base64 to bytes
image_bytes = base64.b64decode(image_base64)
# Option 1: Save to file
with open("city.png", "wb") as f:
f.write(image_bytes)
# Option 2: Show in Python (if you have Pillow installed)
img = Image.open(BytesIO(image_bytes))
img.show()
Variations#
Another cool feature is to create variations of an existing image. You can upload an image and ask the model to generate different versions of it.
result = client.images.create_variation(
image=open("cat.png", "rb"),
n=3, # number of variations
size="512x512"
)
result
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[1], line 1
----> 1 result = client.images.create_variation(
2 image=open("cat.png", "rb"),
3 n=3, # number of variations
4 size="512x512"
5 )
7 result
NameError: name 'client' is not defined
Audio#
Text → Speech#
Using the OpenAI API to convert text to speech. The client.audio.speech.create method is used to generate speech audio from the provided text. The model parameter specifies the speech synthesis model to use, and the voice parameter defines the voice characteristics.
speech = client.audio.speech.create(
model="gpt-4o-mini-tts",
voice="alloy", # other voices available: verse, aria, etc.
input="Hi! My name is Syed Fahad Sultan. I am your AI instructor."
)
# Save to file
with open("speech.mp3", "wb") as f:
f.write(speech.read())
Speech → Text#
Using the OpenAI API to transcribe audio files into text. This is useful for converting spoken language into written text.
with open("speech.mp3", "rb") as audio_file:
transcript = client.audio.transcriptions.create(
model="gpt-4o-transcribe", # or "whisper-1"
file=audio_file
)
print(transcript.text)