NamH ec9b5bf682
fix: update new api from cortex to support 0.5.0 (#3221)
* fix: update new api from cortex to support 0.5.0

Signed-off-by: James <namnh0122@gmail.com>

* fix stop button for streaming

Signed-off-by: James <namnh0122@gmail.com>

* fix stop inference for nonstreaming

Signed-off-by: James <namnh0122@gmail.com>

* chore: remove umami prevent tracking call to vercel

Signed-off-by: James <namnh0122@gmail.com>

* add warning modal when running more than 2 model concurrently

Signed-off-by: James <namnh0122@gmail.com>

* fix: skip summarize if abort

Signed-off-by: James <namnh0122@gmail.com>

* 0.5.0-3

* add inference error popup

Signed-off-by: James <namnh0122@gmail.com>

* add back import local model

Signed-off-by: James <namnh0122@gmail.com>

* fix: max token issue (#3225)

Signed-off-by: James <namnh0122@gmail.com>

* format status

Signed-off-by: James <namnh0122@gmail.com>

* fix migration missing instructions

Signed-off-by: James <namnh0122@gmail.com>

* fix: wait for cortex process overlay should be on top (#3224)

* fix: wait for cortex process overlay should be on top

* chore: update cortex.js

* Cortex 0.5.0-5

* add import model to my model screen

Signed-off-by: James <namnh0122@gmail.com>

* fix: should migrate symlink models (#3226)

* fix import on windows (#3229)

Signed-off-by: James <namnh0122@gmail.com>

* fix yarn lint

Signed-off-by: James <namnh0122@gmail.com>

* fix: clean up port before start jan (#3232)

Signed-off-by: James <namnh0122@gmail.com>

---------

Signed-off-by: James <namnh0122@gmail.com>
Co-authored-by: Van Pham <64197333+Van-QA@users.noreply.github.com>
Co-authored-by: Louis <louis@jan.ai>
2024-08-02 09:37:04 +07:00

171 lines
5.4 KiB
TypeScript

import { Fragment, useCallback, useMemo } from 'react'
import { Model, SettingComponentProps, SliderComponentProps } from '@janhq/core'
import { Accordion, AccordionItem } from '@janhq/joi'
import { useAtomValue, useSetAtom } from 'jotai'
import { useDebouncedCallback } from 'use-debounce'
import EngineSetting from '@/containers/EngineSetting'
import ModelSetting from '@/containers/ModelSetting'
import useModelStop from '@/hooks/useModelStop'
import useModels from '@/hooks/useModels'
import { getConfigurationsData } from '@/utils/componentSettings'
import { toRuntimeParams, toSettingParams } from '@/utils/modelParam'
import { presetConfiguration } from '@/utils/predefinedComponent'
import PromptTemplateSetting from '../PromptTemplateSetting'
import {
getSelectedModelAtom,
updateSelectedModelAtom,
} from '@/helpers/atoms/Model.atom'
const ModelSettingContainer: React.FC = () => {
const stopModel = useModelStop()
const { updateModel } = useModels()
const setSelectedModel = useSetAtom(updateSelectedModelAtom)
const selectedModel = useAtomValue(getSelectedModelAtom)
const modelSettings = useMemo(() => {
if (!selectedModel) return
// runtime setting
const modelRuntimeParams = toRuntimeParams(selectedModel)
const componentDataRuntimeSetting =
getConfigurationsData(modelRuntimeParams)
// engine setting
const modelEngineParams = toSettingParams(selectedModel)
const componentDataEngineSetting = getConfigurationsData(
modelEngineParams
).filter((x) => x.key !== 'prompt_template' && x.key !== 'embedding')
const promptTemplateSettings = getConfigurationsData(
modelEngineParams
).filter((x) => x.key === 'prompt_template')
// the max value of max token has to follow context length
const maxTokens = componentDataRuntimeSetting.find(
(x) => x.key === 'max_tokens'
)
const contextLength = componentDataEngineSetting.find(
(x) => x.key === 'ctx_len'
)
if (maxTokens && contextLength) {
// replace maxToken to componentDataRuntimeSetting
const updatedComponentDataRuntimeSetting: SettingComponentProps[] =
componentDataRuntimeSetting.map((settingComponentProps) => {
if (settingComponentProps.key !== 'max_tokens')
return settingComponentProps
const contextLengthValue = Number(contextLength.controllerProps.value)
const maxTokenValue = Number(
settingComponentProps.controllerProps.value
)
const controllerProps =
settingComponentProps.controllerProps as SliderComponentProps
const sliderProps: SliderComponentProps = {
...controllerProps,
max: contextLengthValue,
value: Math.min(maxTokenValue, contextLengthValue),
}
const updatedSettingProps: SettingComponentProps = {
...settingComponentProps,
controllerProps: sliderProps,
}
return updatedSettingProps
})
return {
runtimeSettings: updatedComponentDataRuntimeSetting,
engineSettings: componentDataEngineSetting,
promptTemplateSettings: promptTemplateSettings,
}
}
return {
runtimeSettings: componentDataRuntimeSetting,
engineSettings: componentDataEngineSetting,
promptTemplateSettings: promptTemplateSettings,
}
}, [selectedModel])
// debounce update model via API 500 ms
const debounceUpdateModel = useDebouncedCallback(
(modelId: string, settings: Record<string, unknown>) => {
updateModel(modelId, settings)
},
500
)
const onValueChanged = useCallback(
async (key: string, value: string | number | boolean) => {
if (!selectedModel) return
const updatedModel: Model = {
...selectedModel,
[key]: value,
}
if (key === 'stop') {
updatedModel['stop'] = (value as string).split(' ')
}
setSelectedModel(updatedModel as Model)
debounceUpdateModel(selectedModel.model, { ...updatedModel })
const shouldStopModel =
presetConfiguration[key]?.requireModelReload ?? true
if (shouldStopModel) {
stopModel.mutate(selectedModel.model)
}
},
[selectedModel, debounceUpdateModel, stopModel, setSelectedModel]
)
if (!modelSettings) return null
return (
<Fragment>
<Accordion defaultValue={[]}>
{modelSettings.runtimeSettings.length !== 0 && (
<AccordionItem
title="Inference Parameters"
value="Inference Parameters"
>
<ModelSetting
componentProps={modelSettings.runtimeSettings}
onValueChanged={onValueChanged}
/>
</AccordionItem>
)}
{modelSettings.promptTemplateSettings.length !== 0 && (
<AccordionItem title="Model Parameters" value="Model Parameters">
<PromptTemplateSetting
componentData={modelSettings.promptTemplateSettings}
onValueChanged={onValueChanged}
/>
</AccordionItem>
)}
{modelSettings.engineSettings.length !== 0 && (
<AccordionItem title="Engine Parameters" value="Engine Parameters">
<EngineSetting
componentData={modelSettings.engineSettings}
onValueChanged={onValueChanged}
/>
</AccordionItem>
)}
</Accordion>
</Fragment>
)
}
export default ModelSettingContainer