mirror of
https://github.com/deiucanta/chatpad.git
synced 2026-03-11 09:04:31 +00:00
send custom prompts with system role
This commit is contained in:
parent
38b585cea9
commit
515960197d
3 changed files with 9 additions and 37 deletions
|
|
@ -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} />
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ export interface Chat {
|
|||
description: string;
|
||||
totalTokens: number;
|
||||
createdAt: Date;
|
||||
promptId?: string;
|
||||
}
|
||||
|
||||
export interface Message {
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Reference in a new issue