OpenAI API

OpenAI API — User Guide

OpenAI API; GPT-4o.

Visit website VPN may be required Freemium Sign-up required
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.

Scenario

First API call

Prompt example
# 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)
Output / what to expect

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

Tips

The API Key should be kept properly and should not be submitted to the Git repository. It is recommended to use environment variable storage.

Scenario

Streaming

Prompt example
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)
Output / what to expect

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.

Tips

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.

Scenario

Let GPT call the weather API

Prompt example
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 / what to expect

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.

Tips

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.

Scenario

Generate image and get URL

Prompt example
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.
Output / what to expect

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.

Tips

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

ToolStrengthBest forPricing
OpenAI API This toolThe model has the highest quality, the most complete documentation, and the richest ecologyApplication development requiring the highest quality AI capabilitiesPay by token (GPT-4o: $2.5/million input tokens)
Anthropic APIClaude model has strong long context capability and high securityNeed to process long documents and have high security requirementsPay by token
Google AI StudioGemini model has a large free quota and strong multi-modal capabilitiesLimited budget, multi-modal capabilities requiredFree quota/pay as you go
Together AIOpen source models are cheaperNeed to reduce API costs and accept open source models10x+ cheaper than OpenAI