-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
102 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
# Building layer | ||
FROM node:18-alpine as development | ||
# Optional NPM automation (auth) token build argument | ||
# ARG NPM_TOKEN | ||
# Optionally authenticate NPM registry | ||
# RUN npm set //registry.npmjs.org/:_authToken ${NPM_TOKEN} | ||
WORKDIR /app | ||
|
||
# Copy configuration files | ||
COPY tsconfig*.json ./ | ||
COPY package*.json ./ | ||
# Install dependencies from package-lock.json, see https://docs.npmjs.com/cli/v7/commands/npm-ci | ||
|
||
# Install dependencies from package-lock.json | ||
RUN npm ci | ||
|
||
# Copy application sources (.ts, .tsx, js) | ||
COPY src/ src/ | ||
|
||
# Build application (produces dist/ folder) | ||
RUN npm run build | ||
|
||
# Runtime (production) layer | ||
FROM node:18-alpine as production | ||
# Optional NPM automation (auth) token build argument | ||
# ARG NPM_TOKEN | ||
# Optionally authenticate NPM registry | ||
# RUN npm set //registry.npmjs.org/:_authToken ${NPM_TOKEN} | ||
WORKDIR /app | ||
|
||
# Copy dependencies files | ||
COPY package*.json ./ | ||
# Install runtime dependecies (without dev/test dependecies) | ||
RUN npm ci --omit=dev | ||
|
||
# Install runtime dependencies (without dev/test dependencies) | ||
RUN npm ci --only=production | ||
|
||
# Copy production build | ||
COPY --from=development /app/dist/ ./dist/ | ||
|
||
# Expose application port | ||
EXPOSE 3000 | ||
|
||
# Start application | ||
CMD [ "node", "dist/main.js" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,83 @@ | ||
import { Controller, Get, Logger, Post, Req, Res } from '@nestjs/common'; | ||
import { AxiosHeaders } from 'axios'; | ||
import { Controller, Get, Post, Req, Res, Logger } from '@nestjs/common'; | ||
import { Request, Response } from 'express'; | ||
import { AxiosHeaders } from 'axios'; | ||
import { AppService } from './app.service'; | ||
|
||
const DEFAULT_API_VERSION = '2023-03-15-preview'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
|
||
@Get('models') | ||
models() { | ||
getModels() { | ||
return this.appService.getModels(); | ||
} | ||
} | ||
|
||
const DEFAULT_API_VERSION = '2023-03-15-preview'; | ||
|
||
@Controller('chat') | ||
export class ChatController { | ||
private readonly logger = new Logger(ChatController.name); | ||
|
||
constructor(private readonly appService: AppService) {} | ||
|
||
@Get() | ||
getHello(): string { | ||
getVersion(): string { | ||
return this.appService.getVersion(); | ||
} | ||
|
||
@Post('completions') | ||
async completions(@Req() request: Request, @Res() res: Response) { | ||
async postCompletions(@Req() request: Request, @Res() response: Response) { | ||
const auth = request.headers['authorization']; | ||
const apiKey = auth.replace('Bearer ', ''); | ||
const [resource_id, deployment_id, azureApiKey, apiVersion] = apiKey.split(':'); | ||
const [resourceId, deploymentId, azureApiKey, apiVersion] = apiKey.split(':'); | ||
this.logger.debug( | ||
`resource_id: ${resource_id}, deployment_id: ${deployment_id}, azureApiKey: ${azureApiKey}, apiVersion: ${apiVersion}`, | ||
`resourceId: ${resourceId}, deploymentId: ${deploymentId}, azureApiKey: ${azureApiKey}, apiVersion: ${apiVersion}`, | ||
); | ||
const endpoint = `https://${resource_id}.openai.azure.com`; | ||
|
||
const endpoint = `https://${resourceId}.openai.azure.com`; | ||
const stream = request.body['stream']; | ||
const response = await this.appService.getCompletions( | ||
const openaiResponse = await this.appService.getCompletions( | ||
endpoint, | ||
deployment_id, | ||
deploymentId, | ||
azureApiKey, | ||
request.body, | ||
stream, | ||
apiVersion || DEFAULT_API_VERSION, | ||
); | ||
|
||
// set response headers | ||
for (const [key, value] of response.headers as AxiosHeaders) { | ||
res.header[key] = value; | ||
// Set response headers | ||
if (openaiResponse.headers instanceof Array) { | ||
for (const [key, value] of openaiResponse.headers as AxiosHeaders) { | ||
response.header[key] = value; | ||
} | ||
} | ||
|
||
if (openaiResponse.status < 200 || openaiResponse.status >= 300) { | ||
this.logger.error( | ||
`The OpenAI has returned an error with status code ${openaiResponse.status} and message ${openaiResponse.statusText}`, | ||
); | ||
} | ||
res.status(response.status); | ||
|
||
response.status(openaiResponse.status); | ||
|
||
if (stream) { | ||
const streamData = response.data; | ||
const streamData = openaiResponse.data; | ||
streamData.on('data', (data) => { | ||
res.write(data); | ||
// Checks for the specific newline character returned by Azure OpenAI and | ||
// replaces it with the expected newline character used by OpenAI | ||
const decodedData = data.toString('utf8'); | ||
if (decodedData.includes('data: [DONE]')) { | ||
response.write(`${decodedData}\n`); | ||
} else { | ||
response.write(data); | ||
} | ||
}); | ||
streamData.on('end', () => { | ||
res.end(); | ||
response.end(); | ||
}); | ||
} else { | ||
res.send(response.data); | ||
response.send(openaiResponse.data); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export let models = { | ||
export const models = { | ||
object: 'list', | ||
data: [ | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters