send custom prompts with system role

This commit is contained in:
Rudis Muiznieks 2023-03-23 12:43:22 -05:00
parent 38b585cea9
commit 515960197d
No known key found for this signature in database
GPG key ID: CABF2F86EF7884F9
3 changed files with 9 additions and 37 deletions

View file

@ -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;
}
});
}
}}
>
<IconPlayerPlay size={20} />

View file

@ -6,6 +6,7 @@ export interface Chat {
description: string;
totalTokens: number;
createdAt: Date;
promptId?: string;
}
export interface Message {

View file

@ -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<string | null>(null);
const [writingTone, setWritingTone] = useState<string | null>(null);
const [writingStyle, setWritingStyle] = useState<string | null>(null);
const [writingFormat, setWritingFormat] = useState<string | null>(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() {
})}
>
<Container>
{messages?.length === 0 && (
{!userPrompt && messages?.length === 0 && (
<SimpleGrid
mb="sm"
spacing="xs"