@@ -3,30 +3,55 @@ import CardContent from "@mui/material/CardContent";
33import Typography from "@mui/material/Typography" ;
44import "./assets/style/Options.css" ;
55
6- const KeyRequest = ( { apiKey, setApiKey, setIsValid, setHome } ) => {
6+ const KeyRequest = ( {
7+ apiKey,
8+ setApiKey,
9+ model,
10+ setModel,
11+ baseUrl,
12+ setBaseUrl,
13+ setIsValid,
14+ setHome,
15+ } ) => {
716 setHome ( false ) ;
817
9- const handleChanges = ( e ) => {
18+ const isValidOpenAiApiKey = ( key ) => {
19+ return key . startsWith ( "sk-proj-" ) ;
20+ } ;
21+
22+ const isOpenAiApi = ( baseUrl ) => {
23+ const parsedBaseUrl = URL . parse ( baseUrl ) ;
24+
25+ if ( parsedBaseUrl ) {
26+ return parsedBaseUrl . host === "api.openai.com" ;
27+ }
28+
29+ return false ;
30+ } ;
31+
32+ const handleApiKeyChange = ( e ) => {
1033 setApiKey ( e . target . value ) ;
1134 } ;
1235
36+ const handleModelChange = ( e ) => {
37+ setModel ( e . target . value ) ;
38+ } ;
39+
40+ const handleBaseUrl = ( e ) => {
41+ setBaseUrl ( e . target . value ) ;
42+ } ;
43+
1344 const handleSubmit = ( e ) => {
1445 e . preventDefault ( ) ;
15- if ( isValidApiKey ( apiKey ) ) {
16- setIsValid ( true ) ;
17- } else {
18- alert (
19- "The API key should start with 'sk-' and be followed by a string of exactly 48 alphanumeric characters." ,
20- ) ;
46+
47+ if ( isOpenAiApi ( baseUrl ) && ! isValidOpenAiApiKey ( apiKey ) ) {
48+ alert ( "The OpenAI API key should start with 'sk-proj-'." ) ;
2149 setIsValid ( false ) ;
50+ } else {
51+ setIsValid ( true ) ;
2252 }
2353 } ;
2454
25- const isValidApiKey = ( key ) => {
26- const apiKeyPattern = / ^ s k - ( p r o j - ) ? [ a - z A - Z 0 - 9 ] { 48 } $ / ;
27- return apiKeyPattern . test ( key ) ;
28- } ;
29-
3055 return (
3156 < >
3257 < div id = "cardContainer" >
@@ -38,28 +63,45 @@ const KeyRequest = ({ apiKey, setApiKey, setIsValid, setHome }) => {
3863 < Typography variant = "body2" color = "text.secondary" style = { { whiteSpace : "pre-wrap" } } >
3964 Please provide an{ " " }
4065 < a href = "https://platform.openai.com/account/api-keys" target = "_blank" >
41- OpenAI API key
66+ OpenAI
4267 </ a > { " " }
43- (the key is not saved). < br > </ br > The API key should start with 'sk-' and be followed
44- by a string of exactly 48 alphanumeric characters .
68+ or OpenAI-compatible API key (the key is not saved). < br > </ br > The OpenAI API key
69+ should start with 'sk-proj-' .
4570 </ Typography >
4671 < br />
4772 < label >
48- API Key :{ " " }
73+ API key :{ " " }
4974 < input
5075 type = "text"
51- placeholder = "sk-... OR sk-proj-..."
76+ required
77+ placeholder = "sk-proj-... OR ..."
5278 value = { apiKey }
53- onChange = { handleChanges }
79+ onChange = { handleApiKeyChange }
5480 />
5581 </ label >
82+ < br />
83+ < label >
84+ Model: < input type = "text" required value = { model } onChange = { handleModelChange } />
85+ </ label >
86+ < br />
87+ < label >
88+ Base URL:{ " " }
89+ < input
90+ type = "url"
91+ required
92+ pattern = "^https://.*|^http://localhost:\d+.*"
93+ value = { baseUrl }
94+ onChange = { handleBaseUrl }
95+ />
96+ </ label >
97+ < br />
5698 < button type = "submit" tabIndex = { 0 } >
5799 Submit
58100 </ button >
59101 </ form >
60- { apiKey . length > 0 && ! isValidApiKey ( apiKey ) && (
102+ { apiKey . length > 0 && ! isValidOpenAiApiKey ( apiKey ) && isOpenAiApi ( baseUrl ) && (
61103 < p style = { { color : "red" } } role = "alert" >
62- Invalid API key format.
104+ Invalid OpenAI API key format.
63105 </ p >
64106 ) }
65107 </ CardContent >
0 commit comments