-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquizHandler.ts
More file actions
59 lines (50 loc) · 1.73 KB
/
Copy pathquizHandler.ts
File metadata and controls
59 lines (50 loc) · 1.73 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Request, Response } from "express";
import { OpenAI } from "langchain/llms/openai";
import { PineconeVectorStore } from "langchain/vectorstores/pinecone";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { Pinecone } from "@pinecone-database/pinecone";
const pinecone = new Pinecone({
apiKey: process.env.PINECONE_API_KEY!,
});
const embeddings = new OpenAIEmbeddings({
openAIApiKey: process.env.OPENAI_API_KEY!,
});
const llm = new OpenAI({
openAIApiKey: process.env.OPENAI_API_KEY!,
modelName: "gpt-3.5-turbo",
});
export async function quizHandler(req: Request, res: Response) {
try {
const index = pinecone.Index(process.env.PINECONE_INDEX_NAME!);
const vectorStore = new PineconeVectorStore(embeddings, { pineconeIndex: index });
// Get relevant documents from the past week
const retriever = vectorStore.asRetriever({ k: 10 });
const documents = await retriever.getRelevantDocuments(
"Generate quiz questions based on this content"
);
// Generate quiz from documents
const quizPrompt = `Based on these documents, generate a JSON object with 5 multiple choice quiz questions:
${documents.map((d) => d.pageContent).join("\n\n")}
Return a JSON object with this structure:
{
"questions": [
{
"question": "...",
"options": ["a", "b", "c", "d"],
"correctAnswer": 0,
"explanation": "..."
}
]
}`;
const quizResponse = await llm.call(quizPrompt);
const quiz = JSON.parse(quizResponse);
res.json({
quiz,
generatedAt: new Date().toISOString(),
documentCount: documents.length,
});
} catch (error) {
console.error("Quiz generation error:", error);
res.status(500).json({ error: "Quiz generation failed" });
}
}