Skip to content

Commit c615b97

Browse files
committed
model selector
1 parent 50e4918 commit c615b97

File tree

4 files changed

+88
-40
lines changed

4 files changed

+88
-40
lines changed

src/client/components/ChatV2/ChatV2.tsx

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { AppContext } from '../../util/context'
2020
import { Close, Settings } from '@mui/icons-material'
2121
import { SettingsModal } from './SettingsModal'
2222
import { Link } from 'react-router-dom'
23+
import { useScrollToBottom } from './useScrollToBottom'
2324

2425
export const ChatV2 = () => {
2526
const { courseId } = useParams()
@@ -36,11 +37,11 @@ export const ChatV2 = () => {
3637
const [prevResponse, setPrevResponse] = useLocalStorageState<{
3738
id: string
3839
}>('general-prev-response', { id: '' })
39-
40+
4041
const appContainerRef = useContext(AppContext)
4142
const chatContainerRef = useRef<HTMLDivElement>(null)
4243
const inputFileRef = useRef<HTMLInputElement>(null)
43-
44+
4445
const [settingsModalOpen, setSettingsModalOpen] = useState(false)
4546
const [activePromptId, setActivePromptId] = useState('')
4647
const [fileName, setFileName] = useState<string>('')
@@ -52,7 +53,7 @@ export const ChatV2 = () => {
5253
const [tokenWarningVisible, setTokenWarningVisible] = useState(false)
5354
const [modelTemperature, setModelTemperature] = useState(0.5)
5455
const [saveConsent, setSaveConsent] = useState(true)
55-
56+
5657
const [setRetryTimeout, clearRetryTimeout] = useRetryTimeout()
5758

5859
const { t, i18n } = useTranslation()
@@ -169,27 +170,7 @@ export const ChatV2 = () => {
169170
clearRetryTimeout()
170171
}
171172

172-
useEffect(() => {
173-
const chatContainer = chatContainerRef.current
174-
const appContainer = appContainerRef.current
175-
176-
if (!chatContainer || !appContainer || !messages.length) return
177-
178-
const scrollToBottom = () => {
179-
appContainer.scrollIntoView({
180-
behavior: 'smooth',
181-
block: 'end',
182-
})
183-
}
184-
185-
const resizeObserver = new ResizeObserver(() => {
186-
scrollToBottom()
187-
})
188-
189-
resizeObserver.observe(chatContainer)
190-
191-
return () => resizeObserver.disconnect()
192-
}, [])
173+
useScrollToBottom(chatContainerRef, appContainerRef, messages)
193174

194175
return (
195176
<Box
@@ -199,20 +180,17 @@ export const ChatV2 = () => {
199180
flexDirection: 'column',
200181
}}
201182
>
202-
<SettingsModal open={settingsModalOpen} setOpen={setSettingsModalOpen}></SettingsModal>
203-
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center'}}>
204-
205-
<Box sx={{ display: 'flex', gap: '1rem' }}>
206-
{disclaimerInfo && <Disclaimer disclaimer={disclaimerInfo} />}
207-
<SystemPrompt content={system.content} setContent={(content) => setSystem({ content })} />
208-
<Button onClick={handleReset}>Reset</Button>
209-
<IconButton onClick={() => setSettingsModalOpen(true)} title="Settings">
210-
<Settings></Settings>
211-
</IconButton>
212-
</Box>
213-
{
214-
courseId ? <Link to={'/v2'}>CurreChat</Link> : <Link to={'/v2/sandbox'}>Ohtu Sandbox</Link>
215-
}
183+
<SettingsModal open={settingsModalOpen} setOpen={setSettingsModalOpen} model={model.name} setModel={(name) => setModel({ name })}></SettingsModal>
184+
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
185+
<Box sx={{ display: 'flex', gap: '1rem' }}>
186+
{disclaimerInfo && <Disclaimer disclaimer={disclaimerInfo} />}
187+
<SystemPrompt content={system.content} setContent={(content) => setSystem({ content })} />
188+
<Button onClick={handleReset}>Reset</Button>
189+
<IconButton onClick={() => setSettingsModalOpen(true)} title="Settings">
190+
<Settings></Settings>
191+
</IconButton>
192+
</Box>
193+
{courseId ? <Link to={'/v2'}>CurreChat</Link> : <Link to={'/v2/sandbox'}>Ohtu Sandbox</Link>}
216194
</Box>
217195
<Box ref={chatContainerRef}>
218196
<Conversation messages={messages} completion={completion} />
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useTranslation } from 'react-i18next'
2+
import { Box, Typography, MenuItem, FormControl, Select, SelectChangeEvent, InputLabel } from '@mui/material'
3+
import { FREE_MODEL } from '../../../config'
4+
5+
const ModelSelector = ({ currentModel, setModel, models }: { currentModel: string; setModel: (model: string) => void; models: string[] }) => {
6+
const { t } = useTranslation()
7+
8+
if (models.length === 1) {
9+
return (
10+
<Box style={{ marginBottom: 5 }}>
11+
<Typography variant="body1">
12+
{t('status:modelInUse')} <code>{models[0]}</code>
13+
</Typography>
14+
</Box>
15+
)
16+
}
17+
18+
return (
19+
<Box mb={2}>
20+
<FormControl sx={{ width: '200px' }}>
21+
<InputLabel>{t('status:modelInUse')}</InputLabel>
22+
<Select label={t('status:modelInUse')} value={currentModel} onChange={(event: SelectChangeEvent) => setModel(event.target.value)}>
23+
{models.map((model) => (
24+
<MenuItem key={model} value={model}>
25+
{model}
26+
</MenuItem>
27+
))}
28+
</Select>
29+
</FormControl>
30+
</Box>
31+
)
32+
}
33+
34+
export default ModelSelector

src/client/components/ChatV2/SettingsModal.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { Close } from '@mui/icons-material'
22
import { Box, IconButton, Modal, Typography } from '@mui/material'
3+
import ModelSelector from './ModelSelector'
4+
import { validModels } from '../../../config'
5+
import React from 'react'
36

4-
export const SettingsModal = ({ open, setOpen }) => {
7+
interface SettingsModalProps {
8+
open: boolean
9+
setOpen: (open: boolean) => void
10+
model: string
11+
setModel: (model: string) => void
12+
}
13+
14+
export const SettingsModal: React.FC<SettingsModalProps> = ({ open, setOpen, model, setModel }) => {
515
return (
616
<Modal open={open} onClose={() => setOpen(false)}>
717
<Box
@@ -18,11 +28,12 @@ export const SettingsModal = ({ open, setOpen }) => {
1828
}}
1929
>
2030
<IconButton onClick={() => setOpen(false)} sx={{ position: 'absolute', top: 8, right: 8, color: 'grey.500' }}>
21-
<Close></Close>
31+
<Close />
2232
</IconButton>
2333
<Typography id="modal-title" variant="h6" component="h2">
2434
Settings
2535
</Typography>
36+
<ModelSelector currentModel={model} setModel={setModel} models={validModels.map(m => m.name)} />
2637
</Box>
2738
</Modal>
2839
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useEffect } from 'react'
2+
3+
export const useScrollToBottom = (chatContainerRef: React.RefObject<HTMLElement>, appContainerRef: React.RefObject<HTMLElement>, messages: any[]) => {
4+
useEffect(() => {
5+
const chatContainer = chatContainerRef.current
6+
const appContainer = appContainerRef.current
7+
8+
if (!chatContainer || !appContainer || !messages.length) return
9+
10+
const scrollToBottom = () => {
11+
appContainer.scrollIntoView({
12+
behavior: 'smooth',
13+
block: 'end',
14+
})
15+
}
16+
17+
const resizeObserver = new ResizeObserver(() => {
18+
scrollToBottom()
19+
})
20+
21+
resizeObserver.observe(chatContainer)
22+
23+
return () => resizeObserver.disconnect()
24+
}, [chatContainerRef, appContainerRef, messages.length])
25+
}

0 commit comments

Comments
 (0)