Skip to content

Commit

Permalink
Adjust mapper with env Variables, Thanks #13
Browse files Browse the repository at this point in the history
  • Loading branch information
haibbo committed Apr 19, 2023
1 parent 0efba89 commit 9541900
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ RUN npm install -g [email protected]

ENV WRANGLER_SEND_METRICS=false

ENV DEPLOY_NAME_GPT35=""
ENV DEPLOY_NAME_GPT4=""

# 复制 Workers 脚本到镜像
COPY cf-openai-azure-proxy.js .

# 启动本地开发服务器
CMD wrangler dev cf-openai-azure-proxy.js --local --var RESOURCE_NAME:$RESOURCE_NAME DEPLOY_NAME:$DEPLOY_NAME
CMD wrangler dev cf-openai-azure-proxy.js --local --var RESOURCE_NAME:$RESOURCE_NAME DEPLOY_NAME_GPT35:$DEPLOY_NAME_GPT35 DEPLOY_NAME_GPT4:$DEPLOY_NAME_GPT4
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const mapper:any = {
其他的map规则直接按这样的格式续写即可
```
- 或者通过 cloudflare worker 控制台, 进入 Workers script > Settings > Add variable under Environment Variables.
<img width="777" src="https://user-images.githubusercontent.com/1295315/232183839-b4baa414-76d4-4ccd-8d27-440edfab1404.png" alt="env" />
<img width="777" src="https://user-images.githubusercontent.com/1295315/233108978-bca97ee8-33c9-42e2-a05b-2b15f2e2078f.png" alt="env" />

### 客户端
以 OpenCat 为例: 自定义 API 域名填写 第六步绑定的域名:
Expand Down
2 changes: 1 addition & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Other map rules can be continued directly in this format.


- go to the Cloudflare Worker console, navigate to Workers script > Settings > Add variable under Environment Variables.
<img width="777" src="https://user-images.githubusercontent.com/1295315/232183839-b4baa414-76d4-4ccd-8d27-440edfab1404.png" alt="env" />
<img width="777" src="https://user-images.githubusercontent.com/1295315/233108978-bca97ee8-33c9-42e2-a05b-2b15f2e2078f.png" alt="env" />

## Client
Take OpenCat as an example: fill in the custom API domain name with the domain name bound in step 6:
Expand Down
49 changes: 25 additions & 24 deletions cf-openai-azure-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
const resourceName=RESOURCE_NAME

// The deployment name you chose when you deployed the model.

// const deployName="deployment-name"
// The mapping of model name.
const mapper = {
'gpt-3.5-turbo': 'gpt35',
'gpt-4': 'gpt4'
// Other mapping rules can be added here.
'gpt-3.5-turbo': DEPLOY_NAME_GPT35,
'gpt-4': DEPLOY_NAME_GPT4
};


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



addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
Expand All @@ -34,23 +30,22 @@ async function handleRequest(request) {
} else {
return new Response('404 Not Found', { status: 404 })
}

// Get the value of the model field and perform mapping.
let deployName;

let body;
if (request.method === 'POST') {
body = await request.json();
const modelName = body?.model;
if (modelName) {
deployName = mapper[modelName] || modelName;
}
}


const modelName = body?.model;
const deployName = mapper[modelName] || ''

if (deployName === '') {
return new Response('Missing model mapper', {
status: 403
});
}
const fetchAPI = `https://${resourceName}.openai.azure.com/openai/deployments/${deployName}/${path}?api-version=${apiVersion}`
// let body;
// if (request.method === 'POST') {
// body = await request.json();
// }

const authKey = request.headers.get('Authorization');
if (!authKey) {
return new Response("Not allowed", {
Expand Down Expand Up @@ -119,8 +114,12 @@ async function stream(readable, writable) {
async function handleModels(request) {
const data = {
"object": "list",
"data": [ {
"id": "gpt-3.5-turbo",
"data": []
};

for (let key in mapper) {
data.data.push({
"id": key,
"object": "model",
"created": 1677610602,
"owned_by": "openai",
Expand All @@ -138,10 +137,11 @@ async function handleModels(request) {
"group": null,
"is_blocking": false
}],
"root": "gpt-3.5-turbo",
"root": key,
"parent": null
}]
};
});
}

const json = JSON.stringify(data, null, 2);
return new Response(json, {
headers: { 'Content-Type': 'application/json' },
Expand All @@ -157,3 +157,4 @@ async function handleOPTIONS(request) {
}
})
}

0 comments on commit 9541900

Please sign in to comment.