Skip to content

Commit

Permalink
Merge pull request janhq#3976 from janhq/chore/should-keep-model-runn…
Browse files Browse the repository at this point in the history
…ing-on-message-error

chore: retrieves the exact model running status upon message error
  • Loading branch information
louis-menlo authored Nov 8, 2024
2 parents ebad6c3 + 43d0e27 commit 1dc2b4d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
8 changes: 7 additions & 1 deletion core/src/browser/extensions/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ 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, optionType?: OptionType): 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>
abstract isModelLoaded(model: string): Promise<boolean>
}
20 changes: 18 additions & 2 deletions extensions/model-extension/src/cortex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ 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, option?: 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 @@ -141,6 +146,17 @@ export class CortexAPI implements ICortexAPI {
)
}

/**
* Check model status
* @param model
*/
async getModelStatus(model: string): Promise<boolean> {
return this.queue
.add(() => ky.get(`${API_URL}/models/status/${model}`))
.then((e) => true)
.catch(() => false)
}

/**
* Do health check on cortex.cpp
* @returns
Expand Down Expand Up @@ -215,7 +231,7 @@ export class CortexAPI implements ICortexAPI {
}
model.metadata = model.metadata ?? {
tags: [],
size: model.size ?? model.metadata?.size ?? 0
size: model.size ?? model.metadata?.size ?? 0,
}
return model as Model
}
Expand Down
8 changes: 8 additions & 0 deletions extensions/model-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ export default class JanModelExtension extends ModelExtension {
return this.cortexAPI.importModel(model, modelPath, name, option)
}

/**
* Check model status
* @param model
*/
async isModelLoaded(model: string): Promise<boolean> {
return this.cortexAPI.getModelStatus(model)
}

/**
* Handle download state from main app
*/
Expand Down
13 changes: 11 additions & 2 deletions web/containers/Providers/EventHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
EngineManager,
InferenceEngine,
extractInferenceParams,
ModelExtension,
} from '@janhq/core'
import { useAtomValue, useSetAtom } from 'jotai'
import { ulid } from 'ulidx'
Expand Down Expand Up @@ -180,8 +181,16 @@ export default function EventHandler({ children }: { children: ReactNode }) {
}
return
} else if (message.status === MessageStatus.Error) {
setActiveModel(undefined)
setStateModel({ state: 'start', loading: false, model: undefined })
;(async () => {
if (
!(await extensionManager
.get<ModelExtension>(ExtensionTypeEnum.Model)
?.isModelLoaded(activeModelRef.current?.id as string))
) {
setActiveModel(undefined)
setStateModel({ state: 'start', loading: false, model: undefined })
}
})()
}
// Mark the thread as not waiting for response
updateThreadWaiting(message.thread_id, false)
Expand Down
4 changes: 4 additions & 0 deletions web/hooks/useModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const useModels = () => {
const localModels = (await getModels()).map((e) => ({
...e,
name: ModelManager.instance().models.get(e.id)?.name ?? e.id,
settings:
ModelManager.instance().models.get(e.id)?.settings ?? e.settings,
parameters:
ModelManager.instance().models.get(e.id)?.parameters ?? e.parameters,
metadata:
ModelManager.instance().models.get(e.id)?.metadata ?? e.metadata,
}))
Expand Down

0 comments on commit 1dc2b4d

Please sign in to comment.