Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ai prompt character limit; no unnecessary words in answers #285

Merged
merged 3 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/expo/src/screens/create-question/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ export const CreateQuestionScreen: FC = () => {
placeholderTextColor={"#757575"}
onFocus={handleTextInputFocus}
multiline
maxLength={300}
maxLength={200}
/>
</>
</View>
Expand Down Expand Up @@ -883,7 +883,7 @@ export const CreateQuestionScreen: FC = () => {
</Text>
<TextInput
multiline={true}
maxLength={150}
maxLength={65}
className={`mx-5 mt-5 h-[50%] flex-col items-center justify-center rounded-2xl ${selectedChoice?.styles} p-2 text-center text-lg font-bold leading-[28.80px] text-white`}
selectionColor="white"
value={selectedChoice?.text}
Expand All @@ -894,9 +894,9 @@ export const CreateQuestionScreen: FC = () => {
placeholderTextColor="#FFFFFF"
/>
{selectedChoice?.text &&
selectedChoice?.text.length >= 150 ? (
selectedChoice?.text.length >= 65 ? (
<Text className="mt-2 text-center text-red-500 ">
You've reached the maximum of 150 characters.
You've reached the maximum of 65 characters.
</Text>
) : null}

Expand Down
7 changes: 5 additions & 2 deletions apps/expo/src/screens/play-test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ export const PlayTestScreen: FC<RootStackScreenProps<"PlayTest">> = ({
}

const isCorrectAnswer = question.choices.some(
(choice) => choice.text === answer,
(choice) =>
choice.text.trim().toLowerCase() === answer.trim().toLowerCase(),
);

if (question.type === "identification") {
Expand Down Expand Up @@ -536,8 +537,10 @@ export const PlayTestScreen: FC<RootStackScreenProps<"PlayTest">> = ({

if (question.type === "identification") {
const isCorrectAnswer = question.choices.some(
(choice) => choice.text === answer,
(choice) =>
choice.text.trim().toLowerCase() === answer.trim().toLowerCase(),
);

if (isCorrectAnswer) {
const elapsedTime =
countdownTimerRef.current?.elapsedTime ?? question.time;
Expand Down
21 changes: 12 additions & 9 deletions packages/api/src/functions/gptHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ Points: [Select from the following options based on how difficult you think the
(points) => points.title,
).join(", ")}]`;

export const generateChoicesPrompt = (numChoices: number) => {
export const generateChoicesPrompt = (
numChoices: number,
maxCharsForChoice = 50,
) => {
let choicesPrompt = "";
for (let i = 1; i <= numChoices; i++) {
choicesPrompt += `Option ${i}: [Choice ${i}]\n`;
choicesPrompt += `Option ${i}: [Choice ${i}, must not exceed ${maxCharsForChoice} characters]\n`;
}
return choicesPrompt.trim();
};
Expand All @@ -60,25 +63,25 @@ export const promptGenerators: {
message,
numChoices = 4,
maxCharsForQuestion = 100,
maxCharsForChoice = 68,
maxCharsForChoice = 50,
) =>
`Create a multiple choice question (maximum of ${maxCharsForQuestion} characters) about: "${message}" with ${numChoices} choices. Each choice must not exceed ${maxCharsForChoice} characters and there must be only 1 correct answer. Format as:
Question: [Your question here]
${generateChoicesPrompt(numChoices)}
${generateChoicesPrompt(numChoices, maxCharsForChoice)}
Correct Answer: Option [Correct option number]
${timeAndPointsPrompt}`,

identification: (
message,
maxCharsForQuestion = 100,
maxCharsForChoice = 68,
maxCharsForChoice = 50,
) =>
`Create an identification question (maximum of ${maxCharsForQuestion} characters) based on: "${message}". The answer must not exceed ${maxCharsForChoice} characters. Format as:
`Create an identification question (maximum of ${maxCharsForQuestion} characters) based on: "${message}". The answer should be concise and straight to the point and must not include unnesessary words or phrases, and must not exceed ${maxCharsForChoice} characters. Format as:
Question: [Your question here]
Answer: [Your answer here]
${timeAndPointsPrompt}`,

trueOrFalse: (message, maxCharsForQuestion = 100, maxCharsForChoice = 68) =>
trueOrFalse: (message, maxCharsForQuestion = 100, maxCharsForChoice = 50) =>
`Based on the information "${message}", generate a true or false question (maximum of ${maxCharsForQuestion} characters). The answer must not exceed ${maxCharsForChoice} characters. Format as:
Question: [Your question here]
Answer: [True/False]
Expand All @@ -88,11 +91,11 @@ ${timeAndPointsPrompt}`,
message,
numChoices = 4,
maxCharsForQuestion = 100,
maxCharsForChoice = 68,
maxCharsForChoice = 50,
) =>
`Create a multiselect question (maximum of ${maxCharsForQuestion} characters) about: "${message}" with ${numChoices} choices. The choices must not exceed ${maxCharsForChoice} characters and there must be atleast 1 correct answer. Multiple answers can be correct. Format as:
Question: [Your question here]
${generateChoicesPrompt(numChoices)}
${generateChoicesPrompt(numChoices, maxCharsForChoice)}
All Correct Answers: Options [Correct option numbers separated by commas, e.g., 1,3]
${timeAndPointsPrompt}`,
};
Expand Down
28 changes: 15 additions & 13 deletions packages/api/src/functions/randomQuestionsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,25 @@ export const questionFormatGenerators: {
multipleChoice: (
numChoices = 4,
maxCharsForQuestion = 100,
maxCharsForChoice = 68,
) => `separator\nQuestion: [Your question here, max ${maxCharsForQuestion} characters, and each choice below must not exceed ${maxCharsForChoice} characters]
${generateChoicesPrompt(
numChoices,
)}\nCorrect Answer: Option [Correct option number and only 1 correct answer] ${timeAndPointsPrompt}`,
maxCharsForChoice = 50,
) => `separator\nQuestion: [Your question here, max ${maxCharsForQuestion} characters]
${generateChoicesPrompt(
numChoices,
maxCharsForChoice,
)}\nCorrect Answer: Option [Correct option number and only 1 correct answer] ${timeAndPointsPrompt}`,

multiselect: (
numChoices = 4,
maxCharsForQuestion = 100,
maxCharsForChoice = 68,
) => `separator\nQuestion: [Your question here, max ${maxCharsForQuestion} characters, and each choice below must not exceed ${maxCharsForChoice} characters]
${generateChoicesPrompt(
numChoices,
)}\nAll Correct Answers: Options [Correct option numbers separated by commas (e.g., 1,3) and at least one correct answer] ${timeAndPointsPrompt}`,

identification: (maxCharsForQuestion = 100, maxCharsForChoice = 68) =>
`separator\nQuestion: [Your question here, max ${maxCharsForQuestion} characters]\nAnswer: [Your answer here, max ${maxCharsForChoice} characters] ${timeAndPointsPrompt}`,
maxCharsForChoice = 50,
) => `separator\nQuestion: [Your question here, max ${maxCharsForQuestion} characters]
${generateChoicesPrompt(
numChoices,
maxCharsForChoice,
)}\nAll Correct Answers: Options [Correct option numbers separated by commas (e.g., 1,3) and at least one correct answer] ${timeAndPointsPrompt}`,

identification: (maxCharsForQuestion = 100, maxCharsForChoice = 50) =>
`separator\nQuestion: [Your question here, max ${maxCharsForQuestion} characters]\nAnswer: [Your answer here, max ${maxCharsForChoice} characters, and the answer should be concise and straight to the point and must not include unnesessary words or phrases] ${timeAndPointsPrompt}`,

trueOrFalse: (maxCharsForQuestion = 100) =>
`separator\nQuestion: [Your question here, max ${maxCharsForQuestion} characters]\nAnswer: [True/False] ${timeAndPointsPrompt}`,
Expand Down
32 changes: 16 additions & 16 deletions packages/api/src/functions/tests/generatePromptForType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ describe("generatePromptForType", () => {
it("should generate prompt for multipleChoice with default number of choices", () => {
const result = generatePromptForType(message, "multipleChoice");
expect(result).toEqual(
`Create a multiple choice question (maximum of 100 characters) about: "${message}" with 4 choices. Each choice must not exceed 68 characters and there must be only 1 correct answer. Format as:
`Create a multiple choice question (maximum of 100 characters) about: "${message}" with 4 choices. Each choice must not exceed 50 characters and there must be only 1 correct answer. Format as:
Question: [Your question here]
Option 1: [Choice 1]
Option 2: [Choice 2]
Option 3: [Choice 3]
Option 4: [Choice 4]
Option 1: [Choice 1, must not exceed 50 characters]
Option 2: [Choice 2, must not exceed 50 characters]
Option 3: [Choice 3, must not exceed 50 characters]
Option 4: [Choice 4, must not exceed 50 characters]
Correct Answer: Option [Correct option number]
${timeAndPointsPrompt}`,
);
Expand All @@ -22,7 +22,7 @@ ${timeAndPointsPrompt}`,
it("should generate prompt for identification", () => {
const result = generatePromptForType(message, "identification");
expect(result).toEqual(
`Create an identification question (maximum of 100 characters) based on: "${message}". The answer must not exceed 68 characters. Format as:
`Create an identification question (maximum of 100 characters) based on: "${message}". The answer should be concise and straight to the point and must not include unnesessary words or phrases, and must not exceed 50 characters. Format as:
Question: [Your question here]
Answer: [Your answer here]
${timeAndPointsPrompt}`,
Expand All @@ -32,7 +32,7 @@ ${timeAndPointsPrompt}`,
it("should generate prompt for trueOrFalse", () => {
const result = generatePromptForType(message, "trueOrFalse");
expect(result).toEqual(
`Based on the information "${message}", generate a true or false question (maximum of 100 characters). The answer must not exceed 68 characters. Format as:
`Based on the information "${message}", generate a true or false question (maximum of 100 characters). The answer must not exceed 50 characters. Format as:
Question: [Your question here]
Answer: [True/False]
${timeAndPointsPrompt}`,
Expand All @@ -42,12 +42,12 @@ ${timeAndPointsPrompt}`,
it("should generate prompt for multiselect with default number of choices", () => {
const result = generatePromptForType(message, "multiselect");
expect(result).toEqual(
`Create a multiselect question (maximum of 100 characters) about: "${message}" with 4 choices. The choices must not exceed 68 characters and there must be atleast 1 correct answer. Multiple answers can be correct. Format as:
`Create a multiselect question (maximum of 100 characters) about: "${message}" with 4 choices. The choices must not exceed 50 characters and there must be atleast 1 correct answer. Multiple answers can be correct. Format as:
Question: [Your question here]
Option 1: [Choice 1]
Option 2: [Choice 2]
Option 3: [Choice 3]
Option 4: [Choice 4]
Option 1: [Choice 1, must not exceed 50 characters]
Option 2: [Choice 2, must not exceed 50 characters]
Option 3: [Choice 3, must not exceed 50 characters]
Option 4: [Choice 4, must not exceed 50 characters]
All Correct Answers: Options [Correct option numbers separated by commas, e.g., 1,3]
${timeAndPointsPrompt}`,
);
Expand All @@ -64,9 +64,9 @@ ${timeAndPointsPrompt}`,
expect(result).toEqual(
`Create a multiple choice question (maximum of 80 characters) about: "${message}" with 3 choices. Each choice must not exceed 50 characters and there must be only 1 correct answer. Format as:
Question: [Your question here]
Option 1: [Choice 1]
Option 2: [Choice 2]
Option 3: [Choice 3]
Option 1: [Choice 1, must not exceed 50 characters]
Option 2: [Choice 2, must not exceed 50 characters]
Option 3: [Choice 3, must not exceed 50 characters]
Correct Answer: Option [Correct option number]
${timeAndPointsPrompt}`,
);
Expand All @@ -75,7 +75,7 @@ ${timeAndPointsPrompt}`,
it("should generate prompt for identification with custom max characters", () => {
const result = generatePromptForType(message, "identification", 90, 60);
expect(result).toEqual(
`Create an identification question (maximum of 90 characters) based on: "${message}". The answer must not exceed 60 characters. Format as:
`Create an identification question (maximum of 90 characters) based on: "${message}". The answer should be concise and straight to the point and must not include unnesessary words or phrases, and must not exceed 60 characters. Format as:
Question: [Your question here]
Answer: [Your answer here]
${timeAndPointsPrompt}`,
Expand Down
18 changes: 9 additions & 9 deletions packages/api/src/functions/tests/randomQuestionHandlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ import { generateChoicesPrompt, timeAndPointsPrompt } from "../gptHandlers";
describe("questionFormatGenerators", () => {
it("It should generate format for multiple choice question/s", () => {
const mcqFormat = questionFormatGenerators["multipleChoice"];
const output = `separator\nQuestion: [Your question here, max 100 characters, and each choice below must not exceed 68 characters]
${generateChoicesPrompt(
4,
)}\nCorrect Answer: Option [Correct option number and only 1 correct answer] ${timeAndPointsPrompt}`;
const output = `separator\nQuestion: [Your question here, max 100 characters]
${generateChoicesPrompt(
4,
)}\nCorrect Answer: Option [Correct option number and only 1 correct answer] ${timeAndPointsPrompt}`;

expect(mcqFormat()).toEqual(output);
});

it("It should generate format for multiselect question/s", () => {
const multiselectFormat = questionFormatGenerators["multiselect"];

const output = `separator\nQuestion: [Your question here, max 100 characters, and each choice below must not exceed 68 characters]
${generateChoicesPrompt(
4,
)}\nAll Correct Answers: Options [Correct option numbers separated by commas (e.g., 1,3) and at least one correct answer] ${timeAndPointsPrompt}`;
const output = `separator\nQuestion: [Your question here, max 100 characters]
${generateChoicesPrompt(
4,
)}\nAll Correct Answers: Options [Correct option numbers separated by commas (e.g., 1,3) and at least one correct answer] ${timeAndPointsPrompt}`;

expect(multiselectFormat()).toEqual(output);
});

it("It should generate format for identification question/s", () => {
const identicationFormat = questionFormatGenerators["identification"];

const output = `separator\nQuestion: [Your question here, max 100 characters]\nAnswer: [Your answer here, max 68 characters] ${timeAndPointsPrompt}`;
const output = `separator\nQuestion: [Your question here, max 100 characters]\nAnswer: [Your answer here, max 50 characters, and the answer should be concise and straight to the point and must not include unnesessary words or phrases] ${timeAndPointsPrompt}`;

expect(identicationFormat()).toEqual(output);
});
Expand Down
Loading