如何调用GPT-4 Turbo API?2026年最新步骤与定价详解
AIAI Summary (BLUF)
This article provides a step-by-step guide on how to access and use the GPT-4 Turbo API from OpenAI. It covers account creation, API key generation, making requests with curl, enabling JSON mode, and
获取和使用 GPT-4 Turbo API 的分步指南
Before you start reading this article, note that GPT-4o is available and is cheaper and faster than GPT-4 Turbo.
在开始阅读本文前,请注意 GPT-4o 已上线,其价格更低、速度更快。
Introduction to GPT-4 Turbo
GPT-4 Turbo 简介
GPT-4 Turbo is a cutting-edge large language model (LLM) developed by OpenAI. Its capabilities have fundamentally transformed how we interact with AI, enabling developers to build applications such as personal assistants, smart chatbots, grammar checkers, spam filters, and code generators.
GPT-4 Turbo 是由 OpenAI 开发的前沿大型语言模型(LLM)。其能力从根本上改变了我们与 AI 交互的方式,使得开发者能够构建诸如个人助理、智能聊天机器人、语法检查器、垃圾邮件过滤器和代码生成器等应用。
If you are unfamiliar with LLMs, consider reading my introductory article on how LLMs like GPT work.
如果你对 LLM 还不熟悉,建议先阅读我关于 LLM(如 GPT)工作原理的入门文章。
Only developers who have paid for OpenAI API usage can access GPT-4 Turbo. New users can start with GPT-3.5 Turbo: Start using GPT-3.5 Turbo’s API in 5 minutes.
只有已经为 OpenAI API 付费的开发者才能使用 GPT-4 Turbo。新用户可以先用 GPT-3.5 Turbo:5 分钟上手 GPT-3.5 Turbo API。
Prerequisites and Account Setup
前提条件与账户设置
Create an OpenAI Account and Obtain Your API Key
创建 OpenAI 账户并获取 API 密钥
- Create an account on OpenAI. (在 OpenAI 上创建一个账户。)

- Confirm your email address. (确认你的电子邮箱地址。)
- Log in to the platform. (登录平台。)
- Check your free $5 of credits on this billing page. Once exhausted, generated API keys will not work until you add payment. (在账单页面查看你的 5 美元免费额度。一旦用尽,生成的 API 密钥将无法使用,直到你添加支付方式。)

- Generate your first API key. It will only be shown once — copy it into a password manager for secure storage. (在此处生成你的第一个 API 密钥。密钥只会显示一次,请将其复制到密码管理器安全保存。)

- Start using your API key with GPT-4 Turbo’s API (continue reading for instructions). (开始使用你的 API 密钥访问 GPT-4 Turbo API,继续阅读以获取操作指南。)
Making Your First API Request
发送你的第一个 API 请求
We will use curl from the terminal for this example. The API can be called from any language that supports HTTP requests.
本例将使用终端中的
curl命令。任何支持 HTTP 请求的编程语言均可调用该 API。
Step-by-Step Process
分步流程
- Locate your GPT-4 Turbo API key: The key is a long string of random characters. Keep it secure. (找到你的 GPT-4 Turbo API 密钥:密钥是一长串随机字符。请妥善保管。)
- Open your terminal: To use curl, open your terminal application. (打开终端:使用 curl 时需要打开终端应用程序。)
- Input the curl command: Use the following command as a template. Replace
YOUR_API_KEYwith your actual key. (输入 curl 命令:使用以下命令模板,将YOUR_API_KEY替换为你的实际密钥。)
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
https://api.openai.com/v1/chat/completions -d \
'{
"model": "gpt-4-turbo-preview",
"messages": [
{
"role": "system",
"content": "You are an assistant."
},
{
"role": "user",
"content": "Hello!"
}
]
}'
- The
-dflag specifies the JSON request body, including the model (gpt-4-turbo-preview) and messages (system role to set assistant behavior, and user message). (-d标志指定了 JSON 格式的请求体,包含模型(gpt-4-turbo-preview)和消息(系统角色设定助手行为,以及用户消息)。)
- Run the command: Press Enter. After a few seconds, you should see the API response in your terminal. (运行命令:按下回车键。几秒钟后,你应在终端中看到 API 的响应。)

Pro tip: GPT-4 Turbo (
gpt-4-turbo-preview) supports up to 128,000 tokens per API call. A token is a numerical representation of text; roughly 1,000 tokens equals 750 English words. All input and output messages combined cannot exceed this limit.专业提示:GPT-4 Turbo (
gpt-4-turbo-preview) 每次 API 调用最多支持 128,000 个 token。Token 是文本的数字表示;大约 1,000 个 token 相当于 750 个英文单词。所有输入和输出消息的总和不能超过此限制。
For more details, refer to the official Chat Completions API reference.
更多详情请参考官方 Chat Completions API 文档。
Enabling JSON Mode
启用 JSON 模式
GPT-4 Turbo (and GPT-3.5 Turbo) now support a JSON mode that forces the model to consistently output valid JSON.
GPT-4 Turbo(以及 GPT-3.5 Turbo)现在支持 JSON 模式,该模式强制模型始终输出有效的 JSON。
Previously, you could ask for JSON output but risked receiving plain text. The new JSON mode mitigates that.
以前,你可以要求 JSON 输出,但可能收到纯文本。新的 JSON 模式解决了这个问题。
How to Use JSON Mode
如何使用 JSON 模式
Add the response_format object and update the system message to mention “JSON”. Example diff:
添加
response_format对象,并更新系统消息以包含“JSON”一词。示例差异如下:
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
https://api.openai.com/v1/chat/completions -d \
'{
"model": "gpt-4-turbo-preview",
"messages": [
{
"role": "system",
- "content": "You are an assistant."
+ "content": "You are an assistant, and you only reply with JSON."
},
{
"role": "user",
"content": "Hello!"
}
- ]
+ ],
+ "response_format": {
+ "type": "json_object"
+ }
}'
Key requirements:
关键要求:
- The system message must contain the word “JSON” (case-insensitive). Otherwise, the API returns an error. (系统消息必须包含“JSON”字样(不区分大小写),否则 API 会返回错误。)
- While JSON mode ensures valid JSON output, it does not guarantee 100% structural accuracy for complex schemas. (虽然 JSON 模式确保输出有效的 JSON,但它不能保证复杂 schema 的 100% 结构正确性。)

