generated from Amery2010/nextjs-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: The conversation ui supports switching models and displaying title
refactor: Optimize file name display in file list
- Loading branch information
Showing
7 changed files
with
133 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { useMemo, useCallback, memo } from 'react' | ||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' | ||
import { useSettingStore, useEnvStore } from '@/store/setting' | ||
import { useModelStore } from '@/store/model' | ||
import { Model } from '@/constant/model' | ||
import { values, keys, find } from 'lodash-es' | ||
|
||
type Props = { | ||
className?: string | ||
defaultModel: string | ||
} | ||
|
||
function ModelSelect({ className, defaultModel }: Props) { | ||
const { models } = useModelStore() | ||
const { update } = useSettingStore() | ||
const { modelList: MODEL_LIST } = useEnvStore() | ||
|
||
const modelOptions = useMemo(() => { | ||
if (models.length > 0) { | ||
const systemModels = values(Model) | ||
models.forEach((item) => { | ||
const modelName = item.name.replace('models/', '') | ||
if (!systemModels.includes(modelName)) { | ||
Model[modelName] = item.displayName | ||
} | ||
}) | ||
} | ||
|
||
let modelList: string[] = [] | ||
const defaultModelList: string[] = keys(Model) | ||
const userModels: string[] = MODEL_LIST ? MODEL_LIST.split(',') : [] | ||
|
||
userModels.forEach((modelName) => { | ||
for (const name of defaultModelList) { | ||
if (!modelList.includes(name)) modelList.push(name) | ||
} | ||
if (modelName === 'all' || modelName === '+all') { | ||
} else if (modelName === '-all') { | ||
modelList = modelList.filter((name) => !defaultModelList.includes(name)) | ||
} else if (modelName.startsWith('-')) { | ||
modelList = modelList.filter((name) => name !== modelName.substring(1)) | ||
} else if (modelName.startsWith('@')) { | ||
const name = modelName.substring(1) | ||
if (!modelList.includes(name)) modelList.push(name) | ||
} else { | ||
modelList.push(modelName.startsWith('+') ? modelName.substring(1) : modelName) | ||
} | ||
}) | ||
|
||
return modelList.length > 0 ? modelList : defaultModelList | ||
}, [models, MODEL_LIST]) | ||
|
||
const handleModelChange = useCallback( | ||
(name: string) => { | ||
const currentModel = find(models, { name: `models/${name}` }) | ||
if (currentModel) { | ||
const values: Record<string, number> = {} | ||
const { topP, topK, temperature, outputTokenLimit } = currentModel | ||
if (topP) values.topP = topP | ||
if (topK) values.topK = topK | ||
if (temperature) values.temperature = temperature | ||
if (outputTokenLimit) values.maxOutputTokens = outputTokenLimit | ||
update({ model: name, ...values }) | ||
} else { | ||
update({ model: name }) | ||
} | ||
}, | ||
[update, models], | ||
) | ||
|
||
return ( | ||
<Select | ||
defaultValue={defaultModel} | ||
onValueChange={(value) => { | ||
handleModelChange(value) | ||
}} | ||
> | ||
<SelectTrigger className={className}> | ||
<SelectValue placeholder="Model" /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
{modelOptions.map((name) => { | ||
return ( | ||
<SelectItem key={name} value={name}> | ||
{name} | ||
</SelectItem> | ||
) | ||
})} | ||
</SelectContent> | ||
</Select> | ||
) | ||
} | ||
|
||
export default memo(ModelSelect) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters