> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-tutorial.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI API密钥生成与测试指南

> OpenAI API密钥生成与测试指南。

* 翻译：[xiaoshun007](https://github.com/xiaoshun007)
* 作者: [Minji](https://github.com/r14minji)
* 评审: [Chang-Jun Lee](https://powersolution.notion.site/), [Wooseok Jeong](https://github.com/jeong-wooseok)
* 校对 : [Q0211](https://github.com/Q0211)
* 本内容是 [LangChain Open Tutorial](https://github.com/LangChain-OpenTutorial/LangChain-OpenTutorial) 的一部分

<Card title="打开Colab" href="https://colab.research.google.com/github/LangChain-OpenTutorial/LangChain-OpenTutorial/blob/main/01-Basic/03-OpenAIAPI-Key-Generation.ipynb" img="https://colab.research.google.com/assets/colab-badge.svg" />

<Card title="打开github" href="https://github.com/LangChain-OpenTutorial/LangChain-OpenTutorial/blob/main/01-Basic/03-OpenAIAPI-Key-Generation.ipynb" img="https://img.shields.io/badge/Open%20in%20GitHub-181717?style=flat-square&logo=github&logoColor=white" />

## 概述

本指南详细说明OpenAI API密钥的获取方式、使用规范及相关成本限制。

首先需要在OpenAI平台创建API密钥，并完成开发环境配置。

完成设置后，请对API密钥进行测试，以确保其能在应用程序中正常运行。

### 目录

* [概述](#概述)
* [OpenAI API支付与使用限额设置](#openai-api支付与使用限额设置)
* [OpenAI API密钥生成指南](#openai-api密钥生成指南)
* [环境配置](#环境配置)

***

## OpenAI API支付与使用限额设置

访问 OpenAI API 网站

* 访问: [https://platform.openai.com/docs/overview](https://platform.openai.com/docs/overview)

点击右上角的 **"注册"** 创建账户（若已有账户，请点击 **"登录"**）。

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-01.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=4990e55cb63792ac5e9463bab7ba205a" alt="step1" width="1469" height="791" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-01.png" />

登录后，请点击右上角 **设置** 菜单并选择\*\*"Billing"\*\*选项

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-02.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=3279b60455f38a8749801242ff638780" alt="step2" width="1459" height="792" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-02.png" />

在左侧 **计费** 菜单中，点击 **付款方式** 完成信用卡绑定

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-03.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=f06e0dd8f9e9121c0ab69a7938897378" alt="step3" width="1464" height="791" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-03.png" />

信用卡成功绑定后，将如下方图示显示在付款方式列表中。

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-04.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=768726837fe813edbd7abd9d8fbb084a" alt="step4" width="1459" height="788" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-04.png" />

点击 **"充值余额"** 按钮，输入您要充值的美元金额。

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-05.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=f935a1af8057ed39f0c9e11f974bb37d" alt="step5" width="1464" height="791" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-05.png" />

最低充值金额为 `5美元`（即单次充值不得低于此金额）。

输入金额后，点击 **"继续"** 完成支付流程。

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-06.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=48c58d65a8163e445c5edfac8666ac10" alt="step6" width="1462" height="792" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-06.png" />

在左侧 **限额** 标签页中，您可以设置月度使用限制：

* **"设置月度预算"**：用于设定月度支出上限。达到该金额后系统将停止计费并中止API服务。

* **"设置邮件通知阈值"**：可指定触发邮件提醒的金额。当用量达到该阈值时，您将收到邮件通知。

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-07.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=ae0fdc6b034acb3990352dada0601c71" alt="step7" width="1463" height="786" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-07.png" />

## OpenAI API密钥生成指南

点击右上角个人头像，然后选择 **"个人信息"**。

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-08.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=584d79991b3f9d988c60bec6e897ade5" alt="step7" width="1466" height="605" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-08.png" />

进入API密钥管理菜单

* 访问: [https://platform.openai.com/api-keys](https://platform.openai.com/api-keys)

点击 **"创建新的密钥"**

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-09.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=b4272f008b02cdde3b518ad765659375" alt="step8" width="1461" height="779" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-09.png" />

输入密钥名称并选择项目（若未创建过项目，请选择默认项目）

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-10.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=0e9d2367bfb435908dfa72969cbc4e13" alt="step9" width="1461" height="757" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-10.png" />

点击右侧 **"复制"** 按钮保存密钥。

**重要警告！！！**

若密钥泄露，他人可使用您的API密钥调用GPT服务，产生的费用将计入 **您的账户**。
**请勿与他人共享密钥，务必将其保存在安全位置！**（请将其视为密码妥善保管）

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-11.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=fe92c4c943f14c0b0747b6106a20c52b" alt="step10" width="1461" height="730" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-11.png" />

## 环境配置

配置 .env 文件

1. 在项目根目录下创建 `.env` 文件
2. 在文件中输入 `OPENAI_API_KEY=复制的密钥`，按 Ctrl + S（Mac 用户使用 Command + S）保存并关闭文件

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-12.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=fbea9fcf836b6485fc71fc98923f4a71" alt="step12" width="1172" height="120" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-12.png" />

配置.gitignore文件

1. 在项目根目录创建 `.gitignore` 文件
2. 在文件中添加 `.env` 以避免该文件被Git追踪

<img src="https://mintcdn.com/langchain-tutorial/vLp0ew_ljuvgPQv4/lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-13.png?fit=max&auto=format&n=vLp0ew_ljuvgPQv4&q=85&s=e2c177702ec45e354d80a2c64033132c" alt="step13" width="1031" height="126" data-path="lang-chain/01-Basic/img/02-OpenAIAPIKeyGeneration-13.png" />

```python theme={null}
# 更新 LangChain
!pip install langchain-opentutorial
```

```python theme={null}
# Configuration file to manage API KEY as environment variable
# Install: pip install python-dotenv
from dotenv import load_dotenv

# Load API KEY information
load_dotenv()
```

```python theme={null}
True
```

现在来验证API密钥是否配置正确：

```python theme={null}
import os
print(f"[API KEY]\n{os.environ['OPENAI_API_KEY']}")
```
