Skip to content

Commit

Permalink
Merge pull request #22 from Gerosullivan/scoring-chat-metadata-refact…
Browse files Browse the repository at this point in the history
…or-topic-quick-quiz

Scoring chat metadata refactor topic quick quiz
  • Loading branch information
Gerosullivan authored Oct 8, 2024
2 parents b2cebbc + 431a7a7 commit be9255e
Show file tree
Hide file tree
Showing 15 changed files with 419 additions and 256 deletions.
10 changes: 9 additions & 1 deletion app/[locale]/[workspaceid]/chat/[chatid]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ export default function ChatIDPage() {

if (chat.topic_description) {
setTopicDescription(chat.topic_description)
const forgottenFactsArray =
typeof chat.recall_analysis === "string"
? (JSON.parse(chat.recall_analysis) as string[])
: []

handleNewState("topic_default")
if (forgottenFactsArray.length > 0) {
handleNewState("topic_default_quiz")
} else {
handleNewState("topic_default")
}
} else if (chat.name && chat.name !== "New topic") {
handleNewState("topic_no_description_in_db")
} else {
Expand Down
29 changes: 0 additions & 29 deletions app/[locale]/[workspaceid]/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,6 @@ export default function ChatPage() {
startTutorial()
}, [profile, router, pathname])

useEffect(() => {
const recallAnalysisInChats: { chatId: string; recallAnalysis: string }[] =
chats.reduce(
(acc: { chatId: string; recallAnalysis: string }[], chat) => {
if (chat.recall_analysis != null) {
try {
const recallAnalysisList: string[] = JSON.parse(
chat.recall_analysis as string
)
recallAnalysisList.forEach((recallAnalysis: string) => {
acc.push({ chatId: chat.id, recallAnalysis })
})
} catch (e) {
console.error(
"Failed to parse recall_analysis:",
chat.recall_analysis
)
}
}
return acc
},
[]
)

if (recallAnalysisInChats.length > 0) {
setAllChatRecallAnalysis(recallAnalysisInChats)
}
}, [chats])

return (
<div className="relative flex h-full flex-col items-center justify-center">
<div className="top-50% left-50% -translate-x-50% -translate-y-50% absolute mb-20">
Expand Down
39 changes: 37 additions & 2 deletions app/[locale]/[workspaceid]/chat/quick-quiz/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import { useContext, useEffect } from "react"
import { useChatHandler } from "@/components/chat/chat-hooks/use-chat-handler"

export default function ChatIDPage() {
const { setSelectedChat, allChatRecallAnalysis, setMessages } =
useContext(LearntimeContext)
const {
setSelectedChat,
allChatRecallAnalysis,
setMessages,
setAllChatRecallAnalysis,
chats
} = useContext(LearntimeContext)

const { handleNewState } = useChatHandler()

Expand All @@ -18,5 +23,35 @@ export default function ChatIDPage() {
handleNewState("quick_quiz_ready")
}, [])

useEffect(() => {
console.log("populating recall analysis")
const recallAnalysisInChats: { chatId: string; recallAnalysis: string }[] =
chats.reduce(
(acc: { chatId: string; recallAnalysis: string }[], chat) => {
if (chat.recall_analysis != null) {
try {
const recallAnalysisList: string[] = JSON.parse(
chat.recall_analysis as string
)
recallAnalysisList.forEach((recallAnalysis: string) => {
acc.push({ chatId: chat.id, recallAnalysis })
})
} catch (e) {
console.error(
"Failed to parse recall_analysis:",
chat.recall_analysis
)
}
}
return acc
},
[]
)

if (recallAnalysisInChats.length > 0) {
setAllChatRecallAnalysis(recallAnalysisInChats)
}
}, [chats, setAllChatRecallAnalysis])

return <ChatUI chatTitle="Quick Quiz" />
}
27 changes: 19 additions & 8 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { handleHinting } from "@/lib/server/hinting-handler"
import { handleReview } from "@/lib/server/review-handler"
import { handleQuickQuizQuestion } from "@/lib/server/quick-quiz-question-handler"
import { handleQuickQuizAnswer } from "@/lib/server/quick-quiz-answer-handler"
import { handleRecallShowHints } from "@/lib/server/recall-show-hints-handler"
import { StudyState } from "@/lib/studyStates"

// export const runtime = "edge"
export const dynamic = "force-dynamic"
Expand All @@ -19,20 +21,21 @@ export async function POST(request: Request) {
chatId,
studyState,
studySheet,
chatRecallMetadata,
chatRecallInfo,
randomRecallFact,
systemContext
} = json

const studentMessage = messages[messages.length - 1]

const defaultModel = openai("gpt-4o-mini") as LanguageModel
const scoringModel = defaultModel
const scoringModel = openai("gpt-4o", {
structuredOutputs: true
}) as LanguageModel
const hintingModel = defaultModel

switch (studyState) {
switch (studyState as StudyState) {
case "topic_name_saved":
case "topic_auto_generate":
case "topic_describe_upload":
case "topic_no_description_in_db":
return await handleTopicGeneration(
Expand All @@ -41,8 +44,8 @@ export async function POST(request: Request) {
systemContext
)

case "recall_tutorial_first_attempt":
case "recall_first_attempt":
case "tutorial_recall_first_attempt":
return await handleRecallAttempt(
scoringModel,
defaultModel,
Expand All @@ -53,14 +56,22 @@ export async function POST(request: Request) {
systemContext
)

case "recall_tutorial_hinting":
case "recall_hinting":
case "recall_show_hints":
return await handleRecallShowHints(
defaultModel,
studySheet,
chatRecallInfo,
systemContext
)
case "recall_final_suboptimal_feedback":
case "recall_answer_hints":
case "tutorial_recall_answer_hints":
return await handleHinting(
hintingModel,
messages,
studyState,
studySheet,
chatRecallMetadata,
chatRecallInfo,
systemContext
)

Expand Down
37 changes: 25 additions & 12 deletions components/chat/QuickResponse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import { v4 as uuidv4 } from "uuid"
const QuickResponse: React.FC<{
setFiles: (files: FileList | null) => void
}> = ({ setFiles }) => {
const { chatStudyState, setMessages } = useContext(LearntimeContext)
const {
chatStudyState,
setMessages,
selectedChat,
setAllChatRecallAnalysis
} = useContext(LearntimeContext)

const { handleNewState, handleTopicSave, handleQuickResponseLLMCall } =
useChatHandler()
Expand All @@ -25,13 +30,29 @@ const QuickResponse: React.FC<{

if (!newStudyState) return

const newStudyStateObject = getStudyStateObject(newStudyState)
let newStudyStateObject = getStudyStateObject(newStudyState)

if (!newStudyStateObject) {
console.log("No study state object found for", newStudyState)
return
}

if (newStudyState === "quick_quiz_ready" && selectedChat) {
const forgottenFactsArray =
typeof selectedChat.recall_analysis === "string"
? (JSON.parse(selectedChat.recall_analysis) as string[])
: []

if (forgottenFactsArray.length > 0) {
setAllChatRecallAnalysis(
forgottenFactsArray.map(fact => ({
chatId: selectedChat?.id || "",
recallAnalysis: fact
}))
)
}
}

if (newStudyStateObject.message === "{{LLM}}") {
handleQuickResponseLLMCall(message, newStudyState)
return
Expand Down Expand Up @@ -61,19 +82,11 @@ const QuickResponse: React.FC<{

const quickResponses = getQuickResponses(chatStudyState)

const widthClass = (index: number): string => {
const totalButtons = quickResponses.length
if (totalButtons === 1) return "w-full"
// For the last button in an odd-numbered array, it should take full width
if (totalButtons % 2 !== 0 && index === totalButtons - 1) return "w-full"
return "w-1/2"
}

// Render buttons based on the available quickResponses
return (
<div className="flex flex-wrap">
<div className="flex flex-wrap justify-center">
{quickResponses.map((quickResponse: QuickResponseType, index) => (
<div key={index} className={`${widthClass(index)} p-2`}>
<div key={index} className="w-1/2 p-2">
<button
className="flex w-full items-center justify-between rounded-md border border-blue-500 px-4 py-2 text-left text-blue-500 transition-colors hover:bg-blue-500 hover:text-white"
onClick={() =>
Expand Down
11 changes: 9 additions & 2 deletions components/chat/chat-hooks/use-chat-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const useChatHandler = () => {
setTopicDescription,
allChatRecallAnalysis,
setAllChatRecallAnalysis,
chatRecallMetadata,
setMessages,
messages,
setInput,
Expand Down Expand Up @@ -77,11 +76,19 @@ ${selectedWorkspace?.instructions || ""}
`

const chatRecallInfo = currentChat
? {
score: currentChat.test_result,
due_date: currentChat.due_date,
forgottenFacts: currentChat.recall_analysis
}
: null

return {
chatId: currentChat?.id,
studyState,
studySheet,
chatRecallMetadata,
chatRecallInfo,
randomRecallFact,
systemContext
}
Expand Down
1 change: 1 addition & 0 deletions components/messages/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const Message: FC<MessageProps> = ({ message, isLast }) => {
</div>
)}
<MessageMarkdown content={message.content} />

{message.experimental_attachments &&
message.experimental_attachments.length > 0 && (
<div className="mt-2 flex flex-row gap-2">
Expand Down
21 changes: 4 additions & 17 deletions components/utility/global-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { StudyState } from "@/lib/studyStates"
import { convertBlobToBase64 } from "@/lib/blob-to-b64"
import { supabase } from "@/lib/supabase/browser-client"
import { Tables } from "@/supabase/types"
import { ChatRecallMetadata } from "@/lib/studyStates"
import { WorkspaceImage } from "@/types"
import { useChat } from "ai/react"
import { useRouter } from "next/navigation"
Expand Down Expand Up @@ -40,33 +39,23 @@ export const GlobalState: FC<GlobalStateProps> = ({ children }) => {
const [selectedChat, setSelectedChat] = useState<Tables<"chats"> | null>(null)
const [topicDescription, setTopicDescription] = useState<string>("")
const [chatStudyState, setChatStudyState] = useState<StudyState>("home")
const [chatRecallMetadata, setChatRecallMetadata] =
useState<ChatRecallMetadata | null>(null)
const [allChatRecallAnalysis, setAllChatRecallAnalysis] = useState<
{ chatId: string; recallAnalysis: any }[]
{ chatId: string; recallAnalysis: string }[]
>([])

const handleResponse = async (response: Response) => {
const newStudyState = response.headers.get("NEW-STUDY-STATE") as StudyState

const chatUpdated = response.headers.get("CHAT-UPDATED")
if (newStudyState) {
setChatStudyState(newStudyState)
}

const score = response.headers.get("SCORE")
if (score) {
const dueDateFromNow = response.headers.get("DUE-DATE-FROM-NOW")

setChatRecallMetadata({
score: parseInt(score),
dueDateFromNow: dueDateFromNow!
})
}

const isQuickQuiz: boolean = chatStudyState.startsWith("quick_quiz")

if (!isQuickQuiz) {
if (!isQuickQuiz && chatUpdated) {
const updatedChat = await getChatById(selectedChat!.id)
setSelectedChat(updatedChat)

if (updatedChat) {
setChats(prevChats => {
Expand Down Expand Up @@ -176,8 +165,6 @@ export const GlobalState: FC<GlobalStateProps> = ({ children }) => {
setTopicDescription,
chatStudyState,
setChatStudyState,
chatRecallMetadata,
setChatRecallMetadata,
allChatRecallAnalysis,
setAllChatRecallAnalysis,
input,
Expand Down
5 changes: 0 additions & 5 deletions context/context.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Tables } from "@/supabase/types"
import { ChatRecallMetadata } from "@/lib/studyStates"
import { WorkspaceImage } from "@/types"
import { Dispatch, SetStateAction, createContext } from "react"
import { StudyState } from "@/lib/studyStates"
Expand Down Expand Up @@ -30,8 +29,6 @@ interface LearntimeContext {
setTopicDescription: Dispatch<SetStateAction<string>>
chatStudyState: StudyState
setChatStudyState: Dispatch<SetStateAction<StudyState>>
chatRecallMetadata: ChatRecallMetadata | null
setChatRecallMetadata: Dispatch<SetStateAction<ChatRecallMetadata | null>>
allChatRecallAnalysis: { chatId: string; recallAnalysis: any }[]
setAllChatRecallAnalysis: Dispatch<
SetStateAction<{ chatId: string; recallAnalysis: any }[]>
Expand Down Expand Up @@ -79,8 +76,6 @@ export const LearntimeContext = createContext<LearntimeContext>({
setTopicDescription: () => {},
chatStudyState: "home",
setChatStudyState: () => {},
chatRecallMetadata: null,
setChatRecallMetadata: () => {},
allChatRecallAnalysis: [],
setAllChatRecallAnalysis: () => {},

Expand Down
Loading

0 comments on commit be9255e

Please sign in to comment.