forked from ElricLiu/AutoGPT-Next-Web
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSettingsDialog.tsx
More file actions
292 lines (282 loc) · 7.59 KB
/
SettingsDialog.tsx
File metadata and controls
292 lines (282 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import React, { useEffect } from "react";
import { useTranslation, Trans } from "next-i18next";
import Button from "./Button";
import {
FaKey,
FaMicrochip,
FaThermometerFull,
FaExclamationCircle,
FaSyncAlt,
FaCoins,
FaCode,
FaServer,
FaComments,
FaSearch,
} from "react-icons/fa";
import Dialog from "./Dialog";
import Input from "./Input";
import { GPT_MODEL_NAMES, GPT_4 } from "../utils/constants";
import Accordion from "./Accordion";
import type { ModelSettings, SettingModel } from "../utils/types";
import { useGuestMode } from "../hooks/useGuestMode";
import clsx from "clsx";
export const SettingsDialog: React.FC<{
show: boolean;
close: () => void;
customSettings: SettingModel;
}> = ({ show, close, customSettings }) => {
const [settings, setSettings] = React.useState<ModelSettings>({
...customSettings.settings,
});
const { isGuestMode } = useGuestMode(settings.customGuestKey);
const { t } = useTranslation(["settings", "common"]);
useEffect(() => {
setSettings(customSettings.settings);
}, [customSettings, close]);
const updateSettings = <Key extends keyof ModelSettings>(
key: Key,
value: ModelSettings[Key]
) => {
setSettings((prev) => {
return { ...prev, [key]: value };
});
};
const keyIsValid = (key: string | undefined) => {
const pattern = /^(sk-[a-zA-Z0-9]{48}|[a-fA-F0-9]{32})$/;
return key && pattern.test(key);
};
const urlIsValid = (url: string | undefined) => {
if (url) {
const pattern = /^(https?:\/\/)?[\w.-]+\.[a-zA-Z]{2,}(\/\S*)?$/;
return pattern.test(url);
}
return true;
};
const handleSave = () => {
if (!isGuestMode && !keyIsValid(settings.customApiKey)) {
alert(
t(
"Key is invalid, please ensure that you have set up billing in your OpenAI account!"
)
);
return;
}
if (!urlIsValid(settings.customEndPoint)) {
alert(
t(
"Endpoint URL is invalid. Please ensure that you have set a correct URL."
)
);
return;
}
customSettings.saveSettings(settings);
close();
return;
};
const handleReset = () => {
customSettings.resetSettings();
close();
};
const disabled = !isGuestMode && !settings.customApiKey;
const advancedSettings = (
<div className="flex flex-col gap-2">
<Input
left={
<>
<FaServer />
<span className="ml-2">{t("endPoint")}</span>
</>
}
disabled={disabled}
value={settings.customEndPoint}
onChange={(e) => updateSettings("customEndPoint", e.target.value)}
/>
<Input
left={
<>
<FaThermometerFull />
<span className="ml-2">{t("temp")}</span>
</>
}
value={settings.customTemperature}
onChange={(e) =>
updateSettings("customTemperature", parseFloat(e.target.value))
}
type="range"
toolTipProperties={{
message: t("temp-tips") as string,
disabled: false,
}}
attributes={{
min: 0,
max: 1,
step: 0.01,
}}
/>
<Input
left={
<>
<FaSyncAlt />
<span className="ml-2">{t("loop")}</span>
</>
}
value={settings.customMaxLoops}
disabled={disabled}
onChange={(e) =>
updateSettings("customMaxLoops", parseFloat(e.target.value))
}
type="range"
toolTipProperties={{
message: t("loop-tips") as string,
disabled: false,
}}
attributes={{
min: 1,
max: 100,
step: 1,
}}
/>
<Input
left={
<>
<FaCoins />
<span className="ml-2">{t("tokens")}</span>
</>
}
value={settings.customMaxTokens ?? 400}
disabled={disabled}
onChange={(e) =>
updateSettings("customMaxTokens", parseFloat(e.target.value))
}
type="range"
toolTipProperties={{
message: t("tokens-tips") as string,
disabled: false,
}}
attributes={{
min: 200,
max: 2000,
step: 100,
}}
/>
<Input
left={
<>
<FaComments />
<span className="ml-2">{t("cody-chat")}</span>
</>
}
value={settings.codyChat}
onChange={(e) => updateSettings("codyChat", e.target.value)}
/>
<Input
left={
<>
<FaSearch />
<span className="ml-2">{t("deep-seek-coder-v3")}</span>
</>
}
value={settings.deepSeekCoderV3}
onChange={(e) => updateSettings("deepSeekCoderV3", e.target.value)}
/>
</div>
);
return (
<Dialog
header={`${t("settings")} ⚙`}
isShown={show}
close={close}
footerButton={
<>
<Button className="bg-red-400 hover:bg-red-500" onClick={handleReset}>
{t("common:reset")}
</Button>
<Button onClick={handleSave}>{t("common:save")}</Button>
</>
}
>
<p>{t("usage")}</p>
<p
className={clsx(
"my-2",
settings.customModelName === GPT_4 &&
"rounded-md border-[2px] border-white/10 bg-yellow-300 text-black"
)}
>
<FaExclamationCircle className="inline-block" />
<Trans i18nKey="gpt4-notice" ns="settings">
<b>
To use the GPT-4 model, you need to also provide the API key for
GPT-4. You can request for it
<a
href="https://openai.com/waitlist/gpt-4-api"
className="text-blue-500"
>
here
</a>
. (ChatGPT Plus subscription will not work)
</b>
</Trans>
</p>
<div className="mt-2 flex flex-col gap-2">
<Input
left={
<>
<FaKey />
<span className="ml-2">{t("key")}</span>
</>
}
placeholder={"sk-..."}
value={settings.customApiKey}
onChange={(e) => updateSettings("customApiKey", e.target.value)}
type="password"
/>
<Input
left={
<>
<FaMicrochip />
<span className="ml-2">{t("model")}</span>
</>
}
type="combobox"
value={settings.customModelName}
onChange={() => null}
setValue={(e) => updateSettings("customModelName", e)}
attributes={{ options: GPT_MODEL_NAMES }}
disabled={disabled}
/>
{isGuestMode && (
<Input
left={
<>
<FaCode />
<span className="ml-2">{t("guest-key")}</span>
</>
}
value={settings.customGuestKey}
onChange={(e) => updateSettings("customGuestKey", e.target.value)}
type="password"
/>
)}
<Accordion
child={advancedSettings}
name={t("advanced-settings")}
></Accordion>
</div>
<Trans i18nKey="api-key-notice" ns="settings">
<strong className="mt-10">
NOTE: To get a key, sign up for an OpenAI account and visit the
following
<a
href="https://platform.openai.com/account/api-keys"
className="text-blue-500"
>
link.
</a>
This key is only used in the current browser session
</strong>
</Trans>
</Dialog>
);
};