chore: update usage database schema

This commit is contained in:
steven
2023-04-24 18:07:13 +08:00
parent a82308bd8b
commit 00e3040670
5 changed files with 115 additions and 87 deletions

View File

@ -76,60 +76,24 @@ docker run --name sqlchat --platform linux/amd64 --env OPENAI_API_KEY=xxx --env
### Database Setup ### Database Setup
1. Install prisma 1. Install dependencies
```bash ```bash
pnpm install prisma --save-dev pnpm i
``` ```
2. Install prisma client 1. Generate prisma client from the model
```bash
pnpm install @prisma/client
```
3. Generate prisma client from the model
```bash ```bash
pnpm prisma generate pnpm prisma generate
``` ```
4. Seed data 1. Seed data
```bash
pnpm install typescript ts-node @types/node --save-dev
```
```bash ```bash
pnpm prisma db seed pnpm prisma db seed
``` ```
## Common Questions
<details><summary>How to self host SQL Chat?</summary>
<p>
- You can deploy SQL Chat to Vercel with one click
<a href="https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fsqlchat%2Fsqlchat&env=OPENAI_API_KEY"><img src="https://img.shields.io/badge/deploy%20on-Vercel-brightgreen.svg?style=for-the-badge&logo=vercel" alt="vercel"></a>
- You can deploy your SQL Chat with docker in seconds
```bash
docker run --name sqlchat --platform linux/amd64 -p 3000:3000 sqlchat/sqlchat
```
</p>
</details>
<details><summary>It always says that I have a network connection issue?</summary>
<p>
Please make sure you have a stable network connection which can access the OpenAI API endpoint. If you cannot access the OpenAI API endpoint, you can try to set the `OPENAI_API_ENDPOINT` in UI or environment variable.
</p>
</details>
## Star History ## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=sqlchat/sqlchat&type=Date)](https://star-history.com/#sqlchat/sqlchat&Date) [![Star History Chart](https://api.star-history.com/svg?repos=sqlchat/sqlchat&type=Date)](https://star-history.com/#sqlchat/sqlchat&Date)
@ -153,3 +117,43 @@ Please make sure you have a stable network connection which can access the OpenA
## License ## License
This project is under the BSL License. See the [LICENSE](LICENSE) file for the full license text. This project is under the BSL License. See the [LICENSE](LICENSE) file for the full license text.
## FAQ
<details><summary>How to self host SQL Chat?</summary>
<p>
- You can deploy SQL Chat to Vercel with one click
<a href="https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fsqlchat%2Fsqlchat&env=OPENAI_API_KEY"><img src="https://img.shields.io/badge/deploy%20on-Vercel-brightgreen.svg?style=for-the-badge&logo=vercel" alt="vercel"></a>
- You can deploy your SQL Chat with docker in seconds
```bash
docker run --name sqlchat --platform linux/amd64 -p 3000:3000 sqlchat/sqlchat
```
</p>
</details>
<details><summary>How to use my OpenAI API key?</summary>
<p>
- You can set the `OPENAI_API_KEY` in environment variable.
```bash
docker run --name sqlchat --platform linux/amd64 --env OPENAI_API_KEY=xxx -p 3000:3000 sqlchat/sqlchat
```
- You can set the `OPENAI_API_KEY` in setting dialog.
</p>
</details>
<details><summary>It always says that I have a network connection issue?</summary>
<p>
Please make sure you have a stable network connection which can access the OpenAI API endpoint. If you cannot access the OpenAI API endpoint, you can try to set the `OPENAI_API_ENDPOINT` in UI or environment variable.
</p>
</details>

View File

