Skip to content

Commit

Permalink
refactor: default max_tokens and ctx_len
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored and urmauur committed Jan 11, 2024
1 parent 5bd4637 commit 8b6f7e8
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 38 deletions.
2 changes: 1 addition & 1 deletion web/containers/Checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useActiveModel } from '@/hooks/useActiveModel'
import useUpdateModelParameters from '@/hooks/useUpdateModelParameters'

import { getConfigurationsData } from '@/utils/componentSettings'
import { toSettingParams } from '@/utils/model_param'
import { toSettingParams } from '@/utils/modelParam'

import {
engineParamsUpdateAtom,
Expand Down
2 changes: 1 addition & 1 deletion web/containers/ModelConfigInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import useUpdateModelParameters from '@/hooks/useUpdateModelParameters'

import { getConfigurationsData } from '@/utils/componentSettings'

import { toSettingParams } from '@/utils/model_param'
import { toSettingParams } from '@/utils/modelParam'

import {
engineParamsUpdateAtom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import { useClickOutside } from '@/hooks/useClickOutside'
import useUpdateModelParameters from '@/hooks/useUpdateModelParameters'

import { getConfigurationsData } from '@/utils/componentSettings'
import { toSettingParams } from '@/utils/model_param'

import { selectedModelAtom } from '../DropdownListSidebar'
import { toSettingParams } from '@/utils/modelParam'

import {
engineParamsUpdateAtom,
Expand Down Expand Up @@ -59,7 +57,6 @@ const SliderRightPanel: React.FC<Props> = ({

const setEngineParamsUpdate = useSetAtom(engineParamsUpdateAtom)

const selectedModel = useAtomValue(selectedModelAtom)
const { stopModel } = useActiveModel()

const [showTooltip, setShowTooltip] = useState({ max: false, min: false })
Expand All @@ -77,19 +74,6 @@ const SliderRightPanel: React.FC<Props> = ({
updateModelParameter(threadId, name, e[0])
}

const maxValue = (name: string) => {
switch (name) {
case 'max_tokens':
return selectedModel?.parameters.max_tokens || max

case 'ctx_len':
return selectedModel?.settings.ctx_len || max

default:
return max
}
}

return (
<div className="flex flex-col">
<div className="mb-3 flex items-center gap-x-2">
Expand All @@ -114,12 +98,12 @@ const SliderRightPanel: React.FC<Props> = ({
value={[value]}
onValueChange={onValueChanged}
min={min}
max={maxValue(name)}
max={max}
step={step}
/>
<div className="relative mt-2 flex items-center justify-between text-gray-400">
<p className="text-sm">{min}</p>
<p className="text-sm">{maxValue(name) as number}</p>
<p className="text-sm">{max}</p>
</div>
</div>
<Tooltip open={showTooltip.max || showTooltip.min}>
Expand All @@ -128,11 +112,11 @@ const SliderRightPanel: React.FC<Props> = ({
type="number"
className="-mt-4 h-8 w-20"
min={min}
max={maxValue(name)}
max={max}
value={String(value)}
onChange={(e) => {
if (Number(e.target.value) > Number(maxValue(name))) {
onValueChanged([Number(maxValue(name))])
if (Number(e.target.value) > Number(max)) {
onValueChanged([Number(max)])
setShowTooltip({ max: true, min: false })
} else if (Number(e.target.value) < Number(min)) {
onValueChanged([Number(min)])
Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useSendChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { currentPromptAtom } from '@/containers/Providers/Jotai'

import { toaster } from '@/containers/Toast'

import { toRuntimeParams, toSettingParams } from '@/utils/model_param'
import { toRuntimeParams, toSettingParams } from '@/utils/modelParam'

import { useActiveModel } from './useActiveModel'

Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useUpdateModelParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

import { useAtomValue, useSetAtom } from 'jotai'

import { toRuntimeParams, toSettingParams } from '@/utils/model_param'
import { toRuntimeParams, toSettingParams } from '@/utils/modelParam'

import { extensionManager } from '@/extension'
import {
Expand Down
4 changes: 2 additions & 2 deletions web/screens/Chat/EngineSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useAtomValue } from 'jotai'
import { selectedModelAtom } from '@/containers/DropdownListSidebar'

import { getConfigurationsData } from '@/utils/componentSettings'
import { toSettingParams } from '@/utils/model_param'
import { toSettingParams } from '@/utils/modelParam'

import settingComponentBuilder from '../ModelSetting/settingComponentBuilder'

Expand All @@ -18,7 +18,7 @@ const EngineSetting = () => {

const modelSettingParams = toSettingParams(activeModelParams)

const componentData = getConfigurationsData(modelSettingParams)
const componentData = getConfigurationsData(modelSettingParams, selectedModel)

componentData.sort((a, b) => a.title.localeCompare(b.title))

Expand Down
9 changes: 5 additions & 4 deletions web/screens/Chat/ModelSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useAtomValue } from 'jotai'
import { selectedModelAtom } from '@/containers/DropdownListSidebar'

import { getConfigurationsData } from '@/utils/componentSettings'
import { toRuntimeParams } from '@/utils/model_param'
import { toRuntimeParams } from '@/utils/modelParam'

import settingComponentBuilder from './settingComponentBuilder'

Expand All @@ -20,9 +20,10 @@ const ModelSetting = () => {

const modelRuntimeParams = toRuntimeParams(activeModelParams)

const componentData = getConfigurationsData(modelRuntimeParams)

componentData.sort((a, b) => a.title.localeCompare(b.title))
const componentData = getConfigurationsData(
modelRuntimeParams,
selectedModel
).toSorted((a, b) => a.title.localeCompare(b.title))

return (
<div className="flex flex-col">
Expand Down
4 changes: 2 additions & 2 deletions web/screens/Chat/ModelSetting/settingComponentBuilder.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-case-declarations */
import Checkbox from '@/containers/Checkbox'
import ModelConfigInput from '@/containers/ModelConfigInput'
import Slider from '@/containers/Slider'
import SliderRightPanel from '@/containers/SliderRightPanel'

export type ControllerType = 'slider' | 'checkbox' | 'input'

Expand Down Expand Up @@ -43,7 +43,7 @@ const settingComponentBuilder = (
case 'slider':
const { min, max, step, value } = data.controllerData as SliderData
return (
<Slider
<SliderRightPanel
key={data.name}
title={data.title}
description={data.description}
Expand Down
2 changes: 1 addition & 1 deletion web/screens/Chat/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import DropdownListSidebar, {
import { useCreateNewThread } from '@/hooks/useCreateNewThread'

import { getConfigurationsData } from '@/utils/componentSettings'
import { toRuntimeParams, toSettingParams } from '@/utils/model_param'
import { toRuntimeParams, toSettingParams } from '@/utils/modelParam'

import EngineSetting from '../EngineSetting'
import ModelSetting from '../ModelSetting'
Expand Down
32 changes: 29 additions & 3 deletions web/utils/componentSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModelRuntimeParams, ModelSettingParams } from '@janhq/core'
import { Model, ModelRuntimeParams, ModelSettingParams } from '@janhq/core'

import { presetConfiguration } from '@/screens/Chat/ModelSetting/predefinedComponent'

Expand All @@ -7,9 +7,16 @@ import { SettingComponentData } from '@/screens/Chat/ModelSetting/settingCompone
import { ModelParams } from '@/helpers/atoms/Thread.atom'

export const getConfigurationsData = (
settings: ModelSettingParams | ModelRuntimeParams
settings: ModelSettingParams | ModelRuntimeParams,
selectedModel?: Model
) => {
const componentData: SettingComponentData[] = []

const defaultValue = (value?: number) => {
if (value && value < 4096) return value
return 4096
}

Object.keys(settings).forEach((key: string) => {
const componentSetting = presetConfiguration[key]

Expand All @@ -18,8 +25,27 @@ export const getConfigurationsData = (
}
if ('slider' === componentSetting.controllerType) {
const value = Number(settings[key as keyof ModelParams])
if ('value' in componentSetting.controllerData)
if ('value' in componentSetting.controllerData) {
componentSetting.controllerData.value = value
if ('max' in componentSetting.controllerData) {
switch (key) {
case 'max_tokens':
componentSetting.controllerData.max =
selectedModel?.parameters.max_tokens || 4096
componentSetting.controllerData.value = defaultValue(
selectedModel?.parameters.max_tokens
)
break
case 'ctx_len':
componentSetting.controllerData.max =
selectedModel?.settings.ctx_len || 4096
componentSetting.controllerData.value = defaultValue(
selectedModel?.settings.ctx_len
)
break
}
}
}
} else if ('input' === componentSetting.controllerType) {
const value = settings[key as keyof ModelParams] as string
const placeholder = settings[key as keyof ModelParams] as string
Expand Down
File renamed without changes.

0 comments on commit 8b6f7e8

Please sign in to comment.