Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat(mlx): expose model context limits from local configuration
thinhlpgadb1f19f118e
diff --git a/extensions/mlx-extension/src/index.test.ts b/extensions/mlx-extension/src/index.test.tsindex 4592a3702..d7863badf 100644--- a/extensions/mlx-extension/src/index.test.ts+++ b/extensions/mlx-extension/src/index.test.ts@@ -71,6 +71,27 @@ describe('getModelProps', () => {})})+describe('getModelContextLimit', () => {+ it('uses the text model limit from a multimodal config', async () => {+ const ext = new mlx_extension()+ mockInvoke.mockResolvedValueOnce({ model_path: '/external/model' })+ mockFileStat.mockResolvedValue({ isDirectory: true } as never)+ mockInvoke.mockResolvedValueOnce(JSON.stringify({+ max_position_embeddings: 1024,+ text_config: { max_position_embeddings: 32768 },+ }))+ expect(await ext.getModelContextLimit('model')).toBe(32768)+ })++ it('does not assume a context limit when the config omits it', async () => {+ const ext = new mlx_extension()+ mockInvoke.mockResolvedValueOnce({ model_path: '/external/model' })+ mockFileStat.mockResolvedValue({ isDirectory: true } as never)+ mockInvoke.mockResolvedValueOnce('{}')+ expect(await ext.getModelContextLimit('model')).toBeUndefined()+ })+})+describe('createDownloadTaskId', () => {it('prefixes the provider and strips everything after the first dot', () => {const ext = newExt()diff --git a/extensions/mlx-extension/src/index.ts b/extensions/mlx-extension/src/index.tsindex d2352e93b..682cc570d 100644--- a/extensions/mlx-extension/src/index.ts+++ b/extensions/mlx-extension/src/index.ts@@ -137,6 +137,35 @@ export default class mlx_extension extends AIEngine {} as modelInfo}+ async getModelContextLimit(modelId: string): Promise<number | undefined> {+ const modelConfig = await invoke<ModelConfig>('read_yaml', {+ path: await joinPath([await this.getProviderPath(), 'models', modelId, 'model.yml']),+ })+ const modelPath = isAbsoluteModelPath(modelConfig.model_path)+ ? modelConfig.model_path+ : await joinPath([await getJanDataFolderPath(), modelConfig.model_path])+ let value: unknown+ if (modelPath.endsWith('.gguf')) {+ const { metadata } = await readGgufMetadata(modelPath)+ value = metadata?.[`${metadata?.['general.architecture']}.context_length`]+ } else {+ const directory = (await fs.fileStat(modelPath)).isDirectory+ ? modelPath+ : modelPath.substring(0, modelPath.lastIndexOf('/'))+ const config = JSON.parse(+ await invoke<string>('read_file_sync', {+ args: [await joinPath([directory, 'config.json'])],+ })+ ) as {+ max_position_embeddings?: number+ text_config?: { max_position_embeddings?: number }+ }+ value = config.text_config?.max_position_embeddings ?? config.max_position_embeddings+ }+ const limit = Number(value)+ return Number.isInteger(limit) && limit > 0 ? limit : undefined+ }+override async list(): Promise<modelInfo[]> {const modelsDir = await joinPath([await this.getProviderPath(), 'models'])if (!(await fs.existsSync(modelsDir))) {