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
|
import React, { useCallback, useContext, useEffect, useState } from "react";
import ModelCard from "../components/ModelCard";
import styled from "styled-components";
import { ArrowLeftIcon } from "@heroicons/react/24/outline";
import {
TextInput,
defaultBorderRadius,
lightGray,
vscBackground,
} from "../components";
import { Form, useNavigate } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { GUIClientContext } from "../App";
import { setShowDialog } from "../redux/slices/uiStateSlice";
import { useParams } from "react-router-dom";
import {
MODEL_INFO,
MODEL_PROVIDER_TAG_COLORS,
ModelInfo,
} from "../util/modelData";
import { RootStore } from "../redux/store";
import StyledMarkdownPreview from "../components/StyledMarkdownPreview";
import { getFontSize } from "../util";
import { FormProvider, useForm } from "react-hook-form";
const GridDiv = styled.div`
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-gap: 2rem;
padding: 1rem;
justify-items: center;
align-items: center;
`;
const CustomModelButton = styled.div<{ disabled: boolean }>`
border: 1px solid ${lightGray};
border-radius: ${defaultBorderRadius};
padding: 4px 8px;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
transition: all 0.5s;
${(props) =>
props.disabled
? `
opacity: 0.5;
`
: `
&:hover {
border: 1px solid #be1b55;
background-color: #be1b5522;
cursor: pointer;
}
`}
`;
function ModelConfig() {
const formMethods = useForm();
const { modelName } = useParams();
const [modelInfo, setModelInfo] = useState<ModelInfo | undefined>(undefined);
useEffect(() => {
if (modelName) {
setModelInfo(MODEL_INFO[modelName]);
}
}, [modelName]);
const client = useContext(GUIClientContext);
const dispatch = useDispatch();
const navigate = useNavigate();
const vscMediaUrl = useSelector(
(state: RootStore) => state.config.vscMediaUrl
);
const disableModelCards = useCallback(() => {
return (
modelInfo?.collectInputFor?.some((d) => {
if (!d.required) return false;
const val = formMethods.watch(d.key);
return (
typeof val === "undefined" || (typeof val === "string" && val === "")
);
}) || false
);
}, [modelInfo, formMethods]);
return (
<FormProvider {...formMethods}>
<div className="overflow-y-scroll">
<div
className="items-center flex m-0 p-0 sticky top-0"
style={{
borderBottom: `0.5px solid ${lightGray}`,
backgroundColor: vscBackground,
zIndex: 2,
}}
>
<ArrowLeftIcon
width="1.2em"
height="1.2em"
onClick={() => navigate("/models")}
className="inline-block ml-4 cursor-pointer"
/>
<h3 className="text-lg font-bold m-2 inline-block">
Configure Model
</h3>
</div>
<div className="px-2">
<div style={{ display: "flex", alignItems: "center" }}>
{vscMediaUrl && (
<img
src={`${vscMediaUrl}/logos/${modelInfo?.icon}`}
height="24px"
style={{ marginRight: "10px" }}
/>
)}
<h2>{modelInfo?.title}</h2>
</div>
{modelInfo?.tags?.map((tag) => {
return (
<span
style={{
backgroundColor: `${MODEL_PROVIDER_TAG_COLORS[tag]}55`,
color: "white",
padding: "2px 4px",
borderRadius: defaultBorderRadius,
marginRight: "4px",
}}
>
{tag}
</span>
);
})}
<StyledMarkdownPreview
className="mt-2"
fontSize={getFontSize()}
source={modelInfo?.longDescription || modelInfo?.description || ""}
wrapperElement={{
"data-color-mode": "dark",
}}
maxHeight={200}
/>
<br />
{(modelInfo?.collectInputFor?.filter((d) => d.required).length || 0) >
0 && (
<>
<h3 className="mb-2">Enter required parameters</h3>
{modelInfo?.collectInputFor?.map((d) => {
return (
<div>
<label htmlFor={d.key}>{d.key}</label>
<TextInput
id={d.key}
className="border-2 border-gray-200 rounded-md p-2 m-2"
placeholder={d.key}
defaultValue={d.defaultValue}
{...formMethods.register(d.key, {
required: true,
})}
/>
</div>
);
})}
</>
)}
{(modelInfo?.collectInputFor?.filter((d) => !d.required).length ||
0) > 0 && (
<details>
<summary className="mb-2">
<b>Advanced (optional)</b>
</summary>
{modelInfo?.collectInputFor?.map((d) => {
if (d.required) return null;
return (
<div>
<label htmlFor={d.key}>{d.key}</label>
<TextInput
id={d.key}
className="border-2 border-gray-200 rounded-md p-2 m-2"
placeholder={d.key}
defaultValue={d.defaultValue}
{...formMethods.register(d.key, {
required: false,
})}
/>
</div>
);
})}
</details>
)}
<h3 className="mb-2">Select a model preset</h3>
</div>
<GridDiv>
{modelInfo?.packages.map((pkg) => {
return (
<ModelCard
disabled={disableModelCards()}
title={pkg.title}
description={pkg.description}
tags={pkg.tags}
refUrl={pkg.refUrl}
icon={pkg.icon || modelInfo.icon}
onClick={(e) => {
if (disableModelCards()) return;
const formParams: any = {};
for (const d of modelInfo.collectInputFor || []) {
formParams[d.key] =
d.inputType === "text"
? formMethods.watch(d.key)
: parseInt(formMethods.watch(d.key));
}
client?.addModelForRole("*", modelInfo.class, {
...pkg.params,
...modelInfo.params,
...formParams,
});
navigate("/");
}}
/>
);
})}
<CustomModelButton
disabled={disableModelCards()}
onClick={(e) => {
if (!modelInfo || disableModelCards()) return;
const formParams: any = {};
for (const d of modelInfo.collectInputFor || []) {
formParams[d.key] =
d.inputType === "text"
? formMethods.watch(d.key)
: parseInt(formMethods.watch(d.key));
}
client?.addModelForRole("*", modelInfo.class, {
...modelInfo.packages[0]?.params,
...modelInfo.params,
...formParams,
});
navigate("/");
}}
>
<h3 className="text-center my-2">Configure Model in config.py</h3>
</CustomModelButton>
</GridDiv>
</div>
</FormProvider>
);
}
export default ModelConfig;
|