From 00e3040670dfd127b44ef4dc0486fbfecadc7a5f Mon Sep 17 00:00:00 2001 From: steven Date: Mon, 24 Apr 2023 18:07:13 +0800 Subject: [PATCH] chore: update usage database schema --- README.md | 100 +++++++++--------- .../20230424100325_add_message/migration.sql | 33 ++++++ prisma/schema.prisma | 8 +- prisma/seed.ts | 59 +++++------ src/pages/api/chat.ts | 2 +- 5 files changed, 115 insertions(+), 87 deletions(-) create mode 100644 prisma/migrations/20230424100325_add_message/migration.sql diff --git a/README.md b/README.md index 8a8a7d4..ee8397d 100644 --- a/README.md +++ b/README.md @@ -76,59 +76,23 @@ docker run --name sqlchat --platform linux/amd64 --env OPENAI_API_KEY=xxx --env ### Database Setup -1. Install prisma +1. Install dependencies - ```bash - pnpm install prisma --save-dev - ``` + ```bash + pnpm i + ``` -2. Install prisma client +1. Generate prisma client from the model - ```bash - pnpm install @prisma/client - ``` + ```bash + pnpm prisma generate + ``` -3. Generate prisma client from the model +1. Seed data - ```bash - pnpm prisma generate - ``` - -4. Seed data - - ```bash - pnpm install typescript ts-node @types/node --save-dev - ``` - - ```bash - pnpm prisma db seed - ``` - -## Common Questions - -
How to self host SQL Chat? -

- -- You can deploy SQL Chat to Vercel with one click - - vercel - -- You can deploy your SQL Chat with docker in seconds - - ```bash - docker run --name sqlchat --platform linux/amd64 -p 3000:3000 sqlchat/sqlchat - ``` - -

-
- -
It always says that I have a network connection issue? -

- -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. - -

-
+ ```bash + pnpm prisma db seed + ``` ## Star History @@ -153,3 +117,43 @@ Please make sure you have a stable network connection which can access the OpenA ## License This project is under the BSL License. See the [LICENSE](LICENSE) file for the full license text. + +## FAQ + +
How to self host SQL Chat? +

+ +- You can deploy SQL Chat to Vercel with one click + + vercel + +- You can deploy your SQL Chat with docker in seconds + + ```bash + docker run --name sqlchat --platform linux/amd64 -p 3000:3000 sqlchat/sqlchat + ``` + +

+
+ +
How to use my OpenAI API key? +

+ +- 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. + +

+
+ +
It always says that I have a network connection issue? +

+ +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. + +

+
diff --git a/prisma/migrations/20230424100325_add_message/migration.sql b/prisma/migrations/20230424100325_add_message/migration.sql new file mode 100644 index 0000000..680c11b --- /dev/null +++ b/prisma/migrations/20230424100325_add_message/migration.sql @@ -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; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c21929b..241fdec 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -14,18 +14,18 @@ model Chat { id String @id @default(uuid()) createdAt DateTime @default(now()) ctx Json - prompts Prompt[] + messages Message[] @@index([createdAt]) } -model Prompt { +model Message { id String @id @default(uuid()) chat Chat @relation(fields: [chatId], references: [id]) chatId String createdAt DateTime @default(now()) - q String - a String + role String + content String upvote Boolean @default(false) downvote Boolean @default(false) diff --git a/prisma/seed.ts b/prisma/seed.ts index b0741ac..e01d77b 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1,70 +1,61 @@ -import { PrismaClient, Prisma } from '@prisma/client' +import { PrismaClient, Prisma } from "@prisma/client"; import { v4 as uuidv4 } from "uuid"; -const prisma = new PrismaClient() +const prisma = new PrismaClient(); const chatData: Prisma.ChatCreateInput[] = [ { id: uuidv4(), createdAt: new Date(), ctx: {}, - prompts: { + messages: { create: [ { id: uuidv4(), createdAt: new Date(), - q: "Hello", - a: "What can I help you with today?", + role: "system", + content: "You are a bot", upvote: true, downvote: false, }, { id: uuidv4(), createdAt: new Date(), - q: "How are you?", - a: "Fine, thank you, and you?", + role: "user", + content: "What can I help you with today?", + upvote: true, + downvote: false, + }, + { + id: uuidv4(), + createdAt: new Date(), + role: "assistant", + content: "Hello", upvote: true, 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() { - console.log(`Start seeding ...`) + console.log(`Start seeding ...`); for (const c of chatData) { const chat = await prisma.chat.create({ 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() .then(async () => { - await prisma.$disconnect() + await prisma.$disconnect(); }) .catch(async (e) => { - console.error(e) - await prisma.$disconnect() - process.exit(1) - }) + console.error(e); + await prisma.$disconnect(); + process.exit(1); + }); diff --git a/src/pages/api/chat.ts b/src/pages/api/chat.ts index 106ed98..e7a0ac2 100644 --- a/src/pages/api/chat.ts +++ b/src/pages/api/chat.ts @@ -8,7 +8,7 @@ export const config = { const getApiEndpoint = (apiEndpoint: string) => { const url = new URL(apiEndpoint); - url.pathname = '/v1/chat/completions'; + url.pathname = "/v1/chat/completions"; return url; };