Skip to content

Commit

Permalink
Merge pull request #120 from RickCarlino/prod
Browse files Browse the repository at this point in the history
Minor Updates to Jitter scheduling and grammar checker.
  • Loading branch information
RickCarlino authored Jan 25, 2025
2 parents 240a25f + 487847d commit e54a7d0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 65 deletions.
53 changes: 2 additions & 51 deletions koala/grammar-ng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,61 +159,13 @@ async function runChecks(props: GrammarCorrectionProps): Promise<Explanation> {
return gradeResponse;
}

async function runChecksO1(
props: GrammarCorrectionProps,
): Promise<Explanation> {
const { userInput, langCode } = props;
const messages = createMessages(langCode, props.definition, userInput);
const rawResponse = await openai.beta.chat.completions.parse({
messages,
model: "o1-mini",
});

const respText = rawResponse.choices[0]?.message?.content;

if (!respText) {
throw new Error("Invalid response format from OpenAI.");
}

const jsonResponse = await openai.beta.chat.completions.parse({
messages: [
{ role: "user", content: respText },
{
role: "system",
content: [
"You are a JSON parser. Convert the input to JSON format.",
].join("\n"),
},
],
model: "gpt-4o-mini",
max_tokens: 125,
temperature: 0.1,
response_format: zodResponseFormat(zodGradeResponse, "grade_response"),
});

// This is the LLM's structured response (or an error if parsing failed)
const gradeResponse = jsonResponse.choices[0]?.message?.parsed;
if (!gradeResponse) {
throw new Error("Invalid response format from OpenAI.");
}

if (compare(userInput, gradeResponse.correctedSentence || "", 0)) {
gradeResponse.grade = "ok";
}

// Store the final data (with "ok" = correct, others = incorrect)
await storeTrainingData(props, gradeResponse);
return gradeResponse;
}

export const grammarCorrectionNG: QuizEvaluator = async ({
userInput,
card,
}) => {
const check = async (): ReturnType<QuizEvaluator> => {
// Not impressed with GPT-O1-Mini.
const fn = Math.random() > 1 ? runChecksO1 : runChecks;
const resp = await fn({
const resp = await runChecks({
term: card.term,
definition: card.definition,
langCode: card.langCode,
Expand All @@ -233,6 +185,5 @@ export const grammarCorrectionNG: QuizEvaluator = async ({
}
};

const results = await Promise.all([check(), check()]);
return results.find((response) => response.result === "pass") || results[0];
return check();
};
5 changes: 2 additions & 3 deletions koala/quiz-evaluators/speaking-evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ export const speaking: QuizEvaluator = async (input) => {
if (compare(userInput, term, 1)) {
return { result: "pass", userMessage: "Exact match." };
}
const promises = CHECKS.map((g) => g(input));
const promises = await Promise.all(CHECKS.map((g) => g(input)));
// Run grammar and equivalence checks in sequence:
for (const promise of promises) {
const result = await promise;
for (const result of promises) {
if (result.result === "fail") {
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion koala/review/review-over.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function ReviewOver({
</Alert>
<Text size="sm">
The server found some issues with your responses. Check the
feedback below. {score}%
feedback below. {100 - score}%
</Text>
<div
style={{
Expand Down
2 changes: 1 addition & 1 deletion koala/trpc-routes/calculate-scheduling-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type SchedulingData = {
};

function fuzzNumber(num: number) {
let pct = num * 0.2;
let pct = num * 0.3;
let fuzzFactor = (Math.random() * 2 - 1) * pct;

return num + fuzzFactor;
Expand Down
7 changes: 4 additions & 3 deletions koala/trpc-routes/remix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const LANG_SPECIFIC_PROMPT: Record<string, string> = {
};

const MODELS: Record<"good" | "fast" | "cheap" | "premium", string> = {
premium: "o1-preview", // $10.00 + $10.00 = $20.00
premium: "o1", // $10.00 + $10.00 = $20.00
good: "o1-mini", // $12.00 + $3.00 = $15.00
fast: "gpt-4o", // $2.50 + $10 = $12.50
cheap: "gpt-4o-mini", // $0.150 + $0.600 = $0.750
Expand Down Expand Up @@ -57,15 +57,16 @@ const buildRemixPrompt = (
term: string,
): string => {
const langSpecific = LANG_SPECIFIC_PROMPT[langCode.toUpperCase()] || "";
return [
const out = [
"You are a language teacher.",
"You help the student learn a language by creating 'remix' sentences using the input above.",
"You help the student learn a language by creating 'remix' sentences using the input below.",
"Your student is a native English speaker.",
"Create a few short, grammatically correct sentences that help the student understand the term.",
langSpecific,
`INPUT: ${term}`,
`YOUR TASK: ${RemixTypePrompts[type]}`,
].join("\n");
return out;
};

const fetchOpenAIResponse = async (
Expand Down
19 changes: 13 additions & 6 deletions pages/repair.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import { LangCode } from "@/koala/shared-types";
import { getServersideUser } from "@/koala/get-serverside-user";
import { GetRepairOutputParams } from "@/koala/fetch-failure-types";


// Fetch data on the server:
type RepairPageProps = {
cards: GetRepairOutputParams;
};

const ATTEMPTS = 2;

export const getServerSideProps: GetServerSideProps<RepairPageProps> = async (
context,
) => {
Expand Down Expand Up @@ -107,7 +110,7 @@ function CardRepairFlow({
// Increment the correct attempts counter
setCorrectAttempts((prev) => {
const newCount = prev + 1;
if (newCount === 3) {
if (newCount === ATTEMPTS) {
repairCard(card.cardId);
playAudio(card.definitionAudio);
onComplete();
Expand Down Expand Up @@ -146,10 +149,10 @@ function CardRepairFlow({

<Stack gap="xs">
<Text size="sm">
You need <strong>3 correct</strong> attempts.
You need <strong>{ATTEMPTS} correct</strong> attempts.
</Text>
<Text size="sm">
Current count: <strong>{correctAttempts}</strong>/3
Current count: <strong>{correctAttempts}</strong>/{ATTEMPTS}
</Text>

{lastAttemptWrong && (
Expand Down Expand Up @@ -190,7 +193,7 @@ function CardRepairFlow({

/**
* Main Repair Page:
* - Renders one card at a time until “3 consecutive correct” resets the card’s failure
* - Renders one card at a time until “n consecutive correct” resets the card’s failure
* - Then moves on to the next card
*/
export default function RepairPage({ cards }: RepairPageProps) {
Expand All @@ -213,7 +216,11 @@ export default function RepairPage({ cards }: RepairPageProps) {
return (
<Center style={{ minHeight: "100vh" }}>
<Stack align="center" justify="center" p="xl">
<Title order={3}>All cards have been repaired!</Title>
<Title order={3}>Session Complete</Title>
<Text size="md" color="dimmed">
You repaired {cards.length} cards.
<Button onClick={() => window.location.reload()}>Load More?</Button>
</Text>
</Stack>
</Center>
);
Expand All @@ -229,7 +236,7 @@ export default function RepairPage({ cards }: RepairPageProps) {
<Stack gap="lg" p="xl" align="center" justify="center">
<Title order={1}>Repair Mode</Title>
<Text size="md" color="dimmed">
Repeat each word/phrase <strong>3 times correctly</strong> to repair.
Repeat each word/phrase <strong>{3} times correctly</strong> to repair.
</Text>
<CardRepairFlow card={currentCard} onComplete={handleComplete} />
</Stack>
Expand Down

0 comments on commit e54a7d0

Please sign in to comment.