-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Modify Ktor config to allow CORS auth * Add API and typings for chat completions * Add generic question page using API * Add settings page and context * Rename Features for Pages in the code organization * Slight styling changes
- Loading branch information
1 parent
ad1c29c
commit 3105b29
Showing
25 changed files
with
633 additions
and
19 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions
4
server/web/src/components/Pages/GenericQuestion/GenericQuestion.module.css
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,4 @@ | ||
.response { | ||
margin-top: 1rem; | ||
white-space: break-spaces; | ||
} |
198 changes: 198 additions & 0 deletions
198
server/web/src/components/Pages/GenericQuestion/GenericQuestion.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,198 @@ | ||
import { ChangeEvent, KeyboardEvent, useContext, useState } from 'react'; | ||
import { | ||
Alert, | ||
Box, | ||
IconButton, | ||
InputAdornment, | ||
Snackbar, | ||
TextField, | ||
Typography, | ||
} from '@mui/material'; | ||
import { SendRounded } from '@mui/icons-material'; | ||
|
||
import { HourglassLoader } from '@/components/HourglassLoader'; | ||
|
||
import { LoadingContext } from '@/state/Loading'; | ||
import { SettingsContext } from '@/state/Settings'; | ||
|
||
import { | ||
ApiOptions, | ||
EndpointsEnum, | ||
apiConfigConstructor, | ||
apiFetch, | ||
defaultApiServer, | ||
} from '@/utils/api'; | ||
|
||
import styles from './GenericQuestion.module.css'; | ||
|
||
const baseHeaders = { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
const chatCompletionsBaseRequest: ChatCompletionsRequest = { | ||
model: 'gpt-3.5-turbo-16k', | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: '', | ||
name: 'USER', | ||
}, | ||
], | ||
temperature: 0.4, | ||
top_p: 1.0, | ||
n: 1, | ||
max_tokens: 12847, | ||
presence_penalty: 0.0, | ||
frequency_penalty: 0.0, | ||
logit_bias: {}, | ||
user: 'USER', | ||
}; | ||
|
||
const chatCompletionsApiBaseOptions: ApiOptions = { | ||
endpointServer: defaultApiServer, | ||
endpointPath: EndpointsEnum.chatCompletions, | ||
endpointValue: '', | ||
requestOptions: { | ||
method: 'POST', | ||
headers: baseHeaders, | ||
}, | ||
}; | ||
|
||
export function GenericQuestion() { | ||
const [loading, setLoading] = useContext(LoadingContext); | ||
const [settings] = useContext(SettingsContext); | ||
const [prompt, setPrompt] = useState<string>(''); | ||
const [showAlert, setShowAlert] = useState<string>(''); | ||
const [responseMessage, setResponseMessage] = | ||
useState<ChatCompletionMessage['content']>(''); | ||
|
||
const handleClick = async () => { | ||
if (!loading) { | ||
try { | ||
setLoading(true); | ||
console.group(`🖱️ Generic question form used:`); | ||
|
||
const chatCompletionsRequest: ChatCompletionsRequest = { | ||
...chatCompletionsBaseRequest, | ||
messages: [ | ||
{ | ||
...chatCompletionsBaseRequest.messages[0], | ||
content: prompt, | ||
}, | ||
], | ||
}; | ||
const chatCompletionsApiOptions: ApiOptions = { | ||
...chatCompletionsApiBaseOptions, | ||
body: JSON.stringify(chatCompletionsRequest), | ||
requestOptions: { | ||
...chatCompletionsApiBaseOptions.requestOptions, | ||
headers: { | ||
...chatCompletionsApiBaseOptions.requestOptions?.headers, | ||
Authorization: `Bearer ${settings.apiKey}`, | ||
}, | ||
}, | ||
}; | ||
const chatCompletionsApiConfig = apiConfigConstructor( | ||
chatCompletionsApiOptions, | ||
); | ||
const chatCompletionResponse = await apiFetch<ChatCompletionsResponse>( | ||
chatCompletionsApiConfig, | ||
); | ||
const { content } = chatCompletionResponse.choices[0].message; | ||
|
||
setResponseMessage(content); | ||
|
||
console.info(`Chat completions request completed`); | ||
} catch (error) { | ||
const userFriendlyError = `Chat completions request couldn't be completed`; | ||
console.info(userFriendlyError); | ||
setShowAlert(` | ||
${userFriendlyError}, is the API key set?`); | ||
} finally { | ||
console.groupEnd(); | ||
setLoading(false); | ||
} | ||
} | ||
}; | ||
|
||
const handleKey = (event: KeyboardEvent<HTMLInputElement>) => { | ||
if (event.key === 'Enter' && !event.shiftKey) { | ||
event.preventDefault(); | ||
handleClick(); | ||
} | ||
}; | ||
|
||
const handleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
setPrompt(event.target.value); | ||
}; | ||
|
||
const disabledButton = loading || !prompt.trim(); | ||
|
||
return ( | ||
<> | ||
<Box | ||
sx={{ | ||
my: 1, | ||
}}> | ||
<Typography variant="h4" gutterBottom> | ||
Generic question | ||
</Typography> | ||
<Typography variant="body1" gutterBottom> | ||
This is an example of a generic call to the xef-server API. | ||
</Typography> | ||
<Typography variant="body1"> | ||
Please check that you have your OpenAI key set in the Settings page. | ||
Then ask any question in the form below: | ||
</Typography> | ||
</Box> | ||
<Box | ||
sx={{ | ||
my: 3, | ||
}}> | ||
<TextField | ||
id="prompt-input" | ||
placeholder="e.g: what's the meaning of life?" | ||
hiddenLabel | ||
multiline | ||
maxRows={2} | ||
value={prompt} | ||
onChange={handleChange} | ||
onKeyDown={handleKey} | ||
disabled={loading} | ||
InputProps={{ | ||
endAdornment: ( | ||
<InputAdornment position="end"> | ||
<IconButton | ||
aria-label="send prompt text" | ||
color="primary" | ||
disabled={disabledButton} | ||
title="Send message" | ||
onClick={handleClick}> | ||
{loading ? <HourglassLoader /> : <SendRounded />} | ||
</IconButton> | ||
</InputAdornment> | ||
), | ||
}} | ||
inputProps={{ | ||
cols: 40, | ||
}} | ||
/> | ||
</Box> | ||
{responseMessage && ( | ||
<> | ||
<Typography variant="h6">Response:</Typography> | ||
<Typography variant="body1" className={styles.response}> | ||
{responseMessage} | ||
</Typography> | ||
</> | ||
)} | ||
<Snackbar | ||
open={!!showAlert} | ||
onClose={(_, reason) => reason !== 'clickaway' && setShowAlert('')} | ||
autoHideDuration={3000}> | ||
<Alert severity="error">{showAlert}</Alert> | ||
</Snackbar> | ||
</> | ||
); | ||
} |
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 @@ | ||
export * from './GenericQuestion'; |
File renamed without changes.
File renamed without changes.
57 changes: 57 additions & 0 deletions
57
server/web/src/components/Pages/SettingsPage/SettingsPage.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,57 @@ | ||
import { ChangeEvent, useContext, useState } from 'react'; | ||
import { Box, Button, TextField, Typography } from '@mui/material'; | ||
import { SaveRounded } from '@mui/icons-material'; | ||
|
||
import { SettingsContext } from '@/state/Settings'; | ||
|
||
export function SettingsPage() { | ||
const [settings, setSettings] = useContext(SettingsContext); | ||
const [apiKeyInput, setApiKeyInput] = useState<string>(settings.apiKey || ''); | ||
|
||
const handleSaving = () => { | ||
setSettings((settings) => ({ ...settings, apiKey: apiKeyInput })); | ||
}; | ||
|
||
const handleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
setApiKeyInput(event.target.value); | ||
}; | ||
|
||
const disabledButton = settings.apiKey === apiKeyInput?.trim(); | ||
|
||
return ( | ||
<> | ||
<Box | ||
sx={{ | ||
my: 1, | ||
}}> | ||
<Typography variant="h4" gutterBottom> | ||
Settings | ||
</Typography> | ||
<Typography variant="body1">These are xef-server settings.</Typography> | ||
</Box> | ||
<Box | ||
sx={{ | ||
my: 3, | ||
}}> | ||
<TextField | ||
id="api-key-input" | ||
label="OpenAI API key" | ||
value={apiKeyInput} | ||
onChange={handleChange} | ||
size="small" | ||
sx={{ | ||
width: { xs: '100%', sm: 550 }, | ||
}} | ||
/> | ||
</Box> | ||
<Button | ||
onClick={handleSaving} | ||
variant="contained" | ||
startIcon={<SaveRounded />} | ||
disableElevation | ||
disabled={disabledButton}> | ||
<Typography variant="button">Save settings</Typography> | ||
</Button> | ||
</> | ||
); | ||
} |
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 @@ | ||
export * from './SettingsPage'; |
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ html { | |
|
||
html, | ||
body { | ||
height: 100%; | ||
height: calc(100% - 64px); | ||
margin: 0; | ||
} | ||
|
||
|
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.