Skip to content

Commit

Permalink
Merge pull request #11 from hbsgithub/main
Browse files Browse the repository at this point in the history
add support for mapper
  • Loading branch information
haibbo authored Apr 19, 2023
2 parents 5a65465 + f355777 commit 0efba89
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
5. 保存并部署 Cloudflare Worker
6. https://github.com/haibbo/cf-openai-azure-proxy/issues/3 **可选**绑定自定义域名: 在 Worker 详情页 -> Trigger -> Custom Domains 中为这个 Worker 添加一个自定义域名


### 使用说明

先得到 resourceName 和 deployName, 登录到Azure的后台:
Expand All @@ -28,8 +29,11 @@
// The name of your Azure OpenAI Resource.
const resourceName="codegpt"

// The deployment name you chose when you deployed the model.
const deployName="gpt3
const mapper:any = {
'gpt-3.5-turbo': 'gpt35',
'gpt-4': 'gpt4'
};
其他的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" />
Expand Down
10 changes: 8 additions & 2 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ First obtain the resourceName and deployName, and log in to the Azure portal:
// The name of your Azure OpenAI Resource.
const resourceName="codegpt"

// The deployment name you chose when you deployed the model.
const deployName="gpt3
const mapper:any = {
'gpt-3.5-turbo': 'gpt35',
'gpt-4': 'gpt4'
};
```
**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.
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" />

Expand Down
25 changes: 22 additions & 3 deletions cf-openai-azure-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
const resourceName=RESOURCE_NAME

// The deployment name you chose when you deployed the model.
const deployName=DEPLOY_NAME

// 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.
};


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

Expand All @@ -26,12 +34,23 @@ async function handleRequest(request) {
} else {
return new Response('404 Not Found', { status: 404 })
}

const fetchAPI = `https://${resourceName}.openai.azure.com/openai/deployments/${deployName}/${path}?api-version=${apiVersion}`

// 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 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

0 comments on commit 0efba89

Please sign in to comment.