Files
QuizMaster/services/geminiService.ts
T
Philip c8ef03a27b feat: Update dependencies and import maps
Upgrades React to v19.2.4 and adjusts ESM import maps to ensure correct versioning and loading. Also, removes redundant API key check in geminiService.ts as it's handled at build time.
2026-01-28 17:45:32 -08:00

47 lines
1.5 KiB
TypeScript

import { GoogleGenAI, Type } from "@google/genai";
import { Question } from "../types";
const generateQuestions = async (topic: string, count: number = 5): Promise<Question[]> => {
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
try {
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
contents: `Generate ${count} trivia questions about "${topic}". The questions should be suitable for a pub quiz.`,
config: {
responseMimeType: "application/json",
responseSchema: {
type: Type.ARRAY,
items: {
type: Type.OBJECT,
properties: {
text: { type: Type.STRING, description: "The question text" },
answer: { type: Type.STRING, description: "The correct answer" },
points: { type: Type.INTEGER, description: "Points value, usually 10, 20, or 30" },
category: { type: Type.STRING, description: "Short category name" }
},
required: ["text", "answer", "points", "category"]
}
}
}
});
if (response.text) {
const rawData = JSON.parse(response.text);
// Map to our internal ID structure
return rawData.map((q: any) => ({
id: crypto.randomUUID(),
text: q.text,
answer: q.answer,
points: q.points,
category: q.category
}));
}
return [];
} catch (error) {
console.error("Gemini generation error:", error);
return [];
}
};
export { generateQuestions };