fix: not able to update model setting in local api (#2687)

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
This commit is contained in:
NamH 2024-04-11 15:49:39 +07:00 committed by GitHub
parent 02c49e796d
commit 3deaa8f7ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 102 additions and 104 deletions

View File

@ -14,7 +14,7 @@ import { InfoIcon } from 'lucide-react'
type Props = { type Props = {
name: string name: string
title: string title: string
enabled?: boolean disabled?: boolean
description: string description: string
checked: boolean checked: boolean
onValueChanged?: (e: string | number | boolean) => void onValueChanged?: (e: string | number | boolean) => void
@ -23,7 +23,7 @@ type Props = {
const Checkbox: React.FC<Props> = ({ const Checkbox: React.FC<Props> = ({
title, title,
checked, checked,
enabled = true, disabled = false,
description, description,
onValueChanged, onValueChanged,
}) => { }) => {
@ -52,7 +52,7 @@ const Checkbox: React.FC<Props> = ({
<Switch <Switch
checked={checked} checked={checked}
onCheckedChange={onCheckedChange} onCheckedChange={onCheckedChange}
disabled={!enabled} disabled={disabled}
/> />
</div> </div>
) )

View File

@ -11,7 +11,7 @@ import { InfoIcon } from 'lucide-react'
type Props = { type Props = {
title: string title: string
enabled?: boolean disabled?: boolean
name: string name: string
description: string description: string
placeholder: string placeholder: string
@ -21,7 +21,7 @@ type Props = {
const ModelConfigInput: React.FC<Props> = ({ const ModelConfigInput: React.FC<Props> = ({
title, title,
enabled = true, disabled = false,
value, value,
description, description,
placeholder, placeholder,
@ -48,7 +48,7 @@ const ModelConfigInput: React.FC<Props> = ({
placeholder={placeholder} placeholder={placeholder}
onChange={(e) => onValueChanged?.(e.target.value)} onChange={(e) => onValueChanged?.(e.target.value)}
value={value} value={value}
disabled={!enabled} disabled={disabled}
/> />
</div> </div>
) )

View File

@ -17,7 +17,7 @@ import { useClickOutside } from '@/hooks/useClickOutside'
type Props = { type Props = {
name: string name: string
title: string title: string
enabled: boolean disabled: boolean
description: string description: string
min: number min: number
max: number max: number
@ -28,7 +28,7 @@ type Props = {
const SliderRightPanel: React.FC<Props> = ({ const SliderRightPanel: React.FC<Props> = ({
title, title,
enabled, disabled,
min, min,
max, max,
step, step,
@ -65,7 +65,7 @@ const SliderRightPanel: React.FC<Props> = ({
min={min} min={min}
max={max} max={max}
step={step} step={step}
disabled={!enabled} disabled={disabled}
/> />
<div className="relative mt-2 flex items-center justify-between text-gray-400"> <div className="relative mt-2 flex items-center justify-between text-gray-400">
<p className="text-sm">{min}</p> <p className="text-sm">{min}</p>
@ -80,7 +80,7 @@ const SliderRightPanel: React.FC<Props> = ({
min={min} min={min}
max={max} max={max}
value={String(value)} value={String(value)}
disabled={!enabled} disabled={disabled}
onBlur={(e) => { onBlur={(e) => {
if (Number(e.target.value) > Number(max)) { if (Number(e.target.value) > Number(max)) {
onValueChanged?.(Number(max)) onValueChanged?.(Number(max))

View File

@ -1,54 +1,23 @@
import { useCallback } from 'react'
import { SettingComponentProps } from '@janhq/core/.' import { SettingComponentProps } from '@janhq/core/.'
import { useAtomValue, useSetAtom } from 'jotai'
import { useActiveModel } from '@/hooks/useActiveModel'
import useUpdateModelParameters from '@/hooks/useUpdateModelParameters'
import SettingComponentBuilder from '../../Chat/ModelSetting/SettingComponent' import SettingComponentBuilder from '../../Chat/ModelSetting/SettingComponent'
import { serverEnabledAtom } from '@/helpers/atoms/LocalServer.atom'
import {
activeThreadAtom,
engineParamsUpdateAtom,
} from '@/helpers/atoms/Thread.atom'
type Props = { type Props = {
componentData: SettingComponentProps[] componentData: SettingComponentProps[]
onValueChanged: (key: string, value: string | number | boolean) => void
disabled?: boolean
} }
const EngineSetting: React.FC<Props> = ({ componentData }) => { const EngineSetting: React.FC<Props> = ({
const isLocalServerRunning = useAtomValue(serverEnabledAtom) componentData,
const activeThread = useAtomValue(activeThreadAtom) onValueChanged,
disabled = false,
const { stopModel } = useActiveModel() }) => (
const { updateModelParameter } = useUpdateModelParameters()
const setEngineParamsUpdate = useSetAtom(engineParamsUpdateAtom)
const onValueChanged = useCallback(
(key: string, value: string | number | boolean) => {
if (!activeThread) return
setEngineParamsUpdate(true)
stopModel()
updateModelParameter(activeThread, {
params: { [key]: value },
})
},
[activeThread, setEngineParamsUpdate, stopModel, updateModelParameter]
)
return (
<SettingComponentBuilder <SettingComponentBuilder
componentProps={componentData} componentProps={componentData}
enabled={!isLocalServerRunning} disabled={disabled}
onValueUpdated={onValueChanged} onValueUpdated={onValueChanged}
/> />
) )
}
export default EngineSetting export default EngineSetting

View File

@ -11,13 +11,13 @@ import SliderRightPanel from '@/containers/SliderRightPanel'
type Props = { type Props = {
componentProps: SettingComponentProps[] componentProps: SettingComponentProps[]
enabled?: boolean disabled?: boolean
onValueUpdated: (key: string, value: string | number | boolean) => void onValueUpdated: (key: string, value: string | number | boolean) => void
} }
const SettingComponent: React.FC<Props> = ({ const SettingComponent: React.FC<Props> = ({
componentProps, componentProps,
enabled = true, disabled = false,
onValueUpdated, onValueUpdated,
}) => { }) => {
const components = componentProps.map((data) => { const components = componentProps.map((data) => {
@ -35,7 +35,7 @@ const SettingComponent: React.FC<Props> = ({
step={step} step={step}
value={value} value={value}
name={data.key} name={data.key}
enabled={enabled} disabled={disabled}
onValueChanged={(value) => onValueUpdated(data.key, value)} onValueChanged={(value) => onValueUpdated(data.key, value)}
/> />
) )
@ -47,7 +47,7 @@ const SettingComponent: React.FC<Props> = ({
return ( return (
<ModelConfigInput <ModelConfigInput
title={data.title} title={data.title}
enabled={enabled} disabled={disabled}
key={data.key} key={data.key}
name={data.key} name={data.key}
description={data.description} description={data.description}
@ -63,7 +63,7 @@ const SettingComponent: React.FC<Props> = ({
return ( return (
<Checkbox <Checkbox
key={data.key} key={data.key}
enabled={enabled} disabled={disabled}
name={data.key} name={data.key}
description={data.description} description={data.description}
title={data.title} title={data.title}

View File

@ -1,49 +1,25 @@
import React, { useCallback } from 'react' import React from 'react'
import { SettingComponentProps } from '@janhq/core/.' import { SettingComponentProps } from '@janhq/core/.'
import { useAtomValue } from 'jotai'
import useUpdateModelParameters from '@/hooks/useUpdateModelParameters'
import SettingComponentBuilder from './SettingComponent' import SettingComponentBuilder from './SettingComponent'
import { serverEnabledAtom } from '@/helpers/atoms/LocalServer.atom'
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'
type Props = { type Props = {
componentProps: SettingComponentProps[] componentProps: SettingComponentProps[]
onValueChanged: (key: string, value: string | number | boolean) => void
disabled?: boolean
} }
const ModelSetting: React.FC<Props> = ({ componentProps }) => { const ModelSetting: React.FC<Props> = ({
const isLocalServerRunning = useAtomValue(serverEnabledAtom) componentProps,
const activeThread = useAtomValue(activeThreadAtom) onValueChanged,
const { updateModelParameter } = useUpdateModelParameters() disabled = false,
}) => (
const onValueChanged = useCallback(
(key: string, value: string | number | boolean) => {
if (!activeThread) return
if (key === 'stop' && typeof value === 'string') {
updateModelParameter(activeThread, {
params: { [key]: [value] },
})
} else {
updateModelParameter(activeThread, {
params: { [key]: value },
})
}
},
[activeThread, updateModelParameter]
)
return (
<SettingComponentBuilder <SettingComponentBuilder
enabled={!isLocalServerRunning} disabled={!disabled}
componentProps={componentProps} componentProps={componentProps}
onValueUpdated={onValueChanged} onValueUpdated={onValueChanged}
/> />
) )
}
export default React.memo(ModelSetting) export default React.memo(ModelSetting)

View File

@ -2,7 +2,7 @@ import React, { useCallback, useMemo } from 'react'
import { Input, Textarea } from '@janhq/uikit' import { Input, Textarea } from '@janhq/uikit'
import { atom, useAtomValue } from 'jotai' import { atom, useAtomValue, useSetAtom } from 'jotai'
import { twMerge } from 'tailwind-merge' import { twMerge } from 'tailwind-merge'
@ -13,8 +13,11 @@ import DropdownListSidebar, {
selectedModelAtom, selectedModelAtom,
} from '@/containers/DropdownListSidebar' } from '@/containers/DropdownListSidebar'
import { useActiveModel } from '@/hooks/useActiveModel'
import { useCreateNewThread } from '@/hooks/useCreateNewThread' import { useCreateNewThread } from '@/hooks/useCreateNewThread'
import useUpdateModelParameters from '@/hooks/useUpdateModelParameters'
import { getConfigurationsData } from '@/utils/componentSettings' import { getConfigurationsData } from '@/utils/componentSettings'
import { toRuntimeParams, toSettingParams } from '@/utils/modelParam' import { toRuntimeParams, toSettingParams } from '@/utils/modelParam'
@ -27,6 +30,7 @@ import PromptTemplateSetting from './PromptTemplateSetting'
import { import {
activeThreadAtom, activeThreadAtom,
engineParamsUpdateAtom,
getActiveThreadModelParamsAtom, getActiveThreadModelParamsAtom,
} from '@/helpers/atoms/Thread.atom' } from '@/helpers/atoms/Thread.atom'
@ -39,6 +43,10 @@ const Sidebar: React.FC = () => {
const selectedModel = useAtomValue(selectedModelAtom) const selectedModel = useAtomValue(selectedModelAtom)
const { updateThreadMetadata } = useCreateNewThread() const { updateThreadMetadata } = useCreateNewThread()
const setEngineParamsUpdate = useSetAtom(engineParamsUpdateAtom)
const { stopModel } = useActiveModel()
const { updateModelParameter } = useUpdateModelParameters()
const modelSettings = useMemo(() => { const modelSettings = useMemo(() => {
const modelRuntimeParams = toRuntimeParams(activeModelParams) const modelRuntimeParams = toRuntimeParams(activeModelParams)
@ -96,6 +104,22 @@ const Sidebar: React.FC = () => {
[activeThread, updateThreadMetadata] [activeThread, updateThreadMetadata]
) )
const onValueChanged = useCallback(
(key: string, value: string | number | boolean) => {
if (!activeThread) {
return
}
setEngineParamsUpdate(true)
stopModel()
updateModelParameter(activeThread, {
params: { [key]: value },
})
},
[activeThread, setEngineParamsUpdate, stopModel, updateModelParameter]
)
return ( return (
<div <div
className={twMerge( className={twMerge(
@ -170,7 +194,10 @@ const Sidebar: React.FC = () => {
{modelSettings.length > 0 && ( {modelSettings.length > 0 && (
<CardSidebar title="Inference Parameters" asChild> <CardSidebar title="Inference Parameters" asChild>
<div className="px-2 py-4"> <div className="px-2 py-4">
<ModelSetting componentProps={modelSettings} /> <ModelSetting
componentProps={modelSettings}
onValueChanged={onValueChanged}
/>
</div> </div>
</CardSidebar> </CardSidebar>
)} )}
@ -188,7 +215,10 @@ const Sidebar: React.FC = () => {
{engineSettings.length > 0 && ( {engineSettings.length > 0 && (
<CardSidebar title="Engine Parameters" asChild> <CardSidebar title="Engine Parameters" asChild>
<div className="px-2 py-4"> <div className="px-2 py-4">
<EngineSetting componentData={engineSettings} /> <EngineSetting
componentData={engineSettings}
onValueChanged={onValueChanged}
/>
</div> </div>
</CardSidebar> </CardSidebar>
)} )}

View File

@ -73,10 +73,15 @@ const LocalServerScreen = () => {
const { startModel, stateModel } = useActiveModel() const { startModel, stateModel } = useActiveModel()
const selectedModel = useAtomValue(selectedModelAtom) const selectedModel = useAtomValue(selectedModelAtom)
const modelEngineParams = toSettingParams(selectedModel?.settings)
const modelRuntimeParams = toRuntimeParams(selectedModel?.settings) const modelRuntimeParams = toRuntimeParams(selectedModel?.settings)
const componentDataEngineSetting = getConfigurationsData(modelEngineParams) const [currentModelSettingParams, setCurrentModelSettingParams] = useState(
toSettingParams(selectedModel?.settings)
)
const componentDataEngineSetting = getConfigurationsData(
currentModelSettingParams
)
const componentDataRuntimeSetting = getConfigurationsData( const componentDataRuntimeSetting = getConfigurationsData(
modelRuntimeParams, modelRuntimeParams,
selectedModel selectedModel
@ -177,6 +182,16 @@ const LocalServerScreen = () => {
} }
} }
const onValueChanged = useCallback(
(key: string, value: string | number | boolean) => {
setCurrentModelSettingParams({
...currentModelSettingParams,
[key]: value,
})
},
[currentModelSettingParams]
)
return ( return (
<div className="flex h-full w-full" data-testid="local-server-testid"> <div className="flex h-full w-full" data-testid="local-server-testid">
{/* Left SideBar */} {/* Left SideBar */}
@ -495,7 +510,11 @@ const LocalServerScreen = () => {
<div className="mt-4"> <div className="mt-4">
<CardSidebar title="Model Parameters" asChild> <CardSidebar title="Model Parameters" asChild>
<div className="px-2 py-4"> <div className="px-2 py-4">
<ModelSetting componentProps={modelSettings} /> <ModelSetting
componentProps={modelSettings}
disabled={serverEnabled}
onValueChanged={onValueChanged}
/>
</div> </div>
</CardSidebar> </CardSidebar>
</div> </div>
@ -505,7 +524,11 @@ const LocalServerScreen = () => {
<div className="my-4"> <div className="my-4">
<CardSidebar title="Engine Parameters" asChild> <CardSidebar title="Engine Parameters" asChild>
<div className="px-2 py-4"> <div className="px-2 py-4">
<EngineSetting componentData={engineSettings} /> <EngineSetting
disabled={serverEnabled}
componentData={engineSettings}
onValueChanged={onValueChanged}
/>
</div> </div>
</CardSidebar> </CardSidebar>
</div> </div>