-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
111 lines (105 loc) · 3.42 KB
/
index.html
File metadata and controls
111 lines (105 loc) · 3.42 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
<!DOCTYPE html>
<html>
<head>
<title>Project Idea Generator</title>
<style>
form {
display: flex;
flex-direction: column;
align-items: center;
}
label {
font-weight: bold;
margin-bottom: 5px;
}
#displayProjectIdea {
margin-top: 10px;
text-align: center;
font-weight: bold;
}
</style>
</head>
<body>
<div>
<form>
<label for="languageSelect">Select a language:</label>
<select id="languageSelect" name="languageSelect">
<option value="JavaScript">JavaScript</option>
<option value="Python">Python</option>
<option value="Java">Java</option>
<option value="C++">C++</option>
</select>
<br>
<label for="skillSelect">Select a skill level:</label>
<select id="skillSelect" name="skillSelect">
<option value="Beginner">Beginner</option>
<option value="Intermediate">Intermediate</option>
<option value="Advanced">Advanced</option>
</select>
<br>
<br>
<input type="button" id="submitBtn" value="Submit">
</form>
<p id="displayProjectIdea"></p>
</div>
<script>
const API_KEY = "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";
const languageSelect = document.getElementById("languageSelect");
const skillSelect = document.getElementById("skillSelect");
const submitBtn = document.getElementById("submitBtn");
const displayProjectIdea = document.getElementById("displayProjectIdea");
let cache = {};
async function getProjectIdea(language, skill) {
const userInput = `Generate a project idea for a ${skill} level ${language} developer.`;
if (cache[userInput] && cache[userInput].timestamp > Date.now() - 86400000) {
console.log("Response retrieved from cache");
return cache[userInput].data;
}
console.log("getProjectIdea function called");
const endpoint = "https://api.openai.com/v1/engines/text-davinci-002/completions";
const body = {
prompt: userInput,
max_tokens: 250,
stop: "Project completed",
temperature: 0.75
}
try {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`
},
body: JSON.stringify(body)
});
const json = await response.json();
if (json.choices && json.choices.length > 0) {
cache[userInput] = {
data: json.choices[0].text,
timestamp: Date.now()
};
return json.choices[0].text;
} else {
throw new Error('No project ideas found');
}
} catch (error) {
console.log(error);
}
}
async function main() {
submitBtn.addEventListener("click", async function() {
const language = languageSelect.value;
const skill = skillSelect.value;
try {
const projectIdea = await getProjectIdea(language, skill);
console.log(projectIdea);
displayProjectIdea.innerHTML = projectIdea;
} catch (error) {
console.log(error);
}
});
}
main();
</script>
</body>
</html>