From 12517bb5b484c786fb8f7acfd57aa41bfcbf13d6 Mon Sep 17 00:00:00 2001 From: Emilio Schaedler Heinzmann Date: Sun, 12 Mar 2023 18:05:04 -0300 Subject: [PATCH 01/14] chore: :loud_sound: add logs to improve error tracking --- src/utils/openai/index.ts | 80 +++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/src/utils/openai/index.ts b/src/utils/openai/index.ts index c0c8b4e..d167e3f 100644 --- a/src/utils/openai/index.ts +++ b/src/utils/openai/index.ts @@ -61,41 +61,49 @@ export async function generateFlashCards({ topics, title, }: GenerateFlashCardsParam): Promise> { - const amountOfCards = 3 - const charactersPerSentence = 65 - - /** Build topics strings */ - const joinedTopics = topics.map(({ title }) => title).join(', ') - - /** Build prompt asking OpenAI to generate a csv string */ - const prompt = `Levando em conta o contexto ${title}, gere um Array JSON com ${amountOfCards} perguntas e respostas curtas e diretas, de no máximo ${charactersPerSentence} caracteres, sobre ${joinedTopics}. [{question: "pergunta", answer: "resposta"}, ...]` - - const response = await openai.createChatCompletion( - { - n: 1, - messages: [{ role: 'user', content: prompt }], - temperature: 0.8, - model: 'gpt-3.5-turbo', - max_tokens: amountOfCards * charactersPerSentence, - }, - { timeout: 15_000 }, - ) - - const generatedJsonString = response.data.choices[0]?.message?.content - - if (!generatedJsonString) { - throw new Error('Não foi possível gerar as perguntas e respostas.') + try { + const amountOfCards = 3 + const charactersPerSentence = 65 + + /** Build topics strings */ + const joinedTopics = topics.map(({ title }) => title).join(', ') + + /** Build prompt asking OpenAI to generate a csv string */ + const prompt = `Levando em conta o contexto ${title}, gere um Array JSON com ${amountOfCards} perguntas e respostas curtas e diretas, de no máximo ${charactersPerSentence} caracteres, sobre ${joinedTopics}. [{question: "pergunta", answer: "resposta"}, ...]` + + const response = await openai.createChatCompletion( + { + n: 1, + messages: [{ role: 'user', content: prompt }], + temperature: 0.8, + model: 'gpt-3.5-turbo', + max_tokens: amountOfCards * charactersPerSentence, + }, + { timeout: 15_000 }, + ) + + const generatedJsonString = response.data.choices[0]?.message?.content + + if (!generatedJsonString) { + throw new Error('Não foi possível gerar as perguntas e respostas.') + } + + const generatedJson = JSON.parse(generatedJsonString) + + const cards: Array = generatedJson.map( + ({ question, answer }: { question: string; answer: string }) => ({ + question: trimAndRemoveDoubleQuotes(question), + answer: trimAndRemoveDoubleQuotes(answer), + isAiPowered: true, + }), + ) + + return cards + } catch (error) { + /** + * Added to improve error tracking on log monitoring tools + */ + console.error(error) + throw error } - - const generatedJson = JSON.parse(generatedJsonString) - - const cards: Array = generatedJson.map( - ({ question, answer }: { question: string; answer: string }) => ({ - question: trimAndRemoveDoubleQuotes(question), - answer: trimAndRemoveDoubleQuotes(answer), - isAiPowered: true, - }), - ) - - return cards } From fa239639674a495c15300a316a16d80495b5fd6f Mon Sep 17 00:00:00 2001 From: Emilio Schaedler Heinzmann Date: Mon, 13 Mar 2023 17:36:55 -0300 Subject: [PATCH 02/14] chore: :goal_net: adds additional information to the logs --- src/utils/openai/index.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/utils/openai/index.ts b/src/utils/openai/index.ts index d167e3f..05aeb1f 100644 --- a/src/utils/openai/index.ts +++ b/src/utils/openai/index.ts @@ -61,6 +61,8 @@ export async function generateFlashCards({ topics, title, }: GenerateFlashCardsParam): Promise> { + let generatedJsonString: string | undefined + try { const amountOfCards = 3 const charactersPerSentence = 65 @@ -82,7 +84,7 @@ export async function generateFlashCards({ { timeout: 15_000 }, ) - const generatedJsonString = response.data.choices[0]?.message?.content + generatedJsonString = response.data.choices[0]?.message?.content if (!generatedJsonString) { throw new Error('Não foi possível gerar as perguntas e respostas.') @@ -103,7 +105,7 @@ export async function generateFlashCards({ /** * Added to improve error tracking on log monitoring tools */ - console.error(error) + console.error(error, generatedJsonString) throw error } } From 944aac3b5bc4ac56343f425ee46e1ca5c62b4958 Mon Sep 17 00:00:00 2001 From: Emilio Schaedler Heinzmann Date: Mon, 13 Mar 2023 17:52:52 -0300 Subject: [PATCH 03/14] fix: :bug: fixes prompt ot be more accestive about the quantity --- src/utils/openai/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/openai/index.ts b/src/utils/openai/index.ts index 05aeb1f..34cacf3 100644 --- a/src/utils/openai/index.ts +++ b/src/utils/openai/index.ts @@ -68,10 +68,10 @@ export async function generateFlashCards({ const charactersPerSentence = 65 /** Build topics strings */ - const joinedTopics = topics.map(({ title }) => title).join(', ') + const joinedTopics = topics.map(({ title }) => title).join(' ou ') /** Build prompt asking OpenAI to generate a csv string */ - const prompt = `Levando em conta o contexto ${title}, gere um Array JSON com ${amountOfCards} perguntas e respostas curtas e diretas, de no máximo ${charactersPerSentence} caracteres, sobre ${joinedTopics}. [{question: "pergunta", answer: "resposta"}, ...]` + const prompt = `Levando em conta o contexto ${title}, gere um Array JSON de tamanho ${amountOfCards} com perguntas e respostas curtas e diretas, de no máximo ${charactersPerSentence} caracteres, sobre ${joinedTopics}. [{question: "pergunta", answer: "resposta"}, ...]` const response = await openai.createChatCompletion( { From f3f0805a54594d3cf2324fde3dc34cb9bc706d74 Mon Sep 17 00:00:00 2001 From: Emilio Schaedler Heinzmann Date: Mon, 13 Mar 2023 17:57:38 -0300 Subject: [PATCH 04/14] refactor: :recycle: change open api timeouts --- src/utils/openai/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/openai/index.ts b/src/utils/openai/index.ts index 34cacf3..92e558d 100644 --- a/src/utils/openai/index.ts +++ b/src/utils/openai/index.ts @@ -23,7 +23,7 @@ const createEmbedding = (str: string) => input: str.replace('\n', ' '), }, { - timeout: 15_000, + timeout: 10_000, }, ) @@ -81,7 +81,7 @@ export async function generateFlashCards({ model: 'gpt-3.5-turbo', max_tokens: amountOfCards * charactersPerSentence, }, - { timeout: 15_000 }, + { timeout: 30_000 }, ) generatedJsonString = response.data.choices[0]?.message?.content From 75a930a2f8629316f24de1d1cb042f4afeab544b Mon Sep 17 00:00:00 2001 From: Emilio Schaedler Heinzmann Date: Fri, 17 Mar 2023 10:44:57 -0300 Subject: [PATCH 05/14] refactor: :recycle: reduces open ai api timeout --- src/utils/openai/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/openai/index.ts b/src/utils/openai/index.ts index 92e558d..aee6722 100644 --- a/src/utils/openai/index.ts +++ b/src/utils/openai/index.ts @@ -81,7 +81,7 @@ export async function generateFlashCards({ model: 'gpt-3.5-turbo', max_tokens: amountOfCards * charactersPerSentence, }, - { timeout: 30_000 }, + { timeout: 10_000 }, ) generatedJsonString = response.data.choices[0]?.message?.content From 9fcb6b3cdfa02338babb3437405c1d4951697cfb Mon Sep 17 00:00:00 2001 From: Emilio Schaedler Heinzmann Date: Fri, 17 Mar 2023 11:52:34 -0300 Subject: [PATCH 06/14] refactor: :recycle: increases ope api call timeout --- src/utils/openai/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/openai/index.ts b/src/utils/openai/index.ts index aee6722..92e558d 100644 --- a/src/utils/openai/index.ts +++ b/src/utils/openai/index.ts @@ -81,7 +81,7 @@ export async function generateFlashCards({ model: 'gpt-3.5-turbo', max_tokens: amountOfCards * charactersPerSentence, }, - { timeout: 10_000 }, + { timeout: 30_000 }, ) generatedJsonString = response.data.choices[0]?.message?.content From 84eaa268c5660740b6e79b5a6afa8ef96318b075 Mon Sep 17 00:00:00 2001 From: Emilio Schaedler Heinzmann Date: Fri, 17 Mar 2023 15:41:25 -0300 Subject: [PATCH 07/14] feat: :sparkles: adds possiblity of having multiple right answers for a single card --- .../migration.sql | 9 +++++ prisma/schema.prisma | 2 +- .../new-card/new-card-modal.component.tsx | 5 +-- .../text-area/text-area.component.tsx | 26 +++++++++++---- src/components/text-area/text-area.types.ts | 1 + src/constants/index.ts | 1 + .../create-new-deck.context.tsx | 5 ++- .../create/components/cards.component.tsx | 4 +-- src/server/api/routers/decks/decks.router.ts | 13 ++++++-- .../study-session/study-session.router.ts | 14 ++++---- src/utils/openai/index.ts | 33 +++++++++++++------ src/utils/validators/card.ts | 15 +++++++-- 12 files changed, 95 insertions(+), 33 deletions(-) create mode 100644 prisma/migrations/20230317171159_adds_possiblity_of_multiple_answers_to_one_card/migration.sql diff --git a/prisma/migrations/20230317171159_adds_possiblity_of_multiple_answers_to_one_card/migration.sql b/prisma/migrations/20230317171159_adds_possiblity_of_multiple_answers_to_one_card/migration.sql new file mode 100644 index 0000000..7189663 --- /dev/null +++ b/prisma/migrations/20230317171159_adds_possiblity_of_multiple_answers_to_one_card/migration.sql @@ -0,0 +1,9 @@ +/* + Warnings: + + - You are about to drop the column `answer` on the `Card` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "Card" DROP COLUMN "answer", +ADD COLUMN "validAnswers" TEXT[]; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index da1da52..0ea0ac9 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -90,7 +90,7 @@ model Topic { model Card { id String @id @default(cuid()) question String - answer String + validAnswers String[] deckId String deck Deck @relation(fields: [deckId], references: [id], onDelete: Cascade) createdAt DateTime @default(now()) diff --git a/src/components/modal/new-card/new-card-modal.component.tsx b/src/components/modal/new-card/new-card-modal.component.tsx index fa0a82d..e5a137a 100644 --- a/src/components/modal/new-card/new-card-modal.component.tsx +++ b/src/components/modal/new-card/new-card-modal.component.tsx @@ -52,8 +52,9 @@ export function NewCardModal(props: NewCardModalProps) {