-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoodle-to-llm.js
More file actions
137 lines (126 loc) · 4.31 KB
/
Copy pathmoodle-to-llm.js
File metadata and controls
137 lines (126 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// This script works in the browser console of a quiz page inside Moodle.
// The problem is that, by default, the CORS policy blocks the request to the API,
// so that needs to be solved first.
//
// Don't forget to replace the API_KEY_HERE placeholders with your actual API keys.
async function fetchFromOpenRouter(prompt) {
const API_KEY = "YOUR_KEY_HERE";
const response = await fetch(
"https://openrouter.ai/api/v1/chat/completions",
{
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"HTTP-Referer": `${window.location.origin}`, // Optional. Site URL for rankings on openrouter.ai.
"X-Title": "Moodler Helper", // Optional. Site title for rankings on openrouter.ai.
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "openai/gpt-4o",
// response_format: {
// type: "json_schema",
// json_schema: {
// name: "answers",
// strict: true,
// schema: {
// type: "object",
// properties: {
// items: {
// type: "array",
// items: {
// type: "object",
// properties: {
// id: { type: "string" },
// answer: {
// type: "object",
// properties: {
// answerNumber: { type: "string" },
// textAnswer: { type: "string" },
// subquestions: {
// type: "array",
// items: {
// type: "object",
// properties: {
// id: { type: "string" },
// },
// required: ["id"],
// additionalProperties: false,
// },
// },
// },
// additionalProperties: false,
// },
// },
// required: ["id", "answer"],
// additionalProperties: false,
// },
// },
// },
// required: ["items"],
// additionalProperties: false,
// },
// },
// },
messages: [
{
role: "user",
content: prompt,
},
],
max_tokens: 3946,
}),
}
);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
const json = await response.json();
return json;
}
const questions = $(".que")
.map(function (_, que) {
const id = $(".qno", que).text();
const question = {};
const text = $(".qtext", que).text().trim();
if (text.length > 0) question.text = text;
const options = $(".ablock > .answer", que)
.children()
.map(function (_, option) {
const answerNumberElement = $(".answernumber", option);
const answerNumber = answerNumberElement.text().trim();
const text = answerNumberElement.next().text().trim();
return { answerNumber, text };
})
.get();
if (options.length > 0) question.options = options;
const subquestions = $(".subquestion", que)
.map(function (_, subquestion) {
const text = $(subquestion)
.parent()
.contents()
.filter(function () {
return this.nodeType === 3; // Node.TEXT_NODE
})
.text();
const options = $("select > option", subquestion)
.map(function (_, option) {
const id = $(option).attr("value").trim();
const text = $(option).text().trim();
return { id, text };
})
.get();
return { text, options };
})
.get();
if (subquestions.length > 0) question.subquestions = subquestions;
return {
id,
question,
};
})
.get();
const jsonAsText = JSON.stringify(questions);
const llmPrompt = `Answer the questions inside the JSON object: ${jsonAsText}`;
console.log(llmPrompt);
const openRouterAnswer = await fetchFromOpenRouter(llmPrompt);
console.log(openRouterAnswer);