Skip to content

Commit 53b17e6

Browse files
committed
Since Azure has implement gpt-3's printer effect, disable gpt-3's tune
1 parent 9541900 commit 53b17e6

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@
55

66
> 大多数 OpenAI 客户端不支持 Azure OpenAI Service,但Azure OpenAI Service的申请和绑卡都非常简单,并且还提供了免费的额度。此脚本使用免费的 Cloudflare Worker 作为代理,使得支持 OpenAI 的客户端可以直接使用 Azure OpenAI Service。
77
8+
### 项目说明:
9+
- 我没有服务器可以使用吗?
10+
- 这段脚本跑在Cloudflare Worker, 不需要服务器, 不需要绑卡, 每天10W次请求 免费
11+
- 我没有自己的域名可以使用吗?
12+
- 也可以, 参考: https://github.com/haibbo/cf-openai-azure-proxy/issues/3
13+
- 实现打印机模式:
14+
- Azure OpenAI Service's 回复是一段一段回复的(当前仅GPT-4有此问题, GPT-3的问题Azure已经解决)
15+
- 返回给客户端的时候, 本项目拆出一条条的消息, 依次给, 达到打印机模式
16+
- 项目也支持 Docker 部署(基于 wrangler)
817

9-
18+
### 部署
1019
代理 OpenAI 的请求到 Azure OpenAI Serivce,代码部署步骤:
1120

1221
1. 注册并登录到 Cloudflare 账户
1322
2. 创建一个新的 Cloudflare Worker
1423
3.[cf-openai-azure-proxy.js](./cf-openai-azure-proxy.js) 复制并粘贴到 Cloudflare Worker 编辑器中
15-
4. 通过修改或环境变量调整 resourceName 和 deployName 的值
24+
4. 通过修改或环境变量调整 resourceName 和 deployment mapper 的值
1625
5. 保存并部署 Cloudflare Worker
1726
6. https://github.com/haibbo/cf-openai-azure-proxy/issues/3 **可选**绑定自定义域名: 在 Worker 详情页 -> Trigger -> Custom Domains 中为这个 Worker 添加一个自定义域名
1827

@@ -29,25 +38,19 @@
2938
// The name of your Azure OpenAI Resource.
3039
const resourceName="codegpt"
3140

32-
const mapper:any = {
41+
// deployment model mapper
42+
const mapper = {
3343
'gpt-3.5-turbo': 'gpt35',
3444
'gpt-4': 'gpt4'
3545
};
3646
其他的map规则直接按这样的格式续写即可
3747
```
3848
- 或者通过 cloudflare worker 控制台, 进入 Workers script > Settings > Add variable under Environment Variables.
39-
<img width="777" src="https://user-images.githubusercontent.com/1295315/233108978-bca97ee8-33c9-42e2-a05b-2b15f2e2078f.png" alt="env" />
49+
<img width="777" src="https://user-images.githubusercontent.com/1295315/233124125-1ea95665-ffab-4b5c-a7ba-26f31f1bb0b3.png" alt="env" />
4050

4151
### 客户端
4252
以 OpenCat 为例: 自定义 API 域名填写 第六步绑定的域名:
4353

4454
<img width="339" src="https://user-images.githubusercontent.com/1295315/229820705-ab2ad1d1-8795-4670-97b4-16a0f9fdebba.png" alt="opencat" />
4555

4656
我已经尝试了多种客户端, 如果遇到其他客户端有问题, 欢迎创建issue.
47-
48-
QA:
49-
- 我没有服务器可以使用吗?
50-
- 这段脚本跑在Cloudflare Worker, 不需要服务器, 不需要绑卡, 每天10W次请求 免费
51-
- 我没有自己的域名可以使用吗?
52-
- 也可以, 参考: https://github.com/haibbo/cf-openai-azure-proxy/issues/3
53-

README_en.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This script proxies requests to Azure OpenAI Service for OpenAI clients. The cod
1010
Register and log in to your Cloudflare account.
1111
- Create a new Cloudflare Worker.
1212
- Copy and paste cf-openai-azure-proxy.js into the Cloudflare Worker editor.
13-
- Adjust the values of **resourceName** and **deployName** by either direct modification or using environment variables..
13+
- Adjust the values of **resourceName** and deployment **mapper** by either direct modification or using environment variables..
1414
- Save and deploy the Cloudflare Worker.
1515
- https://github.com/haibbo/cf-openai-azure-proxy/issues/3 Optional: Bind a custom domain name: Add a custom domain name for this worker in the Worker details page -> Trigger -> Custom Domains.
1616

@@ -29,12 +29,10 @@ const resourceName="codegpt"
2929
'gpt-4': 'gpt4'
3030
};
3131
```
32-
**Mapper configuration example**: If you have deployed the GPT-3.5 Turbo and GPT-4 models on Azure with deployment names 'gpt35' and 'gpt4', respectively, then the mapper should be configured as follows.
3332
Other map rules can be continued directly in this format.
33+
- **OR** go to the Cloudflare Worker console, navigate to Workers script > Settings > Add variable under Environment Variables.
3434

35-
36-
- go to the Cloudflare Worker console, navigate to Workers script > Settings > Add variable under Environment Variables.
37-
<img width="777" src="https://user-images.githubusercontent.com/1295315/233108978-bca97ee8-33c9-42e2-a05b-2b15f2e2078f.png" alt="env" />
35+
<img width="777" src="https://user-images.githubusercontent.com/1295315/233124125-1ea95665-ffab-4b5c-a7ba-26f31f1bb0b3.png" alt="env" />
3836

3937
## Client
4038
Take OpenCat as an example: fill in the custom API domain name with the domain name bound in step 6:

cf-openai-azure-proxy.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ const mapper = {
99

1010
const apiVersion="2023-03-15-preview"
1111

12-
13-
1412
addEventListener("fetch", (event) => {
1513
event.respondWith(handleRequest(event.request));
1614
});
@@ -62,8 +60,14 @@ async function handleRequest(request) {
6260
body: typeof body === 'object' ? JSON.stringify(body) : '{}',
6361
};
6462

65-
let { readable, writable } = new TransformStream()
6663
const response = await fetch(fetchAPI, payload);
64+
65+
// Since Azure has fixed gpt-3's printer effect, do not have to stream it.
66+
if (modelName.startsWith('gpt-3') || body?.stream != true){
67+
return response
68+
}
69+
70+
let { readable, writable } = new TransformStream()
6771
stream(response.body, writable);
6872
return new Response(readable, response);
6973

@@ -98,7 +102,7 @@ async function stream(readable, writable) {
98102
// Loop through all but the last line, which may be incomplete.
99103
for (let i = 0; i < lines.length - 1; i++) {
100104
await writer.write(encoder.encode(lines[i] + delimiter));
101-
await sleep(30);
105+
await sleep(50);
102106
}
103107

104108
buffer = lines[lines.length - 1];

0 commit comments

Comments
 (0)