从零搭建基于 AI 问答平台(1) 搭建框架及数据库

项目介绍

本次计划搭建一个基于AI的问答平台,平台可以根据主题,自动生成问题和待选择的回答。项目会用到NextJS,TailwindCSS,shadcn/ui,Prisma,OpenAI,Next Auth 等技术,根据搭建步骤会一一介绍。

创建NextJs项目

1
bunx create-next-app@latest --ts

通过命令,创建 NextJs 项目 quizdemo

安装shandcn/ui

shandcn/ui 是一个强大的ui组件库,可以按需安装组件。
通过下面的命令,安装shandcn/ui 和 Button 组件:

1
2
bunx --bun shadcn@latest add button
bunx --bun shadcn@latest init

修改 src/app/page.tsx:

1
2
3
4
5
6
7
import { Button } from "@/components/ui/button";

export default function Home() {
return (
<Button>Click me</Button>
);
}

运行项目: bun run dev:

可以看到按钮,安装成功。

安装prisma

Prisma 是一个基于Nodejs 和 TypeScript 的 ORM,它可以帮助开发者以更快的开发速度和更少的错误来管理数据库。

安装:

命令行:

1
bun add @prisma/client

初始化

命令行:

1
bunx prisma init

添加连接代码

添加 src/lib/db.ts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { PrismaClient } from "@prisma/client";
import "server-only";

declare global {
// eslint-disable-next-line no-var, no-unused-vars
var cachedPrisma: PrismaClient;
}

export let prisma: PrismaClient;
if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient();
} else {
if (!global.cachedPrisma) {
global.cachedPrisma = new PrismaClient();
}
prisma = global.cachedPrisma;
}

添加Schema

修改 prisma/schema.prisma:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}

model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}

model Session {
id String @id @default(cuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
}

创建数据库表

命令行:

1
bunx prisma db push

作者:Bearalise
出处:从零搭建基于 AI 问答平台(1) 搭建框架及数据库
版权:本文版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明,必须在文章中给出原文链接。

请我喝杯咖啡吧~

支付宝
微信