Pricing Comparison
定价对比
Pricing is subject to change, but as of writing, GPT-4 Turbo’s rates are significantly lower than older GPT-4 variants.
定价可能变化,但截至撰写本文时,GPT-4 Turbo 的价格远低于旧版 GPT-4 变体。
| Model | Input (per 1K tokens) | Output (per 1K tokens) |
|---|---|---|
| gpt-4-turbo-preview (128K context) | $0.01 | $0.03 |
| gpt-4 (32K context) | $0.06 | $0.12 |
| gpt-4 (8K context) | $0.03 | $0.06 |
模型 输入(每千 token) 输出(每千 token) gpt-4-turbo-preview(128K 上下文) $0.01 $0.03 gpt-4(32K 上下文) $0.06 $0.12 gpt-4(8K 上下文) $0.03 $0.06
This price reduction is great news for developers aiming to build cost-effective AI-powered tools.
这一降价对希望构建高性价比 AI 工具的开发者来说是个好消息。
Ideas for Building with GPT-4 Turbo
基于 GPT-4 Turbo 的构建思路
GPT-4 Turbo opens up many possibilities. For example, I created Nobinge, a tool that summarises and chats with YouTube videos.
GPT-4 Turbo 开辟了许多可能性。例如,我创建了 Nobinge,一个可以总结并与 YouTube 视频对话的工具。
Here are some ideas to experiment with:
以下是一些值得尝试的想法:
- Additional AI-based features for existing products (为现有产品增加基于 AI 的功能)
- Automated email responses (自动电子邮件回复)
- Chatbots (聊天机器人)
- Content summarizers (内容摘要工具)
- Personal assistants (个人助理)
- Personalized teaching programs (个性化教学程序)
- Sentiment analysis tools (情感分析工具)
- Spam filters (垃圾邮件过滤器)
Also check out OpenAI’s Text-to-Speech API for adding realistic voices to your projects.
此外,请查看 OpenAI 的文本转语音 API ,为你的项目添加逼真的语音。
This article provided a step-by-step guide to accessing and using GPT-4 Turbo’s API. For further reading:
本文提供了访问和使用 GPT-4 Turbo API 的分步指南。延伸阅读:
- GPT-3.5 Turbo API guide (GPT-3.5 Turbo API 指南)
- GPT-4o: stronger general model (GPT-4o:更强的通用模型)
- GPT-4o Mini: cheaper alternative (GPT-4o Mini:更便宜的选择)
- Call OpenAI API from PHP (从 PHP 调用 OpenAI API)
- GPT-4.1: clearer step up (GPT-4.1:更清晰的升级)
- GPT-5 API in PHP (在 PHP 中使用 GPT-5 API)
- How LLMs work (LLM 工作原理)
- Laravel ChatGPT integration (Laravel ChatGPT 集成)
- Generate Laravel factories with AI (用 AI 生成 Laravel 工厂)
- Text-to-speech from PHP (从 PHP 实现文本转语音)
Help others find this guide by sharing on social media.
通过社交媒体分享本指南,帮助更多人找到它。
常见问题(FAQ)
GPT-4 Turbo API密钥怎么获取?
在OpenAI平台注册并登录,进入API密钥页面生成密钥。新用户有5美元免费额度,用完后需添加支付方式才能继续使用。
如何用curl发送GPT-4 Turbo请求?
GPT-4 Turbo和GPT-4o哪个更好?
GPT-4o价格更低、速度更快,是更优选择。建议新用户优先考虑GPT-4o,除非有特定需求必须使用GPT-4 Turbo。
版权与免责声明:本文仅用于信息分享与交流,不构成任何形式的法律、投资、医疗或其他专业建议,也不构成对任何结果的承诺或保证。
文中提及的商标、品牌、Logo、产品名称及相关图片/素材,其权利归各自合法权利人所有。本站内容可能基于公开资料整理,亦可能使用 AI 辅助生成或润色;我们尽力确保准确与合规,但不保证完整性、时效性与适用性,请读者自行甄别并以官方信息为准。
若本文内容或素材涉嫌侵权、隐私不当或存在错误,请相关权利人/当事人联系本站,我们将及时核实并采取删除、修正或下架等处理措施。 也请勿在评论或联系信息中提交身份证号、手机号、住址等个人敏感信息。