AI 驱动的测验生成系统,基于 OpenAI Function Calling 实现结构化输出。 支持:从文本提取知识点、生成单选题/填空题、角色人设注入、AI评语生成。 触发场景:(1) 构建测验/考试系统 (2) 知识点自动提取 (3) 个性化导师出题 (4) 学习内容转题库 关键词:quiz, test, exam, assessment, knowledge extraction, MCQ, 出题, 测验
基于 OpenAI Function Calling 的结构化测验生成系统,用于 Next.js 应用。
User Text → Extract Knowledge → Generate Quiz → User Answers → Generate Summary
↓ ↓ ↓
Function Calling Function Calling Plain Chat
{ knowledge_points } { questions } { summary }
OPENAI_LLM_ENDPOINT=https://api.openai.com/v1
OPENAI_LLM_API_KEY=sk-xxx
OPENAI_LLM_MODEL=gpt-4o-mini
npm install openai
lib/
├── llm/ → See [references/llm-client.md](references/llm-client.md)
└── quiz/
├── types.ts → See [references/types.md](references/types.md)
└── tools/ → See [references/function-tools.md](references/function-tools.md)
app/api/quiz/
├── extract-knowledge/
├── generate-quiz/
└── generate-summary/ → See [references/api-routes.md](references/api-routes.md)
const response = await fetch("/api/quiz/extract-knowledge", {
method: "POST",
body: JSON.stringify({ content: userText }),
});
const { knowledgePoints } = await response.json();
// → [{ id, title, content }, ...]
const response = await fetch("/api/quiz/generate-quiz", {
method: "POST",
body: JSON.stringify({
points: knowledgePoints,
character: { name: "孔子", persona: "因材施教...", avatar: "" },
questionCount: 10,
quizType: "mixed",
}),
});
const { questions } = await response.json();
// → [{ id, type, question, options?, answer, explanation }, ...]
const response = await fetch("/api/quiz/generate-summary", {
method: "POST",
body: JSON.stringify({
questions,
userAnswers: { "q-1": "A", "q-2": "答案文本" },
character,
}),
});
const { summary } = await response.json();
// → Markdown 格式的 AI 评语
// Always use tool_choice to force specific function call