---
title: "Azure OpenAI Service 注册申请与配置心得"
date: 2023-04-24T21:21:11.000Z
author: Z.SHINCHVEN
tags: [Azure, Azure OpenAI Service, GPT, Cloudflare, Cloudflare Workers]
canonical: https://atlassc.net/2023/04/24/azure-openai-service
---
Microsoft 的 [Azure](https://azure.microsoft.com/) 上已经推出 [Azure OpenAI Service](https://azure.microsoft.com/en-us/products/cognitive-services/openai-service) 试用，在 [申请](https://aka.ms/oai/access?azure-portal=true) 到测试资格以后便可以部署私有的 OpenAI GPT-3.5 模型 API。同时还可以 [申请 GPT-4 模型](https://aka.ms/oai/get-gpt4) 的试用资格。

> 💡 更新：**GPT-4 模型已经不需要额外申请**
>
> 当你开通 Azure OpenAI Service 以后会同时开通 GPT-4，但是根据旧申请表上的信息，新注册的用户仅能在有限的几个国家/地区使用 GPT-4 模型：
> - Sweden Central
> - Canada East
> - Switzerland North
>
> 请去 [Azure AI Studi 配额页](https://ai.azure.com/quota) 查看你的账号在哪个地区拥有 GPT-4 的配额，如果没有配额，可以在`配额`页面直接填表申请。


私有部署的好处显而易见，它稳定可靠速度快，最重要的是再也不用担心被 OpenAI 无厘头的风控政策反复折磨了。

<div style="display: flex; align-items: center; justify-content: center;"><iframe width="560" height="315" src="https://www.youtube.com/embed/I-5IgYl8N7g" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe></div>

## 注册 Azure 账号的心得

### 注册国版 Azure 账号

Azure OpenAI Service 是 Azure Cognitive Services 的一部分，所以需要先注册 Azure 账号。目前只在 Azure 国际版上提供，所以请到 [Azure 国际版](https://azure.microsoft.com/) 上注册账号。

### 地区与手机号验证

![Azure Phone Number](https://blogimgs.atlassc.net/images/azure-openai-service/azure-phone-number-region-code.jpg)

注册时可以选择 `中国` 地区，这样你可以直接输入 `+86` 的手机号进行验证。注册中国地区的账号也可以申请 Azure OpenAI Service。

如果你选择了其他的 `国家/地区`，但你仍然想使用 `+86` 的手机号进行验证，你可以先输入一个你选择的地区的手机号，然后在 `电话` 下面勾选 `请使用其他电话号码来码证你的身份`，此时会展开一个新的电话号码输入框并让你手动选择手机号的 `国家/地区代码` ，选择 `中国`，然后输入 `+86` 的手机号即可进行验证。

一旦注册，当前账号的 `国家/地区` 将无法更改。

### 信用卡验证

账单地址应该与你选 `国家/地区` 保持一致，否则会提示注册失败。

## 申请开通 Azure OpenAI Service

[Azure OpenAI Service](https://azure.microsoft.com/en-us/products/cognitive-services/openai-service) 目前需要以企业的身份来申请试用。如果你的 Azure 账号已经注册好了，可以点击 [这里](https://go.microsoft.com/fwlink/?linkid=2222006&clcid=0x409&culture=en-us&country=us) 来申请。

Azure OpenAI Service 接受来自中国的企业申请，请依照表单 `如实填写` 信息，其中最重要的是：
- Your Company Email Address：请填写你在你公司的企业邮箱地址。
- Your Company Name：请填写你所在公司的名称。
- Azure Subscription ID：请根据表单内的提示来获取你的 Azure `Subscription ID`，千万不要填错成 `Tenant ID` 了，否则无法通过或者看不到订价层。
- Company Website：请填写你所在公司的网站的网址。网址里最好能展示一个与你企业邮箱地址同一个域名的邮箱地址。
- 你的企业邮箱的域名、公司网站的域名需要保持一致，域名的主体需要与公司的名称保持一致，否则会在审核时被拒绝。
- 如果你填写的资料存在明显的问题，Azure 甚至不会给你任何回复。
- 如果你填写的资料还需要进一步的证明材料，Azure 会给你发送邮件要求提供。

运气好的话，一般两天能通过审核，如果超过两周没有收到审核结果，请尝试重新提交申请。

申请通过的邮件会显示有`Onboarding to Azure OpenAI Service`的字样。

## 部署 Azure OpenAI Service

恭喜你已经申请到了 Azure OpenAI Service 的试用资格，接下来就是部署 Azure OpenAI Service 了。

![Azure services](https://blogimgs.atlassc.net/images/azure-openai-service/azure-services.jpg)

登录到 Azure 以后，你会在首页的 `Azure services` 里面看到 `Azure OpenAI` 的图标，点击进入来创建部署 OpenAI Service 资源。

- 在 `创建 Azure OpenAI` 的页面，`订阅` - `资源组`一栏如果没有，新建一个即可。
- 在 `名称` 一栏根据要求输入一个简短好记的资源名，未来需要通过它来访问你的私有 OpenAI API。
- `定价层` 选择默认选项。
- `标记` 一页，如果你不需要通过 tags 来管理自己的资源，可暂时不填。
- 在 `审阅+提交` 之后，即可进入部署阶段，部署资源需要几分钟时间，请耐心等待，待部署成功之后便可开始使用。

## 部署模型

![Models](https://blogimgs.atlassc.net/images/azure-openai-service/deploy-models.jpg)

部署 `Azure OpenAI Service` 资源成功后，还要为服务资源 `部署模型` 才可以使用。

![Create deployment](https://blogimgs.atlassc.net/images/azure-openai-service/create-deployment.jpg)

请进到资源详情页面左边栏的 `模型部署` 中，点击 `创建` 按钮来部署模型。

- 输入 `模型部署名称`，此名称在后面使用 `Cloudflare Worker` 转换映射API时要用到。
- 选择模型，建议使用 `gpt-35-turbo (version 0301)`，该模型与 `ChatGPT 3.5` 一致。
- 点击 `保存` 以后即可完成部署。

在模型部署完以后，就可以 `转到 Azure OpenAI Studio` 中进行试用。

## Azure OpenAI Studio

这里和 `OpenAI` 的功能比较类似：
- Completions playground：可调用模型根据已输入的文本生成补全文本，与 `GitHub Copilot` 和 `Bing Chat` 的输入框自动补完功能类似。
- Chat playground(Preview)：一个类似 ChatGPT 的聊天式AI助手应用。
- 在两个 playground 中，你都可以点击 `View code` 来查看当前操作的示例代码，供你学习怎样开发自己的 GPT 应用。

## 配置客户端

我们部署 `Azure OpenAI Service` 资源的主要目的是为了能在客户端中使用 `GPT` 或其他 `OpenAI` 模型的能力，所以接下来我们要将 `Azure OpenAI Service API` 配置到流行的 OpenAI 客户端中。

### 获取 API KEY 和 API URL

在配置客户端之前，请先去 `Azure OpenAI Service` 的 `资源详情页面` 中获取 `密钥` 与 `终结点` 中。

- 密钥：作为客户端中的 `API KEY` 使用。
- 终结点：作为 `API URL` 或者 `Endpoint` 使用。

### 配置 Azure OpenAI Service API

像[openai-translator](https://github.com/yetone/openai-translator)和[ChatBox](https://github.com/Bin-Huang/chatbox)这样的客户端已经原生支持 `Azure OpenAI Service` 的API，这里以 `openai-translator` 为例介绍一下配置的要点：

```typescript
// API URL 或者 终结点
const API_URL = `https://${resourceName}.openai.azure.com`
// API URL Path 为你的 Azure OpenAI Service API 路径，主要负责调用模型和指定API版本
const API_URL_PATH = `/openai/deployments/${deployName}/completions?api-version=${apiVersion}`
```

- resourceName: 填写你的 Azure OpenAI Service 资源名称。
- deployName: 填写你的 Azure OpenAI Service `模型部署名称`，更改部署名称以切换模型。
- api-version: 填写“2023-05-15”，或者更新的版本。

如果你使用 `ChatBox`，无需构造 `API_URL_PATH`，只需要填写 `模型部署名称` 即可。

### Cloudflare 转换 Azure OpenAI Service API

`Azure OpenAI Service API` 和 `OpenAI Service API` 的接口不一致。如果你的第三方客户端不支持 `Azure OpenAI Service API`，你可以使用 [cf-openai-azure-proxy](https://github.com/haibbo/cf-openai-azure-proxy) 这个项目来部署一个 `Cloudflare Worker`，将` Azure OpenAI Service API` 映射到 `OpenAI Service`。

部署 `Worker` 时，可参照下面的代码对进行配置：

> 🚨 替换参数的时候变忘记写引号

```js
// The name of your Azure OpenAI Resource.
const resourceName="你的 Azure OpenAI 资源名称"

// The deployment name you chose when you deployed the model.
const mapper = { // 模型名称与模型部署名称映射
    'gpt-3.5-turbo': "模型部署名称",
    // 'gpt-4': DEPLOY_NAME_GPT4 // 如果没有 GPT-4 服务可先注释掉
};

const apiVersion="2023-05-15"; //  Azure OpenAI API 版本

// ...
// ...
// ...
// 跳过中间代码找到下面这一句

const deployName = mapper[modelName] || '模型部署名称' // 设置默认使用已部署的模型，而不是空。
```

`CloudFlare Worker` 部署完成之后，我们就可以去配置客户端了。

### 配置 Cloudflare Worker 转换过的 Azure OpenAI Service API 到客户端中

以[openai translator](https://github.com/yetone/openai-translator)为例：
- 请在设置中选择 `OpenAI` 作为 `Default Service Provider`。
- API KEY：请填写 `Azure OpenAI Service` 的 `密钥`。
- API URL：请填写 `Cloudflare Worker` 的 URL。
- API Model 选择你已部署的 Model，例如 `gpt-35-turbo (version 0301)`，而这个Model需要你在 `Cloudflare Worker` 代码最前面的 mapper 中做好映射。

而[ChatBox](https://github.com/Bin-Huang/chatbox)的配置就更简单了：
- 在 `OpenAI API Key` 中填写 `Azure OpenAI Service` 的 `密钥`。
- 展开 `Proxy`，在 `API Host` 中填写 `Cloudflare Worker` 的 URL。
- 展开 `Model & Token`，在 `Model` 中选择你已部署的 Model，例如 `gpt-35-turbo-0301`。

### 打字效果

直接使用 `Azure OpenAI Service API` 可能会看不到打字机的效果，而 [cf-openai-azure-proxy](https://github.com/haibbo/cf-openai-azure-proxy) 对它进行了兼容，因此我更喜欢用它，而不是直接使用 `Azure OpenAI Service API`。

### Content-filtering

`Azure OpenAI Service API` 的 `Content-filtering` 功能是默认开启的，并且不能自行关闭。如果要关闭 `Content-filtering`，需要联系 Azure Cognitive 客服。

如果输入提示词或者输出结果中包含不适宜的内容，会被 `Content-filtering` 过滤掉。

## FAQ

### 🙋 创建 Azure OpenAI Service 资源时无法选择定价层（Pricing Tier）？

![no pricing tier](https://blogimgs.atlassc.net/images/azure-openai-service/no-pricing-tier.jpeg)

请确保你在注册好 Azure 账号以后，单独申请了 Azure OpenAI Service 的试用，否则你将无法创建 Azure OpenAI Service 资源。

申请通过会有邮件回复。

如果申请通过了，而仍然看不到定价层选项，那就得联系 Azure Cognitive 客服了。

刚收到网友反馈是 `Subscription ID` 填错了，请一定根据表单内的指引去获取 `Subscription ID` 而不是 `Tenant ID`。

### 🙋 申请 Azure OpenAI Service 通过以后使用 Azure 账号登录还是用申请 Azure OpenAI Service 的企业邮箱登录？

使用 subscription 相关的 Azure 账号登录，企业邮箱仅用于验证企业身份。

### 🙋 收到了 GPT-4 模型的回信却不能部署 GPT-4 模型？

再读一读邮件，是不是告诉你被加入到 GPT-4 的 `waitlist` 了？而不是通过了申请。

GPT-4 模型连 waitlist 都要申请和审核，审核通过了才进 `waitlist`。微软真有你的。

### 🙋 GPT-4 模型输出变得不流畅了？

- **Content Filters:** 微软后来加了个烦人 Content Filters，生成的内容会在 Azure 上先 Cache 一段，然后再返回给你。要关闭关闭 Content Filters 你可以参考[这篇文章](https://atlassc.net/2023/11/19/turning-off-azure-openai-service-content-filters)

- **Streaming:** 要实流畅的输出，请确保你的服务端程序支持 `Server-Sent Events (SSE)` 并且没有被 Nginx 给缓存，可以[参考这篇文章](https://atlassc.net/2023/12/29/realtime-server-sent-events-held-back-by-nginx)了解如何在程序上解决流式输出被 Nginx 缓存的问题。

## References

- [快速入门：开始将 ChatGPT（预览版）和 GPT-4（预览版）与 Azure OpenAI 服务结合使用](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/chatgpt-quickstart?pivots=programming-language-studio&tabs=command-line)

----

如果顺利申请到了 Azure OpenAI Service，欢迎 `点赞` 打赏支持我继续写作。