@ -0,0 +1,33 @@
/*
Warnings:
- You are about to drop the `Prompt` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "Prompt" DROP CONSTRAINT "Prompt_chatId_fkey";
-- DropTable
DROP TABLE "Prompt";
-- CreateTable
CREATE TABLE "Message" (
"id" TEXT NOT NULL,
"chatId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"role" TEXT NOT NULL,
"content" TEXT NOT NULL,
"upvote" BOOLEAN NOT NULL DEFAULT false,
"downvote" BOOLEAN NOT NULL DEFAULT false,
CONSTRAINT "Message_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "Message_chatId_idx" ON "Message"("chatId");
-- CreateIndex
CREATE INDEX "Message_createdAt_idx" ON "Message"("createdAt");
-- AddForeignKey
ALTER TABLE "Message" ADD CONSTRAINT "Message_chatId_fkey" FOREIGN KEY ("chatId") REFERENCES "Chat"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -14,18 +14,18 @@ model Chat {
id String @id @default(uuid()) id String @id @default(uuid())
createdAt DateTime @default(now()) createdAt DateTime @default(now())
ctx Json ctx Json
prompts Prompt[] messages Message[]
@@index([createdAt]) @@index([createdAt])
} }
model Prompt { model Message {
id String @id @default(uuid()) id String @id @default(uuid())
chat Chat @relation(fields: [chatId], references: [id]) chat Chat @relation(fields: [chatId], references: [id])
chatId String chatId String
createdAt DateTime @default(now()) createdAt DateTime @default(now())
q String role String
a String content String
upvote Boolean @default(false) upvote Boolean @default(false)
downvote Boolean @default(false) downvote Boolean @default(false)

View File

@ -1,70 +1,61 @@
import { PrismaClient, Prisma } from '@prisma/client' import { PrismaClient, Prisma } from "@prisma/client";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
const prisma = new PrismaClient() const prisma = new PrismaClient();
const chatData: Prisma.ChatCreateInput[] = [ const chatData: Prisma.ChatCreateInput[] = [
{ {
id: uuidv4(), id: uuidv4(),
createdAt: new Date(), createdAt: new Date(),
ctx: {}, ctx: {},
prompts: { messages: {
create: [ create: [
{ {
id: uuidv4(), id: uuidv4(),
createdAt: new Date(), createdAt: new Date(),
q: "Hello", role: "system",
a: "What can I help you with today?", content: "You are a bot",
upvote: true, upvote: true,
downvote: false, downvote: false,
}, },
{ {
id: uuidv4(), id: uuidv4(),
createdAt: new Date(), createdAt: new Date(),
q: "How are you?", role: "user",
a: "Fine, thank you, and you?", content: "What can I help you with today?",
upvote: true,
downvote: false,
},
{
id: uuidv4(),
createdAt: new Date(),
role: "assistant",
content: "Hello",
upvote: true, upvote: true,
downvote: false, downvote: false,
}, },
], ],
}, },
}, },
{ ];
id: uuidv4(),
createdAt: new Date(),
ctx: {},
prompts: {
create: [
{
id: uuidv4(),
createdAt: new Date(),
q: "Tell me a joke",
a: "What do you call a fake noodle? An Impasta.",
upvote: true,
downvote: false,
},
],
},
},
]
async function main() { async function main() {
console.log(`Start seeding ...`) console.log(`Start seeding ...`);
for (const c of chatData) { for (const c of chatData) {
const chat = await prisma.chat.create({ const chat = await prisma.chat.create({
data: c, data: c,
}) });
console.log(`Created chat with id: ${chat.id}`) console.log(`Created chat with id: ${chat.id}`);
} }
console.log(`Seeding finished.`) console.log(`Seeding finished.`);
} }
main() main()
.then(async () => { .then(async () => {
await prisma.$disconnect() await prisma.$disconnect();
}) })
.catch(async (e) => { .catch(async (e) => {
console.error(e) console.error(e);
await prisma.$disconnect() await prisma.$disconnect();
process.exit(1) process.exit(1);
}) });

View File

@ -8,7 +8,7 @@ export const config = {
const getApiEndpoint = (apiEndpoint: string) => { const getApiEndpoint = (apiEndpoint: string) => {
const url = new URL(apiEndpoint); const url = new URL(apiEndpoint);
url.pathname = '/v1/chat/completions'; url.pathname = "/v1/chat/completions";
return url; return url;
}; };