Merge pull request #4095 from janhq/main

Release 0.5.9 sync back - main branch to dev
This commit is contained in:
Louis 2024-11-22 12:52:13 +07:00 committed by GitHub
commit 4f70a5dff2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 152 additions and 49 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "@janhq/inference-cortex-extension", "name": "@janhq/inference-cortex-extension",
"productName": "Cortex Inference Engine", "productName": "Cortex Inference Engine",
"version": "1.0.21", "version": "1.0.22",
"description": "This extension embeds cortex.cpp, a lightweight inference engine written in C++. See https://jan.ai.\nAdditional dependencies could be installed to run without Cuda Toolkit installation.", "description": "This extension embeds cortex.cpp, a lightweight inference engine written in C++. See https://jan.ai.\nAdditional dependencies could be installed to run without Cuda Toolkit installation.",
"main": "dist/index.js", "main": "dist/index.js",
"node": "dist/node/index.cjs.js", "node": "dist/node/index.cjs.js",

View File

@ -1,8 +1,8 @@
{ {
"sources": [ "sources": [
{ {
"url": "https://huggingface.co/cortexso/phi3/resolve/main/model.gguf", "url": "https://huggingface.co/bartowski/Phi-3-mini-4k-instruct-GGUF/resolve/main/Phi-3-mini-4k-instruct-Q4_K_M.gguf",
"filename": "model.gguf" "filename": "Phi-3-mini-4k-instruct-Q4_K_M.gguf"
} }
], ],
"id": "phi3-3.8b", "id": "phi3-3.8b",
@ -14,7 +14,7 @@
"settings": { "settings": {
"ctx_len": 4096, "ctx_len": 4096,
"prompt_template": "<|user|>\n{prompt}<|end|>\n<|assistant|>\n", "prompt_template": "<|user|>\n{prompt}<|end|>\n<|assistant|>\n",
"llama_model_path": "model.gguf", "llama_model_path": "Phi-3-mini-4k-instruct-Q4_K_M.gguf",
"ngl": 33 "ngl": 33
}, },
"parameters": { "parameters": {

View File

@ -1,8 +1,8 @@
{ {
"sources": [ "sources": [
{ {
"url": "https://huggingface.co/bartowski/Phi-3-medium-128k-instruct-GGUF/resolve/main/Phi-3-medium-128k-instruct-Q4_K_M.gguf", "url": "https://huggingface.co/bartowski/Phi-3-mini-4k-instruct-GGUF/resolve/main/Phi-3-mini-4k-instruct-Q4_K_M.gguf",
"filename": "Phi-3-medium-128k-instruct-Q4_K_M.gguf" "filename": "Phi-3-mini-4k-instruct-Q4_K_M.gguf"
} }
], ],
"id": "phi3-medium", "id": "phi3-medium",
@ -14,7 +14,7 @@
"settings": { "settings": {
"ctx_len": 128000, "ctx_len": 128000,
"prompt_template": "<|user|> {prompt}<|end|><|assistant|>", "prompt_template": "<|user|> {prompt}<|end|><|assistant|>",
"llama_model_path": "Phi-3-medium-128k-instruct-Q4_K_M.gguf", "llama_model_path": "Phi-3-mini-4k-instruct-Q4_K_M.gguf",
"ngl": 33 "ngl": 33
}, },
"parameters": { "parameters": {

View File

@ -83,11 +83,11 @@ export default class JanInferenceCortexExtension extends LocalOAIEngine {
}) })
} }
onUnload(): void { async onUnload() {
console.log('Clean up cortex.cpp services') console.log('Clean up cortex.cpp services')
this.shouldReconnect = false this.shouldReconnect = false
this.clean() this.clean()
executeOnMain(NODE, 'dispose') await executeOnMain(NODE, 'dispose')
super.onUnload() super.onUnload()
} }

View File

@ -0,0 +1,27 @@
import { cpuInfo } from 'cpu-instructions'
// Check the CPU info and determine the supported instruction set
const info = cpuInfo.cpuInfo().some((e) => e.toUpperCase() === 'AVX512')
? 'avx512'
: cpuInfo.cpuInfo().some((e) => e.toUpperCase() === 'AVX2')
? 'avx2'
: cpuInfo.cpuInfo().some((e) => e.toUpperCase() === 'AVX')
? 'avx'
: 'noavx'
// Send the result and wait for confirmation before exiting
new Promise<void>((resolve, reject) => {
// @ts-ignore
process.send(info, (error: Error | null) => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
.then(() => process.exit(0))
.catch((error) => {
console.error('Failed to send info:', error)
process.exit(1)
})

View File

@ -1,6 +1,6 @@
import * as path from 'path' import * as path from 'path'
import { cpuInfo } from 'cpu-instructions'
import { GpuSetting, appResourcePath, log } from '@janhq/core/node' import { GpuSetting, appResourcePath, log } from '@janhq/core/node'
import { fork } from 'child_process'
export interface CortexExecutableOptions { export interface CortexExecutableOptions {
enginePath: string enginePath: string
@ -52,7 +52,9 @@ const extension = (): '.exe' | '' => {
*/ */
const cudaVersion = (settings?: GpuSetting): '11-7' | '12-0' | undefined => { const cudaVersion = (settings?: GpuSetting): '11-7' | '12-0' | undefined => {
const isUsingCuda = const isUsingCuda =
settings?.vulkan !== true && settings?.run_mode === 'gpu' && !os().includes('mac') settings?.vulkan !== true &&
settings?.run_mode === 'gpu' &&
!os().includes('mac')
if (!isUsingCuda) return undefined if (!isUsingCuda) return undefined
return settings?.cuda?.version === '11' ? '11-7' : '12-0' return settings?.cuda?.version === '11' ? '11-7' : '12-0'
@ -62,15 +64,29 @@ const cudaVersion = (settings?: GpuSetting): '11-7' | '12-0' | undefined => {
* The CPU instructions that will be set - either 'avx512', 'avx2', 'avx', or 'noavx'. * The CPU instructions that will be set - either 'avx512', 'avx2', 'avx', or 'noavx'.
* @returns * @returns
*/ */
const cpuInstructions = (): string => { const cpuInstructions = async (): Promise<string> => {
if (process.platform === 'darwin') return '' if (process.platform === 'darwin') return ''
return cpuInfo.cpuInfo().some((e) => e.toUpperCase() === 'AVX512')
? 'avx512' const child = fork(path.join(__dirname, './cpuInfo.js')) // Path to the child process file
: cpuInfo.cpuInfo().some((e) => e.toUpperCase() === 'AVX2')
? 'avx2' return new Promise((resolve, reject) => {
: cpuInfo.cpuInfo().some((e) => e.toUpperCase() === 'AVX') child.on('message', (cpuInfo?: string) => {
? 'avx' resolve(cpuInfo ?? 'noavx')
: 'noavx' child.kill() // Kill the child process after receiving the result
})
child.on('error', (err) => {
resolve('noavx')
child.kill()
})
child.on('exit', (code) => {
if (code !== 0) {
resolve('noavx')
child.kill()
}
})
})
} }
/** /**
@ -94,8 +110,11 @@ export const executableCortexFile = (
/** /**
* Find which variant to run based on the current platform. * Find which variant to run based on the current platform.
*/ */
export const engineVariant = (gpuSetting?: GpuSetting): string => { export const engineVariant = async (
const cpuInstruction = cpuInstructions() gpuSetting?: GpuSetting
): Promise<string> => {
const cpuInstruction = await cpuInstructions()
log(`[CORTEX]: CPU instruction: ${cpuInstruction}`)
let engineVariant = [ let engineVariant = [
os(), os(),
gpuSetting?.vulkan gpuSetting?.vulkan

View File

@ -1,5 +1,6 @@
import React from 'react' import React from 'react'
import * as SliderPrimitive from '@radix-ui/react-slider' import * as SliderPrimitive from '@radix-ui/react-slider'
import { twMerge } from 'tailwind-merge'
import './styles.scss' import './styles.scss'
@ -25,7 +26,7 @@ const Slider = ({
disabled, disabled,
}: Props) => ( }: Props) => (
<SliderPrimitive.Root <SliderPrimitive.Root
className="slider" className={twMerge('slider', disabled && 'slider--disabled')}
name={name} name={name}
min={min} min={min}
max={max} max={max}

View File

@ -6,6 +6,11 @@
touch-action: none; touch-action: none;
height: 16px; height: 16px;
&--disabled {
cursor: not-allowed;
opacity: 0.2;
}
&__track { &__track {
background-color: hsla(var(--slider-track-bg)); background-color: hsla(var(--slider-track-bg));
position: relative; position: relative;

View File

@ -1,3 +1,5 @@
import { atom } from 'jotai' import { atom } from 'jotai'
export const serverEnabledAtom = atom<boolean>(false) export const serverEnabledAtom = atom<boolean>(false)
export const LocalAPIserverModelParamsAtom = atom()

View File

@ -17,6 +17,14 @@ jest.mock('@janhq/core', () => ({
fs: { fs: {
rm: jest.fn(), rm: jest.fn(),
}, },
EngineManager: {
instance: jest.fn().mockReturnValue({
get: jest.fn(),
engines: {
values: jest.fn().mockReturnValue([])
}
}),
},
})) }))
describe('useFactoryReset', () => { describe('useFactoryReset', () => {

View File

@ -1,6 +1,6 @@
import { useCallback } from 'react' import { useCallback } from 'react'
import { fs, AppConfiguration } from '@janhq/core' import { fs, AppConfiguration, EngineManager } from '@janhq/core'
import { atom, useAtomValue, useSetAtom } from 'jotai' import { atom, useAtomValue, useSetAtom } from 'jotai'
import { useActiveModel } from './useActiveModel' import { useActiveModel } from './useActiveModel'
@ -37,6 +37,15 @@ export default function useFactoryReset() {
// 1: Stop running model // 1: Stop running model
setFactoryResetState(FactoryResetState.StoppingModel) setFactoryResetState(FactoryResetState.StoppingModel)
await stopModel() await stopModel()
await Promise.all(
EngineManager.instance()
.engines.values()
.map(async (engine) => {
await engine.onUnload()
})
)
await new Promise((resolve) => setTimeout(resolve, 4000)) await new Promise((resolve) => setTimeout(resolve, 4000))
// 2: Delete the old jan data folder // 2: Delete the old jan data folder

View File

@ -90,12 +90,15 @@ const useModels = () => {
const toUpdate = [ const toUpdate = [
...downloadedModels, ...downloadedModels,
...cachedModels.filter( ...cachedModels.filter(
(e: Model) => !downloadedModels.some((g: Model) => g.id === e.id) (e) =>
!isLocalEngine(e.engine) &&
!downloadedModels.some((g: Model) => g.id === e.id)
), ),
] ]
setDownloadedModels(toUpdate) setDownloadedModels(toUpdate)
}, [downloadedModels, setDownloadedModels]) setExtensionModels(cachedModels)
}, [downloadedModels, setDownloadedModels, setExtensionModels])
const getModels = async (): Promise<Model[]> => const getModels = async (): Promise<Model[]> =>
extensionManager extensionManager

View File

@ -1,5 +1,6 @@
import { Fragment, useCallback, useState } from 'react' import { Fragment, useCallback, useState } from 'react'
import { EngineManager, Model, ModelSettingParams } from '@janhq/core'
import { Button, Tooltip, Select, Input, Checkbox } from '@janhq/joi' import { Button, Tooltip, Select, Input, Checkbox } from '@janhq/joi'
import { useAtom, useAtomValue, useSetAtom } from 'jotai' import { useAtom, useAtomValue, useSetAtom } from 'jotai'
@ -22,7 +23,10 @@ import {
hostOptions, hostOptions,
} from '@/helpers/atoms/ApiServer.atom' } from '@/helpers/atoms/ApiServer.atom'
import { serverEnabledAtom } from '@/helpers/atoms/LocalServer.atom' import {
LocalAPIserverModelParamsAtom,
serverEnabledAtom,
} from '@/helpers/atoms/LocalServer.atom'
import { selectedModelAtom } from '@/helpers/atoms/Model.atom' import { selectedModelAtom } from '@/helpers/atoms/Model.atom'
const LocalServerLeftPanel = () => { const LocalServerLeftPanel = () => {
@ -31,7 +35,7 @@ const LocalServerLeftPanel = () => {
const [serverEnabled, setServerEnabled] = useAtom(serverEnabledAtom) const [serverEnabled, setServerEnabled] = useAtom(serverEnabledAtom)
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
const { startModel, stateModel } = useActiveModel() const { stateModel } = useActiveModel()
const selectedModel = useAtomValue(selectedModelAtom) const selectedModel = useAtomValue(selectedModelAtom)
const [isCorsEnabled, setIsCorsEnabled] = useAtom(apiServerCorsEnabledAtom) const [isCorsEnabled, setIsCorsEnabled] = useAtom(apiServerCorsEnabledAtom)
@ -42,9 +46,19 @@ const LocalServerLeftPanel = () => {
const [port, setPort] = useAtom(apiServerPortAtom) const [port, setPort] = useAtom(apiServerPortAtom)
const [prefix, setPrefix] = useAtom(apiServerPrefix) const [prefix, setPrefix] = useAtom(apiServerPrefix)
const setLoadModelError = useSetAtom(loadModelErrorAtom) const setLoadModelError = useSetAtom(loadModelErrorAtom)
const localAPIserverModelParams = useAtomValue(LocalAPIserverModelParamsAtom)
const FIRST_TIME_VISIT_API_SERVER = 'firstTimeVisitAPIServer' const FIRST_TIME_VISIT_API_SERVER = 'firstTimeVisitAPIServer'
const model: Model | undefined = selectedModel
? {
...selectedModel,
object: selectedModel.object || '',
settings: (typeof localAPIserverModelParams === 'object'
? { ...(localAPIserverModelParams as ModelSettingParams) }
: { ...selectedModel.settings }) as ModelSettingParams,
}
: undefined
const [firstTimeVisitAPIServer, setFirstTimeVisitAPIServer] = const [firstTimeVisitAPIServer, setFirstTimeVisitAPIServer] =
useState<boolean>(false) useState<boolean>(false)
@ -80,7 +94,9 @@ const LocalServerLeftPanel = () => {
localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false') localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false')
setFirstTimeVisitAPIServer(false) setFirstTimeVisitAPIServer(false)
} }
startModel(selectedModel.id, false).catch((e) => console.error(e)) const engine = EngineManager.instance().get((model as Model).engine)
engine?.loadModel(model as Model)
// startModel(selectedModel.id, false).catch((e) => console.error(e))
setIsLoading(false) setIsLoading(false)
} catch (e) { } catch (e) {
console.error(e) console.error(e)

View File

@ -17,13 +17,15 @@ import { useClipboard } from '@/hooks/useClipboard'
import { getConfigurationsData } from '@/utils/componentSettings' import { getConfigurationsData } from '@/utils/componentSettings'
import { serverEnabledAtom } from '@/helpers/atoms/LocalServer.atom' import {
LocalAPIserverModelParamsAtom,
serverEnabledAtom,
} from '@/helpers/atoms/LocalServer.atom'
import { selectedModelAtom } from '@/helpers/atoms/Model.atom' import { selectedModelAtom } from '@/helpers/atoms/Model.atom'
import { getActiveThreadModelParamsAtom } from '@/helpers/atoms/Thread.atom'
const LocalServerRightPanel = () => { const LocalServerRightPanel = () => {
const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom)
const loadModelError = useAtomValue(loadModelErrorAtom) const loadModelError = useAtomValue(loadModelErrorAtom)
const setLocalAPIserverModelParams = useSetAtom(LocalAPIserverModelParamsAtom)
const serverEnabled = useAtomValue(serverEnabledAtom) const serverEnabled = useAtomValue(serverEnabledAtom)
const setModalTroubleShooting = useSetAtom(modalTroubleShootingAtom) const setModalTroubleShooting = useSetAtom(modalTroubleShootingAtom)
@ -35,12 +37,19 @@ const LocalServerRightPanel = () => {
extractModelLoadParams(selectedModel?.settings) extractModelLoadParams(selectedModel?.settings)
) )
const overriddenSettings =
selectedModel?.settings.ctx_len && selectedModel.settings.ctx_len > 2048
? { ctx_len: 4096 }
: {}
useEffect(() => { useEffect(() => {
if (selectedModel) { if (selectedModel) {
setCurrentModelSettingParams( setCurrentModelSettingParams({
extractModelLoadParams(selectedModel?.settings) ...selectedModel?.settings,
) ...overriddenSettings,
})
} }
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedModel]) }, [selectedModel])
const modelRuntimeParams = extractInferenceParams(selectedModel?.settings) const modelRuntimeParams = extractInferenceParams(selectedModel?.settings)
@ -50,17 +59,8 @@ const LocalServerRightPanel = () => {
selectedModel selectedModel
) )
const modelEngineParams = extractModelLoadParams(
{
...selectedModel?.settings,
...activeModelParams,
},
selectedModel?.settings
)
const componentDataEngineSetting = getConfigurationsData( const componentDataEngineSetting = getConfigurationsData(
modelEngineParams, currentModelSettingParams
selectedModel
) )
const engineSettings = useMemo( const engineSettings = useMemo(
@ -78,16 +78,27 @@ const LocalServerRightPanel = () => {
) )
}, [componentDataRuntimeSetting]) }, [componentDataRuntimeSetting])
const onUpdateParams = useCallback(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setLocalAPIserverModelParams(() => {
return { ...currentModelSettingParams }
})
}, [currentModelSettingParams, setLocalAPIserverModelParams])
const onValueChanged = useCallback( const onValueChanged = useCallback(
(key: string, value: string | number | boolean) => { (key: string, value: string | number | boolean) => {
setCurrentModelSettingParams({ setCurrentModelSettingParams((prevParams) => ({
...currentModelSettingParams, ...prevParams,
[key]: value, [key]: value,
}) }))
}, },
[currentModelSettingParams] []
) )
useEffect(() => {
onUpdateParams()
}, [currentModelSettingParams, onUpdateParams])
return ( return (
<RightPanelContainer> <RightPanelContainer>
<div className="mb-4 px-4 pt-4"> <div className="mb-4 px-4 pt-4">
@ -156,6 +167,7 @@ const LocalServerRightPanel = () => {
<ModelSetting <ModelSetting
componentProps={modelSettings} componentProps={modelSettings}
onValueChanged={onValueChanged} onValueChanged={onValueChanged}
disabled={serverEnabled}
/> />
</AccordionItem> </AccordionItem>
)} )}
@ -165,6 +177,7 @@ const LocalServerRightPanel = () => {
<EngineSetting <EngineSetting
componentData={engineSettings} componentData={engineSettings}
onValueChanged={onValueChanged} onValueChanged={onValueChanged}
disabled={serverEnabled}
/> />
</AccordionItem> </AccordionItem>
)} )}