From 515960197d2f0f0c06f4be8e3857c87bfbafcf39 Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Thu, 23 Mar 2023 12:43:22 -0500 Subject: [PATCH] send custom prompts with system role --- src/components/Prompts.tsx | 37 +------------------------------------ src/db/index.ts | 1 + src/routes/ChatRoute.tsx | 8 +++++++- 3 files changed, 9 insertions(+), 37 deletions(-) diff --git a/src/components/Prompts.tsx b/src/components/Prompts.tsx index b2fa622..b7da7ab 100644 --- a/src/components/Prompts.tsx +++ b/src/components/Prompts.tsx @@ -91,45 +91,10 @@ export function Prompts({ description: "New Chat", totalTokens: 0, createdAt: new Date(), - }); - await db.messages.add({ - id: nanoid(), - chatId: id, - content: prompt.content, - role: "user", - createdAt: new Date(), + promptId: prompt.id, }); navigate({ to: `/chats/${id}` }); onPlay(); - - const result = await createChatCompletion(apiKey, [ - { - role: "system", - content: - "You are ChatGPT, a large language model trained by OpenAI.", - }, - { role: "user", content: prompt.content }, - ]); - - const resultDescription = - result.data.choices[0].message?.content; - await db.messages.add({ - id: nanoid(), - chatId: id, - content: resultDescription ?? "unknown reponse", - role: "assistant", - createdAt: new Date(), - }); - - if (result.data.usage) { - await db.chats.where({ id: id }).modify((chat) => { - if (chat.totalTokens) { - chat.totalTokens += result.data.usage!.total_tokens; - } else { - chat.totalTokens = result.data.usage!.total_tokens; - } - }); - } }} > diff --git a/src/db/index.ts b/src/db/index.ts index 60668cc..26683eb 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -6,6 +6,7 @@ export interface Chat { description: string; totalTokens: number; createdAt: Date; + promptId?: string; } export interface Message { diff --git a/src/routes/ChatRoute.tsx b/src/routes/ChatRoute.tsx index 4cba4e0..6d30d3e 100644 --- a/src/routes/ChatRoute.tsx +++ b/src/routes/ChatRoute.tsx @@ -45,12 +45,18 @@ export function ChatRoute() { return db.chats.get(chatId); }, [chatId]); + const userPrompt = useLiveQuery(async () => { + if (!chat?.promptId) return null; + return db.prompts.get(chat.promptId); + }, [chat]); + const [writingCharacter, setWritingCharacter] = useState(null); const [writingTone, setWritingTone] = useState(null); const [writingStyle, setWritingStyle] = useState(null); const [writingFormat, setWritingFormat] = useState(null); const getSystemMessage = () => { + if (userPrompt) return userPrompt.content; const message: string[] = []; if (writingCharacter) message.push(`You are ${writingCharacter}.`); if (writingTone) message.push(`Respond in ${writingTone} tone.`); @@ -219,7 +225,7 @@ export function ChatRoute() { })} > - {messages?.length === 0 && ( + {!userPrompt && messages?.length === 0 && (