-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
67 lines (59 loc) · 2.55 KB
/
scripts.js
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
// Get HTML elements
const cuisineButtons = document.querySelectorAll(".cuisine-button");
const categoryInput = document.querySelector("#category-input");
const ingredientInput = document.querySelector("#ingredient-input");
const toneSelect = document.querySelector("#tone-select");
const generateButton = document.querySelector("#generate-button");
const outputTitle = document.querySelector("#output-title");
const outputDescription = document.querySelector("#output-description");
const otherCuisineInput = document.querySelector("#other-cuisine-input");
// Add event listeners
cuisineButtons.forEach((button) => {
button.addEventListener("click", () => {
if (button.value === "Other") {
otherCuisineInput.style.display = "block";
} else {
otherCuisineInput.style.display = "none";
}
});
});
generateButton.addEventListener("click", async () => {
// Get user inputs
const cuisine = document.querySelector('input[name="cuisine"]:checked').value;
const category = categoryInput.value.trim();
const ingredients = ingredientInput.value.trim();
const tone = toneSelect.value;
const prompt = `Generate an item name and description for a ${cuisine.toLowerCase()} dish that is ${tone.toLowerCase()}. The dish is a ${category.toLowerCase()} made with ${ingredients.toLowerCase()}.{}`;
let body = {prompt, model: "text-davinci-003", "max_tokens": 100, stop: "{}", temperature: 0.5, n: 1}
const headers = { Authorization : "Bearer REPLACEMEWITHTOKEN", "Content-Type": "application/json"};
try {
body = JSON.stringify(body);
const response = await fetch("https://api.openai.com/v1/completions", { method: "POST", headers , body });
const reader = response.body.getReader();
const stream = new ReadableStream({
start(controller) {
return pump();
function pump() {
return reader.read().then(({ done, value }) => {
// When no more data needs to be consumed, close the stream
if (done) {
controller.close();
return;
}
// Enqueue the next data chunk into our target stream
controller.enqueue(value);
return pump();
});
}
},
});
let result = await new Response(stream, { headers: { "Content-Type": "text/html" } }).text();
result = JSON.parse(result)
const output = result.choices[0].text.trim();
const [title, description] = output.split(" - ");
outputTitle.textContent = title;
outputDescription.textContent = description;
} catch (error) {
console.log(error)
}
});