Skip to content

Commit

Permalink
chore: new cortex-cpp binary - model import option and model size
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Nov 4, 2024
1 parent b913af9 commit 46d5faf
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/src/browser/extensions/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export abstract class ModelExtension extends BaseExtension implements ModelInter
abstract getModels(): Promise<Model[]>
abstract pullModel(model: string, id?: string, name?: string): Promise<void>
abstract cancelModelPull(modelId: string): Promise<void>
abstract importModel(model: string, modePath: string, name?: string): Promise<void>
abstract importModel(model: string, modePath: string, name?: string, optionType?: OptionType): Promise<void>
abstract updateModel(modelInfo: Partial<Model>): Promise<Model>
abstract deleteModel(model: string): Promise<void>
}
2 changes: 1 addition & 1 deletion core/src/types/model/modelImport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type OptionType = 'SYMLINK' | 'MOVE_BINARY_FILE'
export type OptionType = 'symlink' | 'copy'

export type ModelImportOption = {
type: OptionType
Expand Down
8 changes: 7 additions & 1 deletion core/src/types/model/modelInterface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Model } from './modelEntity'
import { OptionType } from './modelImport'

/**
* Model extension for managing models.
Expand Down Expand Up @@ -43,5 +44,10 @@ export interface ModelInterface {
* @param model id of the model to import
* @param modelPath - path of the model file
*/
importModel(model: string, modePath: string, name?: string): Promise<void>
importModel(
model: string,
modePath: string,
name?: string,
optionType?: OptionType
): Promise<void>
}
2 changes: 1 addition & 1 deletion extensions/inference-cortex-extension/bin/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.2-rc2
1.0.2-rc4
14 changes: 11 additions & 3 deletions extensions/model-extension/src/cortex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ICortexAPI {
getModel(model: string): Promise<Model>
getModels(): Promise<Model[]>
pullModel(model: string, id?: string, name?: string): Promise<void>
importModel(path: string, modelPath: string, name?: string): Promise<void>
importModel(path: string, modelPath: string, name?: string, option?: string): Promise<void>
deleteModel(model: string): Promise<void>
updateModel(model: object): Promise<void>
cancelModelPull(model: string): Promise<void>
Expand Down Expand Up @@ -85,10 +85,17 @@ export class CortexAPI implements ICortexAPI {
* @param model
* @returns
*/
importModel(model: string, modelPath: string, name?: string): Promise<void> {
importModel(
model: string,
modelPath: string,
name?: string,
option?: string
): Promise<void> {
return this.queue.add(() =>
ky
.post(`${API_URL}/v1/models/import`, { json: { model, modelPath, name } })
.post(`${API_URL}/v1/models/import`, {
json: { model, modelPath, name, option },
})
.json()
.catch((e) => console.debug(e)) // Ignore error
.then()
Expand Down Expand Up @@ -208,6 +215,7 @@ export class CortexAPI implements ICortexAPI {
}
model.metadata = model.metadata ?? {
tags: [],
size: model.size ?? model.metadata?.size ?? 0
}
return model as Model
}
Expand Down
6 changes: 4 additions & 2 deletions extensions/model-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DownloadState,
events,
DownloadEvent,
OptionType
} from '@janhq/core'
import { CortexAPI } from './cortex'
import { scanModelsFolder } from './legacy/model-json'
Expand Down Expand Up @@ -228,9 +229,10 @@ export default class JanModelExtension extends ModelExtension {
async importModel(
model: string,
modelPath: string,
name?: string
name?: string,
option?: OptionType
): Promise<void> {
return this.cortexAPI.importModel(model, modelPath, name)
return this.cortexAPI.importModel(model, modelPath, name, option)
}

/**
Expand Down
8 changes: 6 additions & 2 deletions web/hooks/useImportModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useCallback } from 'react'
import {
ExtensionTypeEnum,
ImportingModel,
LocalImportModelEvent,
Model,
ModelEvent,
ModelExtension,
Expand Down Expand Up @@ -66,10 +67,13 @@ const useImportModel = () => {
addDownloadingModel(modelId)
extensionManager
.get<ModelExtension>(ExtensionTypeEnum.Model)
?.importModel(modelId, model.path, model.name)
?.importModel(modelId, model.path, model.name, optionType)
.finally(() => {
removeDownloadingModel(modelId)
events.emit(ModelEvent.OnModelsUpdate, {})
events.emit(LocalImportModelEvent.onLocalImportModelSuccess, {
importId: model.importId,
modelId: modelId,
})
})
}
})
Expand Down
16 changes: 14 additions & 2 deletions web/hooks/useModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@ const useModels = () => {
.models.values()
.toArray()
.filter((e) => !isLocalEngine(e.engine))
setDownloadedModels([...localModels, ...remoteModels])
const toUpdate = [...localModels, ...remoteModels]
setDownloadedModels(toUpdate)

let isUpdated = false
toUpdate.forEach((model) => {
if (!ModelManager.instance().models.has(model.id)) {
ModelManager.instance().models.set(model.id, model)
isUpdated = true
}
})
if (isUpdated) {
getExtensionModels()
}
}

const getExtensionModels = async () => {
Expand All @@ -52,7 +64,7 @@ const useModels = () => {
}

// Fetch all data
Promise.all([getDownloadedModels(), getExtensionModels()])
getExtensionModels().then(getDownloadedModels)
}, [setDownloadedModels, setExtensionModels])

const reloadData = useDebouncedCallback(() => getData(), 300)
Expand Down
4 changes: 2 additions & 2 deletions web/screens/Settings/ImportModelOptionModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import { importingModelsAtom } from '@/helpers/atoms/Model.atom'

const importOptions: ModelImportOption[] = [
{
type: 'SYMLINK',
type: 'symlink',
title: 'Keep Original Files & Symlink',
description:
'You maintain your model files outside of Jan. Keeping your files where they are, and Jan will create a smart link to them.',
},
{
type: 'MOVE_BINARY_FILE',
type: 'copy',
title: 'Move model binary file',
description:
'Jan will move your model binary file from your current folder into Jan Data Folder.',
Expand Down

0 comments on commit 46d5faf

Please sign in to comment.