Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class ChatController {

@Get()
getHello(): string {
return this.appService.getHello();
return this.appService.getVersion();
}

@Post('completions')
Expand Down
32 changes: 28 additions & 4 deletions src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { HttpService } from '@nestjs/axios';
import { Header, Injectable } from '@nestjs/common';
import { Header, Injectable, Logger } from '@nestjs/common';
import { models } from './models';
import { firstValueFrom } from 'rxjs';

@Injectable()
export class AppService {
private readonly logger = new Logger(AppService.name);
constructor(private readonly httpService: HttpService) {}

@Header('Content-Type', 'application/json')
getModels() {
return models;
}

getHello(): string {
return 'Hello World! 1.8';
getVersion(): string {
return 'Hello World! 2.0';
}

async getCompletions(
endpoint: string,
deployment_id: string,
mapping: string,
azureApiKey: string,
body: any,
stream: boolean,
) {
const deployment_id = this.getDeploymentId(mapping, body['model']);
this.logger.debug(`deployment_id: ${deployment_id}`);
const url = `${endpoint}/openai/deployments/${deployment_id}/chat/completions?api-version=2023-03-15-preview`;
const headers = {
'api-key': azureApiKey,
Expand All @@ -35,4 +38,25 @@ export class AppService {
const ret = this.httpService.post(url, body, config);
return await firstValueFrom(ret);
}
private getDeploymentId(mapping: string, model: string): string {
this.logger.debug(`mapping: ${mapping}, model: ${model}`);
if (mapping.includes(',')) {
let defaultDeploymentId = '';
const modelMapping = mapping
.split(',')
.reduce((acc: Record<string, string>, pair: string) => {
const [key, value] = pair.split('|');
if (defaultDeploymentId === '') defaultDeploymentId = value;
acc[key] = value;
return acc;
}, {});
if (!model) {
return defaultDeploymentId;
}
const deploymentId = modelMapping[model];
return deploymentId || defaultDeploymentId;
} else {
return mapping;
}
}
}
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule, {
logger: ['log'],
});
app.setGlobalPrefix('v1');
await app.listen(3000);
}
Expand Down