Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #3673 - API responds with Request body is too large #3729

Merged
merged 1 commit into from
Sep 24, 2024
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
41 changes: 41 additions & 0 deletions core/src/node/api/restful/helper/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,47 @@ describe('builder helper functions', () => {
})
})

it('should return the error on status not ok', async () => {
const request = { body: { model: 'model1' } }
const mockSend = jest.fn()
const reply = {
code: jest.fn().mockReturnThis(),
send: jest.fn(),
headers: jest.fn().mockReturnValue({
send: mockSend,
}),
raw: {
writeHead: jest.fn(),
pipe: jest.fn(),
},
}

;(existsSync as jest.Mock).mockReturnValue(true)
;(readdirSync as jest.Mock).mockReturnValue(['file1'])
;(readFileSync as jest.Mock).mockReturnValue(
JSON.stringify({ id: 'model1', engine: 'openai' })
)

// Mock fetch
const fetch = require('node-fetch')
fetch.mockResolvedValue({
status: 400,
headers: new Map([
['content-type', 'application/json'],
['x-request-id', '123456'],
]),
body: { pipe: jest.fn() },
text: jest.fn().mockResolvedValue({ error: 'Mock error response' }),
})
await chatCompletions(request, reply)
expect(reply.code).toHaveBeenCalledWith(400)
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
error: 'Mock error response',
})
)
})

it('should return the chat completions', async () => {
const request = { body: { model: 'model1' } }
const reply = {
Expand Down
6 changes: 4 additions & 2 deletions core/src/node/api/restful/helper/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
try {
return JSON.parse(result)
} catch (err) {
console.error(err)

Check warning on line 45 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

45 line is not covered with tests
}
})
.filter((e: any) => !!e)

return modelData
} catch (err) {
console.error(err)
return []

Check warning on line 53 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

52-53 lines are not covered with tests
}
}

Expand All @@ -58,7 +58,7 @@
if (existsSync(path)) {
return readFileSync(path, 'utf-8')
} else {
return undefined

Check warning on line 61 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

61 line is not covered with tests
}
}

Expand Down Expand Up @@ -97,7 +97,7 @@
deleted: true,
}
} catch (ex) {
console.error(ex)

Check warning on line 100 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

100 line is not covered with tests
}
}

Expand All @@ -113,8 +113,8 @@

const messageFilePath = join(threadDirPath, messageFile)
if (!existsSync(messageFilePath)) {
console.debug('message file not found')
return []

Check warning on line 117 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

116-117 lines are not covered with tests
}

const lines = readFileSync(messageFilePath, 'utf-8')
Expand All @@ -128,8 +128,8 @@
})
return messages
} catch (err) {
console.error(err)
return []

Check warning on line 132 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

131-132 lines are not covered with tests
}
}

Expand Down Expand Up @@ -172,7 +172,7 @@
await writeFileSync(threadJsonPath, JSON.stringify(updatedThread, null, 2))
return updatedThread
} catch (err) {
return {

Check warning on line 175 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

175 line is not covered with tests
error: err,
}
}
Expand Down Expand Up @@ -202,7 +202,7 @@
await writeFileSync(threadJsonPath, JSON.stringify(updatedThread, null, 2))
return updatedThread
} catch (err) {
return {

Check warning on line 205 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

205 line is not covered with tests
message: err,
}
}
Expand Down Expand Up @@ -247,7 +247,7 @@
appendFileSync(threadMessagePath, JSON.stringify(threadMessage) + '\n')
return threadMessage
} catch (err) {
return {

Check warning on line 250 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

250 line is not covered with tests
message: err,
}
}
Expand All @@ -268,7 +268,7 @@

const directoryPath = join(getJanDataFolderPath(), 'models', modelId)
if (!existsSync(directoryPath)) {
mkdirSync(directoryPath)

Check warning on line 271 in core/src/node/api/restful/helper/builder.ts

View workflow job for this annotation

GitHub Actions / coverage-check

271 line is not covered with tests
}

// path to model binary
Expand Down Expand Up @@ -353,8 +353,10 @@
body: JSON.stringify(request.body),
})
if (response.status !== 200) {
console.error(response)
reply.code(400).send(response)
// Forward the error response to client via reply
const responseBody = await response.text()
const responseHeaders = Object.fromEntries(response.headers)
reply.code(response.status).headers(responseHeaders).send(responseBody)
} else {
reply.raw.writeHead(200, {
'Content-Type': request.body.stream === true ? 'text/event-stream' : 'application/json',
Expand Down
5 changes: 5 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export const startServer = async (configs?: ServerConfig): Promise<boolean> => {
// Initialize Fastify server with logging
server = fastify({
logger: new Logger(),
// Set body limit to 100MB - Default is 1MB
// According to OpenAI - a batch input file can be up to 100 MB in size
// Whisper endpoints accept up to 25MB
// Vision endpoints accept up to 4MB
bodyLimit: 104_857_600
})

// Register CORS if enabled
Expand Down
Loading