forked from krishnathakkar29/Quizzzy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quiz page and ai questions generation route and game and questions mo…
…del prisma
- Loading branch information
1 parent
dec48d6
commit 63c21dd
Showing
8 changed files
with
532 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
prisma/migrations/20240714074128_game_question_model/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
-- CreateEnum | ||
CREATE TYPE "GameType" AS ENUM ('mcq', 'open_ended'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Game" ( | ||
"id" TEXT NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
"timeStarted" TIMESTAMP(3) NOT NULL, | ||
"timeEnded" TIMESTAMP(3), | ||
"topic" TEXT NOT NULL, | ||
"gameType" "GameType" NOT NULL, | ||
|
||
CONSTRAINT "Game_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Questions" ( | ||
"id" TEXT NOT NULL, | ||
"question" TEXT NOT NULL, | ||
"answer" TEXT NOT NULL, | ||
"gameId" TEXT NOT NULL, | ||
"options" JSONB, | ||
"percentageCorrect" DOUBLE PRECISION, | ||
"isCorrect" BOOLEAN, | ||
"questionType" "GameType" NOT NULL, | ||
"userAnswer" TEXT, | ||
|
||
CONSTRAINT "Questions_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "userId" ON "Game"("userId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "gameId" ON "Questions"("gameId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Game" ADD CONSTRAINT "Game_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Questions" ADD CONSTRAINT "Questions_gameId_fkey" FOREIGN KEY ("gameId") REFERENCES "Game"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { auth } from "@/auth"; | ||
import { quizCreationSchema } from "@/schemas/form/quiz"; | ||
import { NextResponse } from "next/server"; | ||
import { ZodError } from "zod"; | ||
const { | ||
GoogleGenerativeAI, | ||
HarmCategory, | ||
HarmBlockThreshold, | ||
} = require("@google/generative-ai"); | ||
|
||
const apiKey = process.env.GEMINI_API_KEY; | ||
const genAI = new GoogleGenerativeAI(apiKey); | ||
const model = genAI.getGenerativeModel({ | ||
model: "gemini-1.5-flash", | ||
}); | ||
const generationConfig = { | ||
temperature: 1, | ||
topP: 0.95, | ||
topK: 64, | ||
maxOutputTokens: 8192, | ||
responseMimeType: "text/plain", | ||
}; | ||
export async function POST(req: Request, res: Response) { | ||
const session = await auth(); | ||
|
||
const body = await req.json(); | ||
try { | ||
const { amount, topic } = quizCreationSchema.parse(body); | ||
const chatSession = model.startChat({ | ||
generationConfig, | ||
// history: [ | ||
// { | ||
// role: "user", | ||
// parts: [ | ||
// { | ||
// text: `You are to generate a random hard mcq question about ${topic} , ${amount} times`, | ||
// }, | ||
// ], | ||
// }, | ||
// { | ||
// role: "system", | ||
// parts: [ | ||
// { | ||
// text: | ||
// "You are a helpful AI that is able to generate mcq questions and answers, the length of each answer should not be more than 15 words, store all answers and questions and options in a JSON array" + | ||
// ` | ||
// if is it asked to generate question 3 times then there should be an array of objects where each object is of the following output format - | ||
|
||
// { | ||
// question: "question", | ||
// answer: "answer with max length of 15 words", | ||
// option1: "option1 with max length of 15 words", | ||
// option2: "option2 with max length of 15 words", | ||
// option3: "option3 with max length of 15 words", | ||
// } | ||
// `, | ||
// }, | ||
// ], | ||
// }, | ||
// ], | ||
history: [], | ||
}); | ||
const prompt = ` | ||
You are a helpful AI that is able to generate mcq questions and answers, the length of each answer should not be more than 15 words, store all answers and questions and options in a JSON array, | ||
You are to generate a random hard mcq question about ${topic} , ${amount} times, | ||
if is it asked to generate question 3 times then there should be an array of objects where each object should be of the object structure give below and strictly give a response which when passed under JSON.parse() , should not give any error:- | ||
{ | ||
question: "question", | ||
answer: "answer with max length of 15 words", | ||
option1: "option1 with max length of 15 words", | ||
option2: "option2 with max length of 15 words", | ||
option3: "option3 with max length of 15 words", | ||
} | ||
Make sure to properly escape any characters that could invalidate the JSON format, such as quotes inside strings, and errors are like these SyntaxError: Expected ',' or '}' after property value in JSON do not occur. | ||
`; | ||
const result = await chatSession.sendMessage(prompt); | ||
let resi = result.response.text().replace(/'/g, '"'); | ||
resi = resi.replace(/(\w)"(\w)/g, "$1'$2"); | ||
|
||
resi = resi | ||
.replace(/```json/g, "") | ||
.replace(/```/g, "") | ||
.trim(); | ||
|
||
// console.log("naya reiessssss\n", resi); | ||
|
||
let parsedResponse; | ||
try { | ||
parsedResponse = JSON.parse(resi); | ||
console.log("yeh hai json wala\n", parsedResponse); | ||
} catch (parseError) { | ||
console.error("JSON parse error:", parseError); | ||
return NextResponse.json( | ||
{ error: "An error occurred while parsing the AI response." }, | ||
{ | ||
status: 500, | ||
} | ||
); | ||
} | ||
|
||
return NextResponse.json( | ||
{ | ||
questions: parsedResponse, | ||
}, | ||
{ | ||
status: 200, | ||
} | ||
); | ||
} catch (error) { | ||
if (error instanceof ZodError) { | ||
return NextResponse.json( | ||
{ error: error.issues }, | ||
{ | ||
status: 400, | ||
} | ||
); | ||
} else { | ||
console.error("elle gpt error", error); | ||
return NextResponse.json( | ||
{ error: "An unexpected error occurred." }, | ||
{ | ||
status: 500, | ||
} | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.