From 881ba95664164c87e7d45bf6b7ad770ebd0810c1 Mon Sep 17 00:00:00 2001 From: Andrei Canta Date: Sat, 18 Mar 2023 12:42:08 +0200 Subject: [PATCH] add characters, formats, styles and tones --- src/routes/ChatRoute.tsx | 84 ++++++++++++++++-- src/utils/characters.ts | 38 -------- src/utils/constants.ts | 183 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 262 insertions(+), 43 deletions(-) delete mode 100644 src/utils/characters.ts create mode 100644 src/utils/constants.ts diff --git a/src/routes/ChatRoute.tsx b/src/routes/ChatRoute.tsx index 257fd14..b33a6ea 100644 --- a/src/routes/ChatRoute.tsx +++ b/src/routes/ChatRoute.tsx @@ -5,6 +5,8 @@ import { Container, Flex, MediaQuery, + Select, + SimpleGrid, Skeleton, Stack, Textarea, @@ -17,7 +19,12 @@ import { AiOutlineSend } from "react-icons/ai"; import { MessageItem } from "../components/MessageItem"; import { db } from "../db"; import { useChatId } from "../hooks/useChatId"; -import { characters } from "../utils/characters"; +import { + writingCharacters, + writingFormats, + writingStyles, + writingTones, +} from "../utils/constants"; import { createChatCompletion } from "../utils/openai"; export function ChatRoute() { @@ -37,7 +44,23 @@ export function ChatRoute() { return db.chats.get(chatId); }, [chatId]); - const [character, setCharacter] = useState(0); + const [writingCharacter, setWritingCharacter] = useState(null); + const [writingTone, setWritingTone] = useState(null); + const [writingStyle, setWritingStyle] = useState(null); + const [writingFormat, setWritingFormat] = useState(null); + + const getSystemMessage = () => { + const message: string[] = []; + if (writingCharacter) message.push(`You are ${writingCharacter}.`); + if (writingTone) message.push(`Respond in ${writingTone} tone.`); + if (writingStyle) message.push(`Respond in ${writingStyle} style.`); + if (writingFormat) message.push(writingFormat); + if (message.length === 0) + message.push( + "You are ChatGPT, a large language model trained by OpenAI." + ); + return message.join(" "); + }; const submit = async () => { if (submitting) return; @@ -75,7 +98,7 @@ export function ChatRoute() { const result = await createChatCompletion(apiKey, [ { role: "system", - content: `You are a ${characters[character].name}.${characters[character].description}`, + content: getSystemMessage(), }, ...(messages ?? []).map((message) => ({ role: message.role, @@ -111,7 +134,7 @@ export function ChatRoute() { const createChatDscription = await createChatCompletion(apiKey, [ { role: "system", - content: `You are a ${characters[character].name}.${characters[character].description}`, + content: getSystemMessage(), }, ...(messages ?? []).map((message) => ({ role: message.role, @@ -194,10 +217,61 @@ export function ChatRoute() { })} > + {messages?.length === 0 && ( + + + + + )}