Skip to content

Commit

Permalink
fix: assistant settings bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kangfenmao committed Jul 21, 2024
1 parent 4169a2e commit c5965dc
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 211 deletions.
1 change: 0 additions & 1 deletion src/renderer/src/config/constant.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const DEFAULT_TEMPERATURE = 0.7
export const DEFAULT_MAXTOKENS = 800
export const DEFAULT_CONEXTCOUNT = 5
9 changes: 3 additions & 6 deletions src/renderer/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ const resources = {
'input.send': 'Send',
'input.pause': 'Pause',
'input.settings': 'Settings',
'input.estimated_tokens': 'Estimated tokens: ',
'settings.temperature': 'Temperature',
'settings.temperature.tip':
'Lower values make the model more creative and unpredictable, while higher values make it more deterministic and precise.',
'settings.max_tokens': 'Max Tokens',
'settings.max_tokens.tip': 'The maximum number of tokens to generate in the completion.',
'settings.conext_count': 'Context',
'settings.conext_count.tip': 'The number of previous messages to keep in the context.',
'settings.reset': 'Reset',
Expand Down Expand Up @@ -200,12 +199,10 @@ const resources = {
'input.send': '发送',
'input.pause': '暂停',
'input.settings': '设置',
'input.estimated_tokens': '预估消耗',
'settings.temperature': '模型温度',
'settings.temperature.tip':
'模型生成文本的随机程度。值越大,回复内容越赋有多样性、创造性、随机性;设为 0 根据事实回答。日常聊天建议设置为 0.7',
'settings.max_tokens': '最大回复',
'settings.max_tokens.tip':
'最大回复内容多少,数值越大,生成的文本越长。普通聊天建议 500-800;短文生成建议 800-2000;代码生成建议 2000-3600;长文生成建议切换模型到 4000 左右',
'settings.conext_count': '上下文数',
'settings.conext_count.tip':
'要保留在上下文中的消息数量,数值越大,上下文越长,消耗的 token 越多。普通聊天建议 5-10,代码生成建议 5-10',
Expand Down Expand Up @@ -233,7 +230,7 @@ const resources = {
},
settings: {
title: '设置',
general: '常规',
general: '常规设置',
provider: '模型提供商',
model: '模型设置',
assistant: '默认助手',
Expand Down
111 changes: 36 additions & 75 deletions src/renderer/src/pages/home/components/AssistantSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { QuestionCircleOutlined } from '@ant-design/icons'
import { DEFAULT_CONEXTCOUNT, DEFAULT_MAXTOKENS, DEFAULT_TEMPERATURE } from '@renderer/config/constant'
import { DEFAULT_CONEXTCOUNT, DEFAULT_TEMPERATURE } from '@renderer/config/constant'
import { useAssistants } from '@renderer/hooks/useAssistant'
import { Assistant } from '@renderer/types'
import { Button, Col, InputNumber, Popover, Row, Slider, Tooltip } from 'antd'
import { FC, PropsWithChildren, useState } from 'react'
import { debounce } from 'lodash'
import { FC, PropsWithChildren, useCallback, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'

Expand All @@ -14,29 +15,26 @@ interface Props {
const PopoverContent: FC<Props> = ({ assistant }) => {
const { updateAssistant } = useAssistants()
const [temperature, setTemperature] = useState(assistant.settings?.temperature ?? DEFAULT_TEMPERATURE)
const [maxTokens, setMaxTokens] = useState(assistant.settings?.maxTokens ?? DEFAULT_MAXTOKENS)
const [contextCount, setConextCount] = useState(assistant.settings?.contextCount ?? DEFAULT_CONEXTCOUNT)
const { t } = useTranslation()

const onUpdateAssistantSettings = ({
_temperature,
_maxTokens,
_contextCount
}: {
_temperature?: number
_maxTokens?: number
_contextCount?: number
}) => {
updateAssistant({
...assistant,
settings: {
...assistant.settings,
temperature: _temperature ?? temperature,
maxTokens: _maxTokens ?? maxTokens,
contextCount: _contextCount ?? contextCount
}
})
}
const onUpdateAssistantSettings = useCallback(
debounce(
({ _temperature, _contextCount }: { _temperature?: number; _contextCount?: number }) => {
updateAssistant({
...assistant,
settings: {
...assistant.settings,
temperature: _temperature ?? temperature,
contextCount: _contextCount ?? contextCount
}
})
},
1000,
{ leading: false, trailing: true }
),
[]
)

const onTemperatureChange = (value) => {
if (!isNaN(value as number)) {
Expand All @@ -45,13 +43,6 @@ const PopoverContent: FC<Props> = ({ assistant }) => {
}
}

const onMaxTokensChange = (value) => {
if (!isNaN(value as number)) {
setMaxTokens(value)
onUpdateAssistantSettings({ _maxTokens: value })
}
}

const onConextCountChange = (value) => {
if (!isNaN(value as number)) {
setConextCount(value)
Expand All @@ -61,24 +52,27 @@ const PopoverContent: FC<Props> = ({ assistant }) => {

const onReset = () => {
setTemperature(DEFAULT_TEMPERATURE)
setMaxTokens(DEFAULT_MAXTOKENS)
setConextCount(DEFAULT_CONEXTCOUNT)
updateAssistant({
...assistant,
settings: {
...assistant.settings,
temperature: DEFAULT_TEMPERATURE,
maxTokens: DEFAULT_MAXTOKENS,
contextCount: DEFAULT_CONEXTCOUNT
}
})
}

useEffect(() => {
setTemperature(assistant.settings?.temperature ?? DEFAULT_TEMPERATURE)
setConextCount(assistant.settings?.contextCount ?? DEFAULT_CONEXTCOUNT)
}, [assistant])

return (
<Container>
<Row align="middle" style={{ marginBottom: 10 }} gutter={20}>
<Col span={5}>
<Row align="middle">
<Col span={6}>
<Row align="middle" justify="end">
<Label>{t('assistant.settings.temperature')}</Label>
<Tooltip title={t('assistant.settings.temperature.tip')}>
<QuestionIcon />
Expand All @@ -95,11 +89,11 @@ const PopoverContent: FC<Props> = ({ assistant }) => {
step={0.1}
/>
</Col>
<Col span={4}>
<Col span={3}>
<InputNumber
min={0}
max={1.2}
style={{ width: 70, marginLeft: 5, textAlign: 'center' }}
style={{ width: 50, marginLeft: 5, textAlign: 'center' }}
step={0.1}
value={temperature}
onChange={onTemperatureChange}
Expand All @@ -108,8 +102,8 @@ const PopoverContent: FC<Props> = ({ assistant }) => {
</Col>
</Row>
<Row align="middle" style={{ marginBottom: 10 }} gutter={20}>
<Col span={5}>
<Row align="middle">
<Col span={6}>
<Row align="middle" justify="end">
<Label>{t('assistant.settings.conext_count')}</Label>
<Tooltip title={t('assistant.settings.conext_count.tip')}>
<QuestionIcon />
Expand All @@ -126,53 +120,20 @@ const PopoverContent: FC<Props> = ({ assistant }) => {
step={1}
/>
</Col>
<Col span={4}>
<Col span={3}>
<InputNumber
min={0}
max={20}
style={{ width: 70, marginLeft: 5, textAlign: 'center' }}
style={{ width: 50, marginLeft: 5, textAlign: 'center' }}
step={1}
value={contextCount}
onChange={onConextCountChange}
controls={false}
/>
</Col>
</Row>
<Row align="middle" gutter={20}>
<Col span={5}>
<Row align="middle">
<Label>{t('assistant.settings.max_tokens')}</Label>
<Tooltip title={t('assistant.settings.max_tokens.tip')}>
<QuestionIcon />
</Tooltip>
</Row>
</Col>
<Col span={14}>
<Slider
min={0}
max={5000}
onChange={onMaxTokensChange}
value={typeof maxTokens === 'number' ? maxTokens : 0}
marks={{ 0: '0', 800: '800', 2000: '2000', 3600: '3600', 5000: t('assistant.settings.max') }}
step={64}
/>
</Col>
<Col span={4}>
<InputNumber
min={0}
max={5000}
style={{ width: 70, marginLeft: 5, textAlign: 'center' }}
step={64}
value={maxTokens}
onChange={onMaxTokensChange}
controls={false}
/>
</Col>
</Row>
<Row justify="center" style={{ marginTop: 10 }}>
<Button onClick={onReset} style={{ marginRight: 10 }}>
{t('assistant.settings.reset')}
</Button>
<Row justify="center">
<Button onClick={onReset}>{t('assistant.settings.reset')}</Button>
</Row>
</Container>
)
Expand All @@ -199,7 +160,7 @@ const Container = styled.div`
display: flex;
flex-direction: column;
margin-bottom: 8px;
width: 500px;
width: 420px;
padding: 5px;
`

Expand Down
6 changes: 2 additions & 4 deletions src/renderer/src/pages/home/components/Assistants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ const Assistants: FC<Props> = ({ activeAssistant, setActiveAssistant, onCreateAs
const { t } = useTranslation()

const onDelete = (assistant: Assistant) => {
const _assistant = last(assistants.filter((a) => a.id !== assistant.id))
_assistant ? setActiveAssistant(_assistant) : onCreateAssistant()
removeAssistant(assistant.id)
setTimeout(() => {
const _assistant = last(assistants.filter((a) => a.id !== assistant.id))
_assistant ? setActiveAssistant(_assistant) : onCreateAssistant()
}, 0)
}

const items: MenuProps['items'] = [
Expand Down
13 changes: 4 additions & 9 deletions src/renderer/src/pages/home/components/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Assistant, Message } from '@renderer/types'
import { FC, useRef } from 'react'
import { Assistant } from '@renderer/types'
import { FC } from 'react'
import styled from 'styled-components'
import Inputbar from './Inputbar'
import Messages from './Messages'
Expand All @@ -15,17 +15,12 @@ interface Props {
const Chat: FC<Props> = (props) => {
const { assistant } = useAssistant(props.assistant.id)
const { activeTopic, setActiveTopic } = useActiveTopic(assistant)
const messagesRef = useRef<Message[]>([])

if (!assistant) {
return null
}

return (
<Container id="chat">
<Flex vertical flex={1} justify="space-between">
<Messages assistant={assistant} topic={activeTopic} messagesRef={messagesRef} />
<Inputbar assistant={assistant} setActiveTopic={setActiveTopic} messagesRef={messagesRef} />
<Messages assistant={assistant} topic={activeTopic} />
<Inputbar assistant={assistant} setActiveTopic={setActiveTopic} />
</Flex>
<Topics assistant={assistant} activeTopic={activeTopic} setActiveTopic={setActiveTopic} />
</Container>
Expand Down
Loading

0 comments on commit c5965dc

Please sign in to comment.