-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ryan Holinshead
committed
Dec 29, 2023
1 parent
c88b9c9
commit 434a192
Showing
8 changed files
with
218 additions
and
27 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
82 changes: 82 additions & 0 deletions
82
python/src/aiconfig/editor/client/src/components/prompt/ModelSelector.tsx
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,82 @@ | ||
import { Autocomplete, AutocompleteItem, Button } from "@mantine/core"; | ||
import { memo, useState } from "react"; | ||
import { getPromptModelName } from "../../utils/promptUtils"; | ||
import { Prompt } from "aiconfig"; | ||
import useLoadModels from "../../hooks/useLoadModels"; | ||
import { IconX } from "@tabler/icons-react"; | ||
|
||
type Props = { | ||
prompt: Prompt; | ||
getModels: (search: string) => Promise<string[]>; | ||
onSetModel: (model?: string) => void; | ||
defaultConfigModelName?: string; | ||
}; | ||
|
||
export default memo(function ModelSelector({ | ||
prompt, | ||
getModels, | ||
onSetModel, | ||
defaultConfigModelName, | ||
}: Props) { | ||
const [selectedModel, setSelectedModel] = useState<string | undefined>( | ||
getPromptModelName(prompt, defaultConfigModelName) | ||
); | ||
const [showAll, setShowAll] = useState(true); | ||
const [autocompleteSearch, setAutocompleteSearch] = useState( | ||
getPromptModelName(prompt, defaultConfigModelName) | ||
); | ||
|
||
const models = useLoadModels(showAll ? "" : autocompleteSearch, getModels); | ||
|
||
const onSelectModel = (model?: string) => { | ||
setSelectedModel(model); | ||
onSetModel(model); | ||
}; | ||
|
||
return ( | ||
<Autocomplete | ||
placeholder="Select model" | ||
limit={100} | ||
maxDropdownHeight={200} | ||
rightSection={ | ||
selectedModel ? ( | ||
<Button | ||
size="xs" | ||
variant="subtle" | ||
mr={10} | ||
onClick={() => { | ||
onSelectModel(undefined); | ||
setShowAll(true); | ||
setAutocompleteSearch(""); | ||
}} | ||
> | ||
<IconX size={10} /> | ||
</Button> | ||
) : null | ||
} | ||
filter={(searchValue: string, item: AutocompleteItem) => { | ||
if (showAll) { | ||
return true; | ||
} | ||
|
||
const modelName: string = item.value; | ||
return modelName | ||
.toLocaleLowerCase() | ||
.includes(searchValue.toLocaleLowerCase().trim()); | ||
}} | ||
data={models} | ||
value={autocompleteSearch} | ||
onChange={(value: string) => { | ||
setAutocompleteSearch(value); | ||
setShowAll(false); | ||
onSelectModel(value); | ||
models.some((model) => { | ||
if (model === value) { | ||
setShowAll(true); | ||
return true; | ||
} | ||
}); | ||
}} | ||
/> | ||
); | ||
}); |
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
28 changes: 28 additions & 0 deletions
28
python/src/aiconfig/editor/client/src/hooks/useLoadModels.ts
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,28 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { showNotification } from "@mantine/notifications"; | ||
|
||
export default function useLoadModels( | ||
modelSearch: string, | ||
getModels: (search: string) => Promise<string[]> | ||
) { | ||
const [models, setModels] = useState<string[]>([]); | ||
|
||
const loadModels = useCallback(async (modelSearch: string) => { | ||
try { | ||
const models = await getModels(modelSearch); | ||
setModels(models); | ||
} catch (err: any) { | ||
showNotification({ | ||
title: "Error loading models", | ||
message: err?.message, | ||
color: "red", | ||
}); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
loadModels(modelSearch); | ||
}, [loadModels, modelSearch]); | ||
|
||
return models; | ||
} |
Oops, something went wrong.