Skip to content

Commit

Permalink
feat(ai-quiz): generate-quizzesを共通化
Browse files Browse the repository at this point in the history
  • Loading branch information
nakasyou committed Aug 20, 2024
1 parent 59d39b4 commit 7b80923
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 34 deletions.
41 changes: 7 additions & 34 deletions src/islands/ai-quiz/components/Quiz.qwik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import { safeParse } from 'valibot'
import { Loading } from './Utils.qwik'
import { QuizDB } from '../storage'
import { quizzesGenerator } from '../utils/generate-quizzes'

export const QuizScreen = component$(() => {
const quizState = useStore<QuizState>(
Expand Down Expand Up @@ -180,20 +181,10 @@ export const QuizScreen = component$(() => {
) {
return
}
const gemini = getGoogleGenerativeAI()
if (!gemini) {
const generateQuizzes = quizzesGenerator()
if (!generateQuizzes) {
return alert('AIエラー')
}
const model = gemini.getGenerativeModel({
model: 'gemini-1.5-flash',
generationConfig: {
responseMimeType: 'application/json',
},
systemInstruction: {
role: 'system',
parts: [{ text: PROMPT_TO_GENERATE_SELECT_QUIZ }],
},
})

const sourceNotes = screenState.note!.notes.filter(
(note) => note.type === 'text',
Expand All @@ -210,28 +201,9 @@ export const QuizScreen = component$(() => {
const randomNote =
sourceNotes[Math.floor(Math.random() * sourceNotes.length)]!

const res = await model
.startChat()
.sendMessage(randomNote?.canToJsonData.html || '')

let contents: unknown
try {
contents = JSON.parse(res.response.text())
} catch {
// Unable to parse
continue
}

if (!Array.isArray(contents)) {
continue
}
const quizzes = await Promise.all(
contents
.filter(
(content): content is QuizContent =>
safeParse(CONTENT_SCHEMA, content).success,
)
.map(async (content): Promise<Quiz> => {
(await generateQuizzes(randomNote)).map(
async (content): Promise<Quiz> => {
const res = await quizDB.quizzesByNote.add({
noteId: randomNote.id,
quiz: {
Expand All @@ -254,7 +226,8 @@ export const QuizScreen = component$(() => {
source: randomNote,
id: res,
}
}),
},
),
)
const addingQuizzes: typeof quizState.quizzes = []
for (const quiz of quizzes) {
Expand Down
54 changes: 54 additions & 0 deletions src/islands/ai-quiz/utils/generate-quizzes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { safeParse } from 'valibot'
import type { TextNoteData } from '../../note/components/notes/TextNote/types'
import { getGoogleGenerativeAI } from '../../shared/gemini'
import {
CONTENT_SCHEMA,
PROMPT_TO_GENERATE_SELECT_QUIZ,
type QuizContent,
} from '../constants'

export const quizzesGenerator = () => {
const gemini = getGoogleGenerativeAI()
if (!gemini) {
return null
}

const model = gemini.getGenerativeModel({
model: 'gemini-1.5-flash',
generationConfig: {
responseMimeType: 'application/json',
},
systemInstruction: {
role: 'system',
parts: [{ text: PROMPT_TO_GENERATE_SELECT_QUIZ }],
},
})

return async function generatedQuizzes(
note: TextNoteData,
): Promise<QuizContent[]> {
const res = await model
.startChat()
.sendMessage(note?.canToJsonData.html || '')

let contents: unknown
try {
contents = JSON.parse(res.response.text())
} catch {
// Unable to parse
return []
}

if (!Array.isArray(contents)) {
return []
}

const quizzes = await Promise.all(
contents.filter(
(content): content is QuizContent =>
safeParse(CONTENT_SCHEMA, content).success,
),
)
return quizzes
}
}

0 comments on commit 7b80923

Please sign in to comment.