jan/web/utils/componentSettings.ts
Faisal Amir 314cb03693
feat: stop word model setting (#4113)
* feat: stop word model setting

* chore: update test tag input

* chore: handle UI when no stop word

* chore: fix types of value tag input
2024-11-25 21:17:16 +07:00

74 lines
2.7 KiB
TypeScript

import { Model, SettingComponentProps } from '@janhq/core'
import { presetConfiguration } from './predefinedComponent'
export const getConfigurationsData = (
settings: object,
selectedModel?: Model
): SettingComponentProps[] => {
const componentData: SettingComponentProps[] = []
Object.keys(settings).forEach((key: string) => {
const componentSetting = presetConfiguration[key]
const keySetting = settings[key as keyof typeof settings]
if (!componentSetting) {
return
}
if ('slider' === componentSetting.controllerType) {
const value = Number(keySetting)
if ('value' in componentSetting.controllerProps) {
componentSetting.controllerProps.value = value
if ('max' in componentSetting.controllerProps) {
switch (key) {
case 'max_tokens':
componentSetting.controllerProps.max =
selectedModel?.parameters.max_tokens ||
componentSetting.controllerProps.max ||
4096
break
case 'ctx_len':
componentSetting.controllerProps.max =
selectedModel?.settings.ctx_len ||
componentSetting.controllerProps.max ||
2048
break
}
}
}
} else if ('input' === componentSetting.controllerType) {
const value =
typeof keySetting === 'object' && Array.isArray(keySetting)
? // Support array input with text input
// TODO: remove this when we support muti-tag input
(keySetting as string[])
.filter((e) => e.trim() !== '')
.join(' ')
.concat(
// Keep last space to allow user to add new array element
(keySetting as string[])[
(keySetting as string[]).length - 1
] === ''
? ' '
: ''
)
: (keySetting as string)
const placeholder = keySetting as string
if ('value' in componentSetting.controllerProps)
componentSetting.controllerProps.value = value
if ('placeholder' in componentSetting.controllerProps)
componentSetting.controllerProps.placeholder = placeholder
} else if ('checkbox' === componentSetting.controllerType) {
const checked = keySetting as boolean
if ('value' in componentSetting.controllerProps)
componentSetting.controllerProps.value = checked
} else if ('tag' === componentSetting.controllerType) {
if ('value' in componentSetting.controllerProps)
componentSetting.controllerProps.value = keySetting as string
}
componentData.push(componentSetting)
})
return componentData
}