Skip to content

Commit

Permalink
feat: ✨ adds similarity and most similar answer to database
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliosheinz committed May 1, 2023
1 parent c027ed8 commit bafa1a0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:
- Added the required column `mostSimilarAnswer` to the `StudySessionAttempt` table without a default value. This is not possible if the table is not empty.
- Added the required column `similarity` to the `StudySessionAttempt` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "StudySessionAttempt" ADD COLUMN "mostSimilarAnswer" TEXT NOT NULL,
ADD COLUMN "similarity" DOUBLE PRECISION NOT NULL;
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ model StudySessionAttempt {
id String @id @default(cuid())
answer String
isRight Boolean
similarity Float
mostSimilarAnswer String
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
studySessionBoxCardId String
Expand Down
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Visibility } from '@prisma/client'
import type { Option } from '~/components/radio-group'

export const MAX_VALID_ANSWERS_PER_CARD = 5
export const MINIMUM_ACCEPTED_SIMILARITY = 0.95
export const MINIMUM_ACCEPTED_SIMILARITY = 0.9
export const MAX_TOPICS_PER_DECK_AND_USER = 5
export const ITEMS_PER_PAGE = 30

Expand Down
14 changes: 7 additions & 7 deletions src/server/api/routers/study-session/study-session.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,16 @@ export const studySessionRouter = createTRPCRouter({
return { isRight: false, answer: card.validAnswers.join('; ') }
}

const isAnswerRight = await verifyIfAnswerIsRight(
answer,
card.validAnswers,
)
const { isRight, highestSimilarity, mostSimilarAnswer } =
await verifyIfAnswerIsRight(answer, card.validAnswers)

let updateBoxCard
const addNewAttempt = ctx.prisma.studySessionAttempt.create({
data: {
answer,
isRight: isAnswerRight,
isRight,
mostSimilarAnswer,
similarity: highestSimilarity,
studySessionBoxCardId: boxCardId,
},
})
Expand All @@ -333,7 +333,7 @@ export const studySessionRouter = createTRPCRouter({

const isInTheLastBox = currentBoxIdx === lastBoxIdx

if (isAnswerRight && !isInTheLastBox) {
if (isRight && !isInTheLastBox) {
const nextStudySessionBox = studySessionBoxes[currentBoxIdx + 1]

if (!nextStudySessionBox) {
Expand All @@ -356,7 +356,7 @@ export const studySessionRouter = createTRPCRouter({

await Promise.all([addNewAttempt, updateBoxCard])

return { isRight: isAnswerRight, answer: card.validAnswers.join('; ') }
return { isRight, answer: mostSimilarAnswer }
}),
finishReviewSession: protectedProcedure
.input(
Expand Down
12 changes: 10 additions & 2 deletions src/utils/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,22 @@ export async function verifyIfAnswerIsRight(
throw new Error('Could not calculate similarity. No results where found.')
}

const isRight = validAnswersResults.some(validAnswerResult => {
let highestSimilarity = 0
let mostSimilarAnswer = ''

const isRight = validAnswersResults.some((validAnswerResult, index) => {
const similarity = calculateSimilarity(
actualAnswerResult,
validAnswerResult || [],
)

if (similarity > highestSimilarity) {
highestSimilarity = similarity
mostSimilarAnswer = validAnswers[index]!
}

return similarity > MINIMUM_ACCEPTED_SIMILARITY
})

return isRight
return { isRight, highestSimilarity, mostSimilarAnswer }
}

0 comments on commit bafa1a0

Please sign in to comment.