-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Max token Setting #315
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,66 @@ | ||
import React from "react"; | ||
import React, { useEffect } from "react"; | ||
import Button from "./Button"; | ||
import { | ||
FaKey, | ||
FaMicrochip, | ||
FaThermometerFull, | ||
FaExclamationCircle, | ||
FaSyncAlt, | ||
FaCoins, | ||
} from "react-icons/fa"; | ||
import Dialog from "./Dialog"; | ||
import Input from "./Input"; | ||
import { | ||
GPT_MODEL_NAMES, | ||
GPT_4, | ||
DEFAULT_MAX_LOOPS_CUSTOM_API_KEY, | ||
DEFAULT_MAX_LOOPS_FREE, | ||
} from "../utils/constants"; | ||
import { GPT_MODEL_NAMES, GPT_4 } from "../utils/constants"; | ||
import Accordion from "./Accordion"; | ||
import type { reactModelStates } from "./types"; | ||
import type { ModelSettings } from "../utils/types"; | ||
|
||
export default function SettingsDialog({ | ||
show, | ||
close, | ||
reactModelStates, | ||
}: { | ||
export const SettingsDialog: React.FC<{ | ||
show: boolean; | ||
close: () => void; | ||
reactModelStates: reactModelStates; | ||
}) { | ||
const { | ||
customApiKey, | ||
setCustomApiKey, | ||
customModelName, | ||
setCustomModelName, | ||
customTemperature, | ||
setCustomTemperature, | ||
customMaxLoops, | ||
setCustomMaxLoops, | ||
} = reactModelStates; | ||
customSettings: [ | ||
ModelSettings, | ||
React.Dispatch<React.SetStateAction<ModelSettings>> | ||
]; | ||
}> = ({ show, close, customSettings: [customSettings, setCustomSettings] }) => { | ||
const [settings, setSettings] = React.useState<ModelSettings>({ | ||
...customSettings, | ||
}); | ||
|
||
const [key, setKey] = React.useState<string>(customApiKey); | ||
useEffect(() => { | ||
setSettings(customSettings); | ||
}, [customSettings, close]); | ||
|
||
const handleClose = () => { | ||
setKey(customApiKey); | ||
close(); | ||
const updateSettings = <Key extends keyof ModelSettings>( | ||
key: Key, | ||
value: ModelSettings[Key] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. generics 😍 |
||
) => { | ||
setSettings((prev) => { | ||
return { ...prev, [key]: value }; | ||
}); | ||
}; | ||
|
||
function is_valid_key(key: string) { | ||
function keyIsValid(key: string | undefined) { | ||
const pattern = /^sk-[a-zA-Z0-9]{48}$/; | ||
return pattern.test(key); | ||
return key && pattern.test(key); | ||
} | ||
|
||
const handleSave = () => { | ||
if (is_valid_key(key)) { | ||
setCustomApiKey(key); | ||
close(); | ||
} else { | ||
if (!keyIsValid(settings.customApiKey)) { | ||
alert( | ||
"key is invalid, please ensure that you have set up billing in your OpenAI account" | ||
); | ||
return; | ||
} | ||
}; | ||
|
||
React.useEffect(() => { | ||
setCustomMaxLoops( | ||
!key ? DEFAULT_MAX_LOOPS_FREE : DEFAULT_MAX_LOOPS_CUSTOM_API_KEY | ||
); | ||
setCustomSettings((prev) => { | ||
return { ...prev, ...settings }; | ||
}); | ||
|
||
return () => { | ||
setCustomMaxLoops(DEFAULT_MAX_LOOPS_FREE); | ||
}; | ||
}, [key, setCustomMaxLoops]); | ||
close(); | ||
return; | ||
}; | ||
|
||
const disabled = !settings.customApiKey; | ||
const advancedSettings = ( | ||
<> | ||
<Input | ||
|
@@ -80,8 +70,10 @@ export default function SettingsDialog({ | |
<span className="ml-2">Temp: </span> | ||
</> | ||
} | ||
value={customTemperature} | ||
onChange={(e) => setCustomTemperature(parseFloat(e.target.value))} | ||
value={settings.customTemperature} | ||
onChange={(e) => | ||
updateSettings("customTemperature", parseFloat(e.target.value)) | ||
} | ||
type="range" | ||
toolTipProperties={{ | ||
message: | ||
|
@@ -102,9 +94,11 @@ export default function SettingsDialog({ | |
<span className="ml-2">Loop #: </span> | ||
</> | ||
} | ||
value={customMaxLoops} | ||
disabled={!key} | ||
onChange={(e) => setCustomMaxLoops(parseFloat(e.target.value))} | ||
value={settings.customMaxLoops} | ||
disabled={disabled} | ||
onChange={(e) => | ||
updateSettings("customMaxLoops", parseFloat(e.target.value)) | ||
} | ||
type="range" | ||
toolTipProperties={{ | ||
message: | ||
|
@@ -117,14 +111,39 @@ export default function SettingsDialog({ | |
step: 1, | ||
}} | ||
/> | ||
<br /> | ||
<Input | ||
left={ | ||
<> | ||
<FaCoins /> | ||
<span className="ml-2">Tokens: </span> | ||
</> | ||
} | ||
value={settings.maxTokens ?? 400} | ||
disabled={disabled} | ||
onChange={(e) => | ||
updateSettings("maxTokens", parseFloat(e.target.value)) | ||
} | ||
type="range" | ||
toolTipProperties={{ | ||
message: | ||
"Controls the maximum number of tokens used in each API call (higher value will make responses more detailed but cost more).", | ||
disabled: false, | ||
}} | ||
attributes={{ | ||
min: 200, | ||
max: 2000, | ||
step: 100, | ||
}} | ||
/> | ||
</> | ||
); | ||
|
||
return ( | ||
<Dialog | ||
header="Settings ⚙" | ||
isShown={show} | ||
close={handleClose} | ||
close={close} | ||
footerButton={<Button onClick={handleSave}>Save</Button>} | ||
> | ||
<p> | ||
|
@@ -135,7 +154,7 @@ export default function SettingsDialog({ | |
<br /> | ||
<p | ||
className={ | ||
customModelName === GPT_4 | ||
settings.customModelName === GPT_4 | ||
? "rounded-md border-[2px] border-white/10 bg-yellow-300 text-black" | ||
: "" | ||
} | ||
|
@@ -159,29 +178,30 @@ export default function SettingsDialog({ | |
<Input | ||
left={ | ||
<> | ||
<FaMicrochip /> | ||
<span className="ml-2">Model:</span> | ||
<FaKey /> | ||
<span className="ml-2">Key: </span> | ||
</> | ||
} | ||
type="combobox" | ||
value={customModelName} | ||
onChange={() => null} | ||
setValue={setCustomModelName} | ||
attributes={{ options: GPT_MODEL_NAMES }} | ||
placeholder={"sk-..."} | ||
value={settings.customApiKey} | ||
onChange={(e) => updateSettings("customApiKey", e.target.value)} | ||
/> | ||
<br className="hidden md:inline" /> | ||
<br className="md:inline" /> | ||
<Input | ||
left={ | ||
<> | ||
<FaKey /> | ||
<span className="ml-2">Key: </span> | ||
<FaMicrochip /> | ||
<span className="ml-2">Model:</span> | ||
</> | ||
} | ||
placeholder={"sk-..."} | ||
value={key} | ||
onChange={(e) => setKey(e.target.value)} | ||
type="combobox" | ||
value={settings.customModelName} | ||
onChange={() => null} | ||
setValue={(e) => updateSettings("customModelName", e)} | ||
attributes={{ options: GPT_MODEL_NAMES }} | ||
disabled={disabled} | ||
/> | ||
<br className="md:inline" /> | ||
<br className="hidden md:inline" /> | ||
<Accordion | ||
child={advancedSettings} | ||
name="Advanced Settings" | ||
|
@@ -201,4 +221,4 @@ export default function SettingsDialog({ | |
</div> | ||
</Dialog> | ||
); | ||
} | ||
}; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
automatic re-order