|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import { createClient } from "@shared/utils/supabase/server"; |
| 4 | +import { |
| 5 | + ChatClientWithSession, |
| 6 | + createChatClient, |
| 7 | + sendMessage, |
| 8 | +} from "@shared/lib/ragflow/chat/chat-client"; |
| 9 | + |
| 10 | +import { getUserAndClassroomData } from "@shared/lib/userContext/contextFetcher"; |
| 11 | + |
| 12 | +import { AugmentConfigTemplate } from "@shared/lib/ragflow/chat/chat-configs"; |
| 13 | + |
| 14 | +// import { revalidatePath } from "next/cache"; |
| 15 | +// import { |
| 16 | +// ChatClientWithSession, |
| 17 | +// createChatClient, |
| 18 | +// deleteSession, |
| 19 | +// sendMessage, |
| 20 | +// } from "@shared/lib/ragflow/chat/chat-client"; |
| 21 | +// import { createDatasetClient } from "@shared/lib/ragflow/dataset-client"; |
| 22 | + |
| 23 | +export async function createNotesClient(classroomId: string) { |
| 24 | + //create a session for that set of notes |
| 25 | + |
| 26 | + //first get the dataset ID |
| 27 | + const supabase = await createClient(); |
| 28 | + const { data, error } = await supabase |
| 29 | + .from("Classrooms") |
| 30 | + .select() |
| 31 | + .eq("id", Number(classroomId)) |
| 32 | + .single(); |
| 33 | + |
| 34 | + if (error) { |
| 35 | + console.error("Error fetching classroom data:", error); |
| 36 | + throw new Error("Error fetching classroom data"); |
| 37 | + } |
| 38 | + |
| 39 | + const datasetId: string = data?.ragflow_dataset_id || ""; |
| 40 | + |
| 41 | + // console.log("datasetId", datasetId); |
| 42 | + // console.log("classroomId", classroomId); |
| 43 | + |
| 44 | + // const params = { dataset_ids: [datasetId], name: datasetId }; |
| 45 | + |
| 46 | + // const res = await fetch(`${process.env.RAGFLOW_API_URL}/v1/chats`, { |
| 47 | + // method: "POST", // or 'PUT' |
| 48 | + // headers: { |
| 49 | + // Authorization: `Bearer ${process.env.RAGFLOW_API_KEY}`, |
| 50 | + // "Content-Type": "application/json", |
| 51 | + // }, |
| 52 | + // body: JSON.stringify(params), |
| 53 | + // }); |
| 54 | + |
| 55 | + // console.log("res", res); |
| 56 | + |
| 57 | + const userAndClassData = await getUserAndClassroomData(); |
| 58 | + |
| 59 | + const resClient = await createChatClient( |
| 60 | + { |
| 61 | + ...AugmentConfigTemplate, |
| 62 | + associatedClassroomName: "Classroom", |
| 63 | + primaryKeyValuesAssistant: [{ key: "id", value: classroomId }], |
| 64 | + primaryKeyValuesSession: [ |
| 65 | + { key: "classroom_id", value: classroomId }, |
| 66 | + { key: "user_id", value: userAndClassData?.userData.id }, |
| 67 | + ], |
| 68 | + datasets: [datasetId], |
| 69 | + } |
| 70 | + // classroomInfo.chat_assistant_id |
| 71 | + ); |
| 72 | + |
| 73 | + if (!resClient.client) { |
| 74 | + console.error("Client is null"); |
| 75 | + throw new Error("Invalid chat client instance"); |
| 76 | + } |
| 77 | + |
| 78 | + if (!("sessionId" in resClient.client)) { |
| 79 | + console.error("Client not of correct type"); |
| 80 | + throw new Error("Invalid chat client instance: Missing sessionId"); |
| 81 | + } |
| 82 | + const chatClient: ChatClientWithSession = resClient.client; |
| 83 | + |
| 84 | + // console.log( |
| 85 | + // "chatClient", |
| 86 | + // chatClient.clientConfig.modelSettings.promptSettings |
| 87 | + // ); |
| 88 | + |
| 89 | + // console.log("datasetId", datasetId); |
| 90 | + |
| 91 | + return chatClient; |
| 92 | +} |
| 93 | + |
| 94 | +export async function reviseNotesLine( |
| 95 | + chatClient: ChatClientWithSession, |
| 96 | + passedLine: string |
| 97 | +) { |
| 98 | + const passMessage: string = `Below is a line of text notes taken by a student. |
| 99 | + Please augment these notes with you comments and revise them primarily based on accuracy. |
| 100 | + Only change the parts you want to revise. |
| 101 | + Do not rewrite the notes, simply make small edits for correctness. |
| 102 | + Do not make style and diction revisions. |
| 103 | + Return the notes exactly as given if they are already correct. |
| 104 | + If they notes are accurate, leave them exactly as is. |
| 105 | + Also, please do not use any markdown formatting. |
| 106 | + ${passedLine} |
| 107 | +`; |
| 108 | + |
| 109 | + const messageResponse = await sendMessage(chatClient, passMessage); |
| 110 | + |
| 111 | + if (!messageResponse.ragflowCallSuccess) { |
| 112 | + console.error("Error sending message"); |
| 113 | + throw new Error("Error sending message"); |
| 114 | + } |
| 115 | + |
| 116 | + const revisedNotes: string = messageResponse.response |
| 117 | + .replace("Notes: ", "") |
| 118 | + .replaceAll("##0$$\n", ""); |
| 119 | + // console.log("revisedNotes", revisedNotes); |
| 120 | + |
| 121 | + const reason: string = await getAugmentReason( |
| 122 | + chatClient, |
| 123 | + passedLine, |
| 124 | + revisedNotes |
| 125 | + ); |
| 126 | + |
| 127 | + return [revisedNotes, reason]; |
| 128 | +} |
| 129 | + |
| 130 | +export async function getAugmentReason( |
| 131 | + chatClient: ChatClientWithSession, |
| 132 | + originalNotes: string, |
| 133 | + augmentedNotes: string |
| 134 | +) { |
| 135 | + const passMessage: string = `In a previous message, you augmented the following notes: |
| 136 | + "${originalNotes}" |
| 137 | + to |
| 138 | + "${augmentedNotes}" |
| 139 | +
|
| 140 | + Can you explain why you made this change? |
| 141 | + Please do not use any markdown formatting in your response. |
| 142 | +`; |
| 143 | + |
| 144 | + const messageResponse = await sendMessage(chatClient, passMessage); |
| 145 | + |
| 146 | + if (!messageResponse.ragflowCallSuccess) { |
| 147 | + console.error("Error getting explanation"); |
| 148 | + throw new Error("Error getting explanation"); |
| 149 | + } |
| 150 | + |
| 151 | + return messageResponse.response; |
| 152 | +} |
0 commit comments