2023-03-04 17:39:06 -05:00
|
|
|
import React, { useState, useRef } from 'react';
|
|
|
|
|
import TextareaAutosize from 'react-textarea-autosize';
|
|
|
|
|
import { useDispatch } from 'react-redux';
|
2023-03-04 18:00:12 -05:00
|
|
|
import { setModel, setCustomGpt } from '~/store/submitSlice';
|
2023-03-04 17:39:06 -05:00
|
|
|
import manualSWR from '~/utils/fetchers';
|
|
|
|
|
import { Button } from '../ui/Button.tsx';
|
|
|
|
|
import { Input } from '../ui/Input.tsx';
|
|
|
|
|
import { Label } from '../ui/Label.tsx';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
DialogClose,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle
|
|
|
|
|
} from '../ui/Dialog.tsx';
|
|
|
|
|
|
2023-03-05 17:02:00 -05:00
|
|
|
export default function ModelDialog({ mutate, modelMap }) {
|
2023-03-04 17:39:06 -05:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const [chatGptLabel, setChatGptLabel] = useState('');
|
|
|
|
|
const [promptPrefix, setPromptPrefix] = useState('');
|
|
|
|
|
const [saveText, setSaveText] = useState('Save');
|
|
|
|
|
const [required, setRequired] = useState(false);
|
|
|
|
|
const inputRef = useRef(null);
|
2023-03-06 15:56:25 -05:00
|
|
|
const updateCustomGpt = manualSWR(`http://localhost:3080/api/customGpts/`, 'post');
|
2023-03-04 17:39:06 -05:00
|
|
|
|
|
|
|
|
const submitHandler = (e) => {
|
|
|
|
|
if (chatGptLabel.length === 0) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
setRequired(true);
|
|
|
|
|
inputRef.current.focus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
dispatch(setCustomGpt({ chatGptLabel, promptPrefix }));
|
|
|
|
|
dispatch(setModel('chatgptCustom'));
|
2023-03-04 18:00:12 -05:00
|
|
|
// dispatch(setDisabled(false));
|
2023-03-04 17:39:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const saveHandler = (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const value = chatGptLabel.toLowerCase();
|
|
|
|
|
|
|
|
|
|
if (chatGptLabel.length === 0) {
|
|
|
|
|
setRequired(true);
|
|
|
|
|
inputRef.current.focus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-05 13:31:12 -05:00
|
|
|
updateCustomGpt.trigger({ value, chatGptLabel, promptPrefix });
|
2023-03-04 17:39:06 -05:00
|
|
|
|
|
|
|
|
mutate();
|
2023-03-05 17:02:00 -05:00
|
|
|
setSaveText((prev) => prev + 'd!');
|
2023-03-04 17:39:06 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
|
setSaveText('Save');
|
|
|
|
|
}, 2500);
|
|
|
|
|
|
2023-03-04 18:00:12 -05:00
|
|
|
dispatch(setCustomGpt({ chatGptLabel, promptPrefix }));
|
|
|
|
|
dispatch(setModel('chatgptCustom'));
|
2023-03-04 17:39:06 -05:00
|
|
|
// dispatch(setDisabled(false));
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-05 17:02:00 -05:00
|
|
|
if (modelMap[chatGptLabel.toLowerCase()] && saveText === 'Save') {
|
|
|
|
|
setSaveText('Update');
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-04 17:39:06 -05:00
|
|
|
const requiredProp = required ? { required: true } : {};
|
|
|
|
|
|
|
|
|
|
return (
|
2023-03-05 13:31:12 -05:00
|
|
|
<DialogContent className="shadow-2xl dark:bg-gray-800">
|
2023-03-04 17:39:06 -05:00
|
|
|
<DialogHeader>
|
2023-03-05 13:31:12 -05:00
|
|
|
<DialogTitle className="text-gray-800 dark:text-white">Customize ChatGPT</DialogTitle>
|
|
|
|
|
<DialogDescription className="text-gray-600 dark:text-gray-300">
|
2023-03-04 17:39:06 -05:00
|
|
|
Note: important instructions are often better placed in your message rather than the
|
|
|
|
|
prefix.{' '}
|
|
|
|
|
<a
|
|
|
|
|
href="https://platform.openai.com/docs/guides/chat/instructing-chat-models"
|
|
|
|
|
target="_blank"
|
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
|
>
|
|
|
|
|
<u>More info here</u>
|
|
|
|
|
</a>
|
|
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<div className="grid gap-4 py-4">
|
|
|
|
|
<div className="grid grid-cols-4 items-center gap-4">
|
|
|
|
|
<Label
|
|
|
|
|
htmlFor="chatGptLabel"
|
|
|
|
|
className="text-right"
|
|
|
|
|
>
|
|
|
|
|
Custom Name
|
|
|
|
|
</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="chatGptLabel"
|
|
|
|
|
value={chatGptLabel}
|
|
|
|
|
ref={inputRef}
|
|
|
|
|
onChange={(e) => setChatGptLabel(e.target.value)}
|
|
|
|
|
placeholder="Set a custom name for ChatGPT"
|
2023-03-05 13:31:12 -05:00
|
|
|
className=" col-span-3 shadow-[0_0_10px_rgba(0,0,0,0.10)] outline-none placeholder:text-gray-400 invalid:border-red-400 invalid:text-red-600 invalid:placeholder-red-600 invalid:placeholder-opacity-70 invalid:ring-opacity-10 focus:ring-0 focus:invalid:border-red-400 focus:invalid:ring-red-300 dark:border-none dark:bg-gray-700
|
|
|
|
|
dark:text-gray-50 dark:shadow-[0_0_15px_rgba(0,0,0,0.10)] dark:invalid:border-red-600 dark:invalid:text-red-300 dark:invalid:placeholder-opacity-80 dark:focus:border-none dark:focus:border-transparent dark:focus:outline-none dark:focus:ring-0 dark:focus:ring-gray-400 dark:focus:ring-offset-0 dark:focus:invalid:ring-red-600 dark:focus:invalid:ring-opacity-50"
|
2023-03-04 17:39:06 -05:00
|
|
|
{...requiredProp}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="grid grid-cols-4 items-center gap-4">
|
|
|
|
|
<Label
|
|
|
|
|
htmlFor="promptPrefix"
|
|
|
|
|
className="text-right"
|
|
|
|
|
>
|
|
|
|
|
Prompt Prefix
|
|
|
|
|
</Label>
|
|
|
|
|
<TextareaAutosize
|
|
|
|
|
id="promptPrefix"
|
|
|
|
|
value={promptPrefix}
|
|
|
|
|
onChange={(e) => setPromptPrefix(e.target.value)}
|
|
|
|
|
placeholder="Set custom instructions. Defaults to: 'You are ChatGPT, a large language model trained by OpenAI.'"
|
2023-03-05 13:31:12 -05:00
|
|
|
className="col-span-3 flex h-20 w-full resize-none rounded-md border border-gray-300 bg-transparent py-2 px-3 text-sm shadow-[0_0_10px_rgba(0,0,0,0.10)] outline-none placeholder:text-gray-400 focus:outline-none focus:ring-gray-400 focus:ring-opacity-20 focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 dark:border-none dark:bg-gray-700 dark:text-gray-50 dark:shadow-[0_0_15px_rgba(0,0,0,0.10)] dark:focus:border-none dark:focus:border-transparent dark:focus:outline-none dark:focus:ring-0 dark:focus:ring-gray-400 dark:focus:ring-offset-0"
|
2023-03-04 17:39:06 -05:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<DialogFooter>
|
2023-03-05 13:31:12 -05:00
|
|
|
<DialogClose className="dark:hover:gray-400 border-gray-700">Cancel</DialogClose>
|
2023-03-04 17:39:06 -05:00
|
|
|
<Button
|
|
|
|
|
style={{ backgroundColor: 'rgb(16, 163, 127)' }}
|
|
|
|
|
onClick={saveHandler}
|
2023-03-05 13:31:12 -05:00
|
|
|
className="inline-flex h-10 items-center justify-center rounded-md border-none py-2 px-4 text-sm font-semibold text-white transition-colors dark:text-gray-200"
|
2023-03-04 17:39:06 -05:00
|
|
|
>
|
|
|
|
|
{saveText}
|
|
|
|
|
</Button>
|
|
|
|
|
<DialogClose
|
|
|
|
|
onClick={submitHandler}
|
2023-03-05 13:31:12 -05:00
|
|
|
className="inline-flex h-10 items-center justify-center rounded-md border-none bg-gray-900 py-2 px-4 text-sm font-semibold text-white transition-colors hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-gray-400 focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-gray-100 dark:text-gray-900 dark:hover:bg-gray-200 dark:focus:ring-gray-400 dark:focus:ring-offset-gray-900"
|
2023-03-04 17:39:06 -05:00
|
|
|
>
|
|
|
|
|
Submit
|
|
|
|
|
</DialogClose>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
);
|
|
|
|
|
}
|