Skip to content

Commit 92e5537

Browse files
committed
add support for mapper
1 parent 8b56209 commit 92e5537

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

main.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import { serve } from "https://deno.land/std@0.181.0/http/server.ts";
44
const resourceName:string = "your-resource-name";
55

66
// The deployment name you chose when you deployed the model.
7-
const deployName:string = "deployment-name";
7+
// const deployName:string = "deployment-name";
88

99
const apiVersion:string = "2023-03-15-preview";
10+
// 自定义 mapper 来映射 model 字段
11+
const mapper:any = {
12+
'gpt-3.5-turbo': 'gpt35'
13+
// 其他映射规则可以在这里添加
14+
};
1015

1116
async function handleRequest(request:Request):Promise<Response> {
1217
if (request.method === 'OPTIONS') {
@@ -24,12 +29,25 @@ async function handleRequest(request:Request):Promise<Response> {
2429
} else {
2530
return new Response('404 Not Found', { status: 404 })
2631
}
27-
28-
const fetchAPI:string = `https://${resourceName}.openai.azure.com/openai/deployments/${deployName}/${path}?api-version=${apiVersion}`;
32+
33+
34+
// 获取 model 字段的值,并进行映射
35+
let deployName:string = '';
2936
let body:any;
3037
if (request.method === 'POST') {
3138
body = await request.json();
39+
const modelName:string|undefined = body?.model;
40+
if (modelName) {
41+
deployName = mapper[modelName] || modelName;
42+
}
3243
}
44+
45+
const fetchAPI:string = `https://${resourceName}.openai.azure.com/openai/deployments/${deployName}/${path}?api-version=${apiVersion}`;
46+
47+
// let body:any;
48+
// if (request.method === 'POST') {
49+
// body = await request.json();
50+
// }
3351
const authKey:string|null = request.headers.get('Authorization');
3452
if (!authKey) {
3553
return new Response("Not allowed", {status: 403});
@@ -55,6 +73,7 @@ async function handleRequest(request:Request):Promise<Response> {
5573

5674
}
5775

76+
5877
function sleep(ms:number):Promise<void> {
5978
return new Promise(resolve => setTimeout(resolve, ms));
6079
}

0 commit comments

Comments
 (0)