Skip to content

Commit 3c7213d

Browse files
haha1903Hai Chang
andauthored
Update API version handling and READMEs (#9)
for #8 Co-authored-by: Hai Chang <haichang@microsoft.com>
1 parent a624b31 commit 3c7213d

File tree

8 files changed

+1567
-1563
lines changed

8 files changed

+1567
-1563
lines changed

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"printWidth": 120,
23
"singleQuote": true,
34
"trailingComma": "all"
4-
}
5+
}

README.en-US.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ You must have an Azure OpenAI account to use the Azure OpenAI Proxy.
1717
2. Clone the code in the command line window.
1818
3. Run `npm install` to install dependencies.
1919
4. Run `npm start` to start the application.
20-
5. Run the script below for testing, replacing `YOUR_RESOURCE_ID`, `YOUR_MODEL_DEPLOYMENT`, and `YOUR_API_KEY` before running it.
20+
5. Run the script below for testing, replacing `YOUR_RESOURCE_ID`, `YOUR_MODEL_DEPLOYMENT`, and `YOUR_API_KEY` before running it, `AZURE_API_VERSION` is optional and the default value is 2023-03-15-preview..
2121
```bash
2222
curl -X "POST" "http://localhost:3000/v1/chat/completions" \
23-
-H 'Authorization: YOUR_RESOURCE_ID:YOUR_MODEL_DEPLOYMENT:YOUR_API_KEY' \
23+
-H 'Authorization: YOUR_RESOURCE_ID:YOUR_MODEL_DEPLOYMENT:YOUR_API_KEY:AZURE_API_VERSION' \
2424
-H 'Content-Type: application/json; charset=utf-8' \
2525
-d $'{
2626
"messages": [
@@ -59,7 +59,7 @@ Q: How do I support GPT-4?
5959

6060
A: To use GPT-4, please use key format as follows:
6161

62-
`YOUR_RESOURCE_ID:gpt-3.5-turbo|YOUR_MODEL_DEPLOYMENT,gpt-4|YOUR_MODEL_DEPLOYMENT,gpt-4-32k|YOUR_MODEL_DEPLOYMENT:YOUR_API_KEY`
62+
`YOUR_RESOURCE_ID:gpt-3.5-turbo|YOUR_MODEL_DEPLOYMENT,gpt-4|YOUR_MODEL_DEPLOYMENT,gpt-4-32k|YOUR_MODEL_DEPLOYMENT:YOUR_API_KEY:AZURE_API_VERSION`
6363

6464
# How To Contribute Code?
6565

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
2. 克隆代码到命令行窗口。
1818
3. 运行 `npm install` 安装依赖项。
1919
4. 运行 `npm start` 启动应用程序。
20-
5. 运行下面脚本测试,运行前需要把`YOUR_RESOURCE_ID``YOUR_MODEL_DEPLOYMENT``YOUR_API_KEY`替换
20+
5. 运行下面脚本测试,运行前需要把`YOUR_RESOURCE_ID``YOUR_MODEL_DEPLOYMENT``YOUR_API_KEY`, `AZURE_API_VERSION`替换,`AZURE_API_VERSION`参数可选,目前默认是2023-03-15-preview
2121
```bash
2222
curl -X "POST" "http://localhost:3000/v1/chat/completions" \
23-
-H 'Authorization: YOUR_RESOURCE_ID:YOUR_MODEL_DEPLOYMENT:YOUR_API_KEY' \
23+
-H 'Authorization: YOUR_RESOURCE_ID:YOUR_MODEL_DEPLOYMENT:YOUR_API_KEY:AZURE_API_VERSION' \
2424
-H 'Content-Type: application/json; charset=utf-8' \
2525
-d $'{
2626
"messages": [
@@ -59,7 +59,7 @@ Q: 如何支持GPT-4
5959

6060
A: 要使用GPT-4,请使用下列格式的key:
6161

62-
`YOUR_RESOURCE_ID:gpt-3.5-turbo|YOUR_MODEL_DEPLOYMENT,gpt-4|YOUR_MODEL_DEPLOYMENT,gpt-4-32k|YOUR_MODEL_DEPLOYMENT:YOUR_API_KEY`
62+
`YOUR_RESOURCE_ID:gpt-3.5-turbo|YOUR_MODEL_DEPLOYMENT,gpt-4|YOUR_MODEL_DEPLOYMENT,gpt-4-32k|YOUR_MODEL_DEPLOYMENT:YOUR_API_KEY:AZURE_API_VERSION`
6363

6464
# 贡献代码方式
6565

src/app.controller.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, Post, Req, Res } from '@nestjs/common';
1+
import { Controller, Get, Logger, Post, Req, Res } from '@nestjs/common';
22
import { AxiosHeaders } from 'axios';
33
import { Request, Response } from 'express';
44
import { AppService } from './app.service';
@@ -13,8 +13,11 @@ export class AppController {
1313
}
1414
}
1515

