Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 13a5230

Browse files
authored
fix: remote engines should accept normalized headers only (#885)
1 parent 30d44a5 commit 13a5230

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

cortex-js/src/domain/abstracts/oai.abstract.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export abstract class OAIEngineExtension extends EngineExtension {
1818
createChatDto: any,
1919
headers: Record<string, string>,
2020
): Promise<stream.Readable | any> {
21-
const payload = this.transformPayload ? this.transformPayload(createChatDto) : createChatDto;
21+
const payload = this.transformPayload
22+
? this.transformPayload(createChatDto)
23+
: createChatDto;
2224
const { stream: isStream } = payload;
2325
const additionalHeaders = _.omit(headers, [
2426
'content-type',

cortex-js/src/infrastructure/controllers/chat.controller.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import { Response } from 'express';
55
import { ApiOperation, ApiTags, ApiResponse } from '@nestjs/swagger';
66
import { ChatCompletionResponseDto } from '../dtos/chat/chat-completion-response.dto';
77
import { TelemetryUsecases } from '@/usecases/telemetry/telemetry.usecases';
8-
import {
9-
EventName,
10-
TelemetrySource,
11-
} from '@/domain/telemetry/telemetry.interface';
8+
import { EventName } from '@/domain/telemetry/telemetry.interface';
9+
import { extractCommonHeaders } from '@/utils/request';
1210

1311
@ApiTags('Inference')
1412
@Controller('chat')
@@ -20,7 +18,8 @@ export class ChatController {
2018

2119
@ApiOperation({
2220
summary: 'Create chat completion',
23-
description: 'Creates a model response for the given conversation. The following parameters are not working for the `TensorRT-LLM` engine:\n- `frequency_penalty`\n- `presence_penalty`\n- `top_p`',
21+
description:
22+
'Creates a model response for the given conversation. The following parameters are not working for the `TensorRT-LLM` engine:\n- `frequency_penalty`\n- `presence_penalty`\n- `top_p`',
2423
})
2524
@HttpCode(200)
2625
@ApiResponse({
@@ -38,7 +37,7 @@ export class ChatController {
3837

3938
if (stream) {
4039
this.chatService
41-
.inference(createChatDto, headers)
40+
.inference(createChatDto, extractCommonHeaders(headers))
4241
.then((stream) => {
4342
res.header('Content-Type', 'text/event-stream');
4443
stream.pipe(res);
@@ -49,7 +48,7 @@ export class ChatController {
4948
} else {
5049
res.header('Content-Type', 'application/json');
5150
this.chatService
52-
.inference(createChatDto, headers)
51+
.inference(createChatDto, extractCommonHeaders(headers))
5352
.then((response) => {
5453
res.json(response);
5554
})

cortex-js/src/utils/request.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export function extractCommonHeaders(headers: any) {
2+
const commonHeaders = [
3+
'Content-Type',
4+
'User-Agent',
5+
'Accept',
6+
'Authorization',
7+
'Origin',
8+
'Referer',
9+
'Connection',
10+
];
11+
12+
const extractedHeaders: Record<string, string> = {};
13+
14+
for (const header of commonHeaders) {
15+
const value = headers[header];
16+
if (value) {
17+
extractedHeaders[header] = value;
18+
}
19+
}
20+
21+
return extractedHeaders;
22+
}

0 commit comments

Comments
 (0)