
OpenAI API — User Guide
OpenAI API; GPT-4o.
Strengths
- Provide top models such as GPT-4o, o1, DALL·E 3, Whisper, etc.
- The API documentation is complete and the community ecosystem is the richest
- Supports advanced functions such as Function Calling and JSON Mode
- Assistants API to build AI assistants with memory
- Pay by token, flexible billing method
Best for
- Build AI chatbots and conversational apps
- NLP tasks such as text generation, summarization, and translation
- Image generation (DALL·E 3)
- Speech to text (Whisper)
- Build AI Agents and automated workflows
quick start
After registering an account and obtaining the API Key, you can call GPT-4o within a few minutes.
First API call
# Install the OpenAI Python library
pip install openai
# Python code example
from openai import OpenAI
client = OpenAI(api_key="your-api-key")
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Explain what machine learning is in one sentence"}
]
)
print(response.choices[0].message.content)The output is similar to:
“Machine learning allows computers to automatically learn patterns from data.
without the need for explicit programming techniques. "
The response object also contains:
usage.prompt_tokens: enter the number of tokens
usage.completion_tokens: output token number
model: model version used
The API Key should be kept properly and should not be submitted to the Git repository. It is recommended to use environment variable storage.
Streaming
from openai import OpenAI
client = OpenAI(api_key="your-api-key")
# Streaming output, displayed verbatim
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a poem about spring"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)The text will be output word by word like a typewriter,
Better user experience,
Suitable for scenarios such as chat interfaces that require real-time response.
Streaming output can significantly improve user experience, especially in long text generation scenarios.
Function Calling
Function Calling allows GPT to call external tools and APIs and is a core function for building AI Agents.
Let GPT call the weather API
from openai import OpenAI
import json
client = OpenAI(api_key="your-api-key")
#define tools
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather of the specified city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "city name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
}]
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "How is the weather in Beijing today?"}],
tools=tools,
tool_choice="auto"
)
# Check whether the tool needs to be called
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
print(f"GPT to be called: {tool_call.function.name}")
print(f"Parameters: {args}")Output:
GPT to call: get_weather
Parameters: {“city”: “Beijing”, “unit”: “celsius”}
Then you need to actually call the weather API,
Return results to GPT,
GPT generates the final natural language answer.
Function Calling is the foundation for building AI Agents, allowing GPT to interact with any external system.
Image generation (DALL·E 3)
By calling DALL·E 3 via API, the image generation function can be integrated in the application.
Generate image and get URL
from openai import OpenAI
client = OpenAI(api_key="your-api-key")
response = client.images.generate(
model="dall-e-3",
prompt="A serene Japanese garden with cherry blossoms, "
"traditional stone lantern, koi pond, "
"soft morning light, photorealistic",
size="1024x1024",
quality="standard",
n=1
)
image_url = response.data[0].url
print(f"Image URL: {image_url}")
# The URL is valid for 1 hour and needs to be downloaded and saved in time.Returns the temporary URL of the generated image,
The picture is of high quality and conforms to the Prompt description.
The URL is valid for about 1 hour and needs to be downloaded and saved.
The picture quality of DALL·E 3 is very high, but the generation speed is slow (about 10-20 seconds), which is suitable for non-real-time scenes.
Compared with similar tools
| Tool | Strength | Best for | Pricing |
|---|---|---|---|
| OpenAI API This tool | The model has the highest quality, the most complete documentation, and the richest ecology | Application development requiring the highest quality AI capabilities | Pay by token (GPT-4o: $2.5/million input tokens) |
| Anthropic API | Claude model has strong long context capability and high security | Need to process long documents and have high security requirements | Pay by token |
| Google AI Studio | Gemini model has a large free quota and strong multi-modal capabilities | Limited budget, multi-modal capabilities required | Free quota/pay as you go |
| Together AI | Open source models are cheaper | Need to reduce API costs and accept open source models | 10x+ cheaper than OpenAI |
Sources & references:
- OpenAI API official documentation (2025-03)
- OpenAI Cookbook (code example) (2025-03)
- OpenAI pricing page (2025-03)