16+
const DEFAULT_API_VERSION = '2023-03-15-preview';
17+
1618
@Controller('chat')
1719
export class ChatController {
20+
private readonly logger = new Logger(ChatController.name);
1821
constructor(private readonly appService: AppService) {}
1922

2023
@Get()
@@ -26,7 +29,10 @@ export class ChatController {
2629
async completions(@Req() request: Request, @Res() res: Response) {
2730
const auth = request.headers['authorization'];
2831
const apiKey = auth.replace('Bearer ', '');
29-
const [resource_id, deployment_id, azureApiKey] = apiKey.split(':');
32+
const [resource_id, deployment_id, azureApiKey, apiVersion] = apiKey.split(':');
33+
this.logger.debug(
34+
`resource_id: ${resource_id}, deployment_id: ${deployment_id}, azureApiKey: ${azureApiKey}, apiVersion: ${apiVersion}`,
35+
);
3036
const endpoint = `https://${resource_id}.openai.azure.com`;
3137
const stream = request.body['stream'];
3238
const response = await this.appService.getCompletions(
@@ -35,6 +41,7 @@ export class ChatController {
3541
azureApiKey,
3642
request.body,
3743
stream,
44+
apiVersion || DEFAULT_API_VERSION,
3845
);
3946

4047
// set response headers

src/app.service.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ export class AppService {
2323
azureApiKey: string,
2424
body: any,
2525
stream: boolean,
26+
apiVersion: string,
2627
) {
2728
const deployment_id = this.getDeploymentId(mapping, body['model']);
2829
this.logger.debug(`deployment_id: ${deployment_id}`);
29-
const url = `${endpoint}/openai/deployments/${deployment_id}/chat/completions?api-version=2023-03-15-preview`;
30+
const url = `${endpoint}/openai/deployments/${deployment_id}/chat/completions?api-version=${apiVersion}`;
3031
const headers = {
3132
'api-key': azureApiKey,
3233
'Content-Type': 'application/json',
@@ -46,14 +47,12 @@ export class AppService {
4647
this.logger.debug(`mapping: ${mapping}, model: ${model}`);
4748
if (mapping.includes(',')) {
4849
let defaultDeploymentId = '';
49-
const modelMapping = mapping
50-
.split(',')
51-
.reduce((acc: Record<string, string>, pair: string) => {
52-
const [key, value] = pair.split('|');
53-
if (defaultDeploymentId === '') defaultDeploymentId = value;
54-
acc[key] = value;
55-
return acc;
56-
}, {});
50+
const modelMapping = mapping.split(',').reduce((acc: Record<string, string>, pair: string) => {
51+
const [key, value] = pair.split('|');
52+
if (defaultDeploymentId === '') defaultDeploymentId = value;
53+
acc[key] = value;
54+
return acc;
55+
}, {});
5756
if (!model) {
5857
return defaultDeploymentId;
5958
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AppModule } from './app.module';
33

44
async function bootstrap() {
55
const app = await NestFactory.create(AppModule, {
6-
logger: ['log'],
6+
logger: ['debug'],
77
});
88
app.setGlobalPrefix('v1');
99
await app.listen(3000);

0 commit comments

Comments
 (0)