
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)Streaming output can significantly improve user experience, especially in long text generation scenarios.