Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 1 addition & 36 deletions src/components/Prompts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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} />
Expand Down
1 change: 1 addition & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Chat {
description: string;
totalTokens: number;
createdAt: Date;
promptId?: string;
}

export interface Message {
Expand Down
8 changes: 7 additions & 1 deletion src/routes/ChatRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down Expand Up @@ -219,7 +225,7 @@ export function ChatRoute() {
})}
>
<Container>
{messages?.length === 0 && (
{!userPrompt && messages?.length === 0 && (
<SimpleGrid
mb="sm"
spacing="xs"
Expand Down