Skip to content

Commit

Permalink
Merge pull request #67 from RickCarlino/prod
Browse files Browse the repository at this point in the history
* Update PostgreSQL ENVs
* Allow GPT-4 for approved users
  • Loading branch information
RickCarlino authored Feb 19, 2024
2 parents eb8efea + d26f6c3 commit 7ebc1e6
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ GOOGLE_CLOUD_PROJECT=my-gcs-project-change-this
# -v ${PWD}/db:/var/lib/postgresql/data \
# -p 5432:5432 \
# postgres
DATABASE_URL="postgres://prisma:your_password@localhost:5432/prisma_dev?schema=public"
POSTGRES_URI="postgres://prisma:your_password@localhost:5432/prisma_dev?schema=public"
# This is the path to the JSON file you downloaded from Google Cloud
# Learn more at
# https://cloud.google.com/docs/authentication/application-default-credentials#GAC
Expand Down
21 changes: 13 additions & 8 deletions pages/study.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ function useQuizState(initialState: State) {
}

function Study(props: Props) {
const cardsById = props.quizzes.reduce((acc, quiz) => {
acc[quiz.quizId] = quiz;
return acc;
}, {} as Record<number, Quiz>);
const cardsById = props.quizzes.reduce(
(acc, quiz) => {
acc[quiz.quizId] = quiz;
return acc;
},
{} as Record<number, Quiz>,
);
const settings = useUserSettings();
const newState = newQuizState({
cardsById,
Expand Down Expand Up @@ -199,10 +202,12 @@ function Study(props: Props) {
};
failProps.onDiscard = () => {
if (f.rollbackData) {
rollbackGrade.mutateAsync({
id: f.id,
schedulingData: f.rollbackData,
}).then(clear);
rollbackGrade
.mutateAsync({
id: f.id,
schedulingData: f.rollbackData,
})
.then(clear);
} else {
alert("No rollback data found. This is a bug.");
}
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ generator client {

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
url = env("POSTGRES_URI")
}

model Account {
Expand Down
8 changes: 6 additions & 2 deletions server/quiz-evaluators/listening.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ export const listening: QuizEvaluator = async (ctx) => {
const { userInput, card } = ctx;
const { term, definition, langCode } = card;
const tplData = { term, definition, langCode };
const content = template(PROMPT, tplData);
const listeningYN = await yesOrNo(userInput, content);
const question = template(PROMPT, tplData);
const listeningYN = await yesOrNo({
userInput,
question,
userID: ctx.userID,
});

if (listeningYN.response === "no") {
return {
Expand Down
18 changes: 14 additions & 4 deletions server/quiz-evaluators/speaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,39 @@ const gradeGrammar = async (
term: string,
definition: string,
langCode: string,
userID: string,
): Promise<YesOrNo> => {
const tplData = {
term,
definition,
langCode,
};
const content = template(GRAMMAR_PROMPT, tplData);
const grammarYN = await yesOrNo(userInput, content);
const question = template(GRAMMAR_PROMPT, tplData);
const grammarYN = await yesOrNo({
userInput,
question,
userID,
});
if (grammarYN.response === "no") {
return grammarYN;
}

const meaningYn = await yesOrNo(userInput, template(MEANING_PROMPT, tplData));
const meaningYn = await yesOrNo({
userInput,
question: template(MEANING_PROMPT, tplData),
userID,
});

return meaningYn;
};

export const speaking: QuizEvaluator = async ({ userInput, card }) => {
export const speaking: QuizEvaluator = async ({ userInput, card, userID }) => {
const result = await gradeGrammar(
userInput,
card.term,
card.definition,
card.langCode,
userID,
);
if (result.response === "no") {
console.log(`Fail`);
Expand Down
1 change: 1 addition & 0 deletions server/quiz-evaluators/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type QuizEvaluatorInput = {
quiz: Quiz;
card: Card;
userInput: string;
userID: string;
};

export type QuizEvaluatorOutput = {
Expand Down
5 changes: 4 additions & 1 deletion server/routers/bulk-create-cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export const bulkCreateCards = procedure
z.object({
// Koala does not actually support Spanish.
// It's a placeholder.
langCode: z.union([z.literal("ko"), z.literal("TODO: Support other langauges.")]),
langCode: z.union([
z.literal("ko"),
z.literal("TODO: Support other langauges."),
]),
input: z
.array(
z.object({
Expand Down
4 changes: 2 additions & 2 deletions server/routers/get-next-quizzes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export async function getLessonMeta(userId: string) {
lt: currentDate,
},
firstReview: {
gt: 0
}
gt: 0,
},
},
});

Expand Down
1 change: 1 addition & 0 deletions server/routers/grade-quiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const gradeQuiz = procedure
quiz,
card,
userInput: transcript.text,
userID: user.id,
});
const now = new Date().getTime();
const lastReview = quiz.lastReview;
Expand Down
4 changes: 3 additions & 1 deletion server/routers/import-cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ export async function setGrade(
...calculateSchedulingData(quiz, grade, now),
},
};
console.log(`(${data.data.id}) Update grade. Next review: ${timeUntil(data.data.nextReview)}`);
console.log(
`(${data.data.id}) Update grade. Next review: ${timeUntil(data.data.nextReview)}`,
);
await prismaClient.quiz.update(data);
}

Expand Down
4 changes: 3 additions & 1 deletion server/routers/rollback-grade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const rollbackGrade = procedure
lapses: Math.max(quiz.lapses - 1, 0),
},
};
console.log(`Rollback grade. Next review: ${timeUntil(data.data.nextReview)}`)
console.log(
`Rollback grade. Next review: ${timeUntil(data.data.nextReview)}`,
);
prismaClient.quiz.update(data);
});
1 change: 0 additions & 1 deletion utils/fetch-lesson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import path from "path";
import { draw, map, template } from "radash";
import { errorReport } from "./error-report";
import { prismaClient } from "@/server/prisma-client";
import { timeUntil } from "./time-until";

type LessonType = "listening" | "speaking";

Expand Down
16 changes: 10 additions & 6 deletions utils/openai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import OpenAI from "openai";
import { ChatCompletionCreateParamsNonStreaming } from "openai/resources";
import { errorReport } from "./error-report";
import { isApprovedUser } from "./is-approved-user";

const apiKey = process.env.OPENAI_API_KEY;

Expand Down Expand Up @@ -54,11 +55,13 @@ const YES_OR_NO_FUNCTION = {
};

export type YesOrNo = { response: "yes" | "no"; whyNot?: string };

export const yesOrNo = async (
userInput: string,
question: string,
): Promise<YesOrNo> => {
export type YesOrNoInput = {
userInput: string;
question: string;
userID: string;
};
export const yesOrNo = async (input: YesOrNoInput): Promise<YesOrNo> => {
const { userInput, question, userID } = input;
console.log([userInput, question]);
const grammarResp = await gptCall({
messages: [
Expand All @@ -71,7 +74,7 @@ export const yesOrNo = async (
content: question,
},
],
model: "gpt-3.5-turbo",
model: isApprovedUser(userID) ? "gpt-4-turbo-preview" : "gpt-3.5-turbo",
tools: [
{
type: "function",
Expand All @@ -80,6 +83,7 @@ export const yesOrNo = async (
],
max_tokens: 300,
temperature: 0.7,
user: userID,
tool_choice: { type: "function", function: { name: "yes_or_no" } },
});
const jsonString =
Expand Down

0 comments on commit 7ebc1e6

Please sign in to comment.