Skip to content

Commit

Permalink
Merge pull request #3766 from janhq/hotfix/graceful-error-handling-mo…
Browse files Browse the repository at this point in the history
…del-import

hotfix: graceful error handling model import
  • Loading branch information
louis-jan authored Oct 4, 2024
2 parents 27c6ac6 + 2c2f179 commit 3f6843a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
58 changes: 58 additions & 0 deletions extensions/model-extension/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,5 +784,63 @@ describe('JanModelExtension', () => {
expect.anything()
)
})

it('should handle model with valid chat_template', async () => {
executeMock.mockResolvedValue('{prompt}')
;(gguf as jest.Mock).mockResolvedValue({
metadata: {},
})
// @ts-ignore
global.NODE = 'node'
// @ts-ignore
global.DEFAULT_MODEL = {
parameters: { stop: [] },
settings: {
prompt_template: '<|im-start|>{prompt}<|im-end|>',
},
}

const result = await sut.retrieveGGUFMetadata({})

expect(result).toEqual({
parameters: {
stop: [],
},
settings: {
ctx_len: 4096,
ngl: 33,
prompt_template: '{prompt}',
},
})
})

it('should handle model without chat_template', async () => {
executeMock.mockRejectedValue({})
;(gguf as jest.Mock).mockResolvedValue({
metadata: {},
})
// @ts-ignore
global.NODE = 'node'
// @ts-ignore
global.DEFAULT_MODEL = {
parameters: { stop: [] },
settings: {
prompt_template: '<|im-start|>{prompt}<|im-end|>',
},
}

const result = await sut.retrieveGGUFMetadata({})

expect(result).toEqual({
parameters: {
stop: [],
},
settings: {
ctx_len: 4096,
ngl: 33,
prompt_template: '<|im-start|>{prompt}<|im-end|>',
},
})
})
})
})
9 changes: 7 additions & 2 deletions extensions/model-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ export default class JanModelExtension extends ModelExtension {
dirName,
binaryFileName,
])
)
).catch(() => undefined)

const updatedModel = await this.retrieveGGUFMetadata(metadata)

Expand Down Expand Up @@ -877,8 +877,13 @@ export default class JanModelExtension extends ModelExtension {
* @returns
*/
async retrieveGGUFMetadata(metadata: any): Promise<Partial<Model>> {
const template = await executeOnMain(NODE, 'renderJinjaTemplate', metadata)
const defaultModel = DEFAULT_MODEL as Model
var template = await executeOnMain(
NODE,
'renderJinjaTemplate',
metadata
).catch(() => undefined)

const eos_id = metadata['tokenizer.ggml.eos_token_id']
const architecture = metadata['general.architecture']

Expand Down
7 changes: 1 addition & 6 deletions extensions/model-extension/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ export const retrieveGGUFMetadata = async (ggufPath: string) => {
// Parse metadata and tensor info
const { metadata } = ggufMetadata(buffer.buffer)

// Parse jinja template
const renderedTemplate = renderJinjaTemplate(metadata)
return {
...metadata,
parsed_chat_template: renderedTemplate,
}
return metadata
} catch (e) {
console.log('[MODEL_EXT]', e)
}
Expand Down

0 comments on commit 3f6843a

Please sign in to comment.