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

100
README.md
View File

@ -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
<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>
```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
<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())
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)

View File

@ -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);
});

View File

@ -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;
};