scrimba
AI Apps
Build a movie idea generator with ChatGPT and Dall-E
Refactor to use dependency II - The dependency
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Refactor to use dependency II - The dependency
AboutCommentsNotes
Refactor to use dependency II - The dependency
Expand for more info
index.js
run
preview
console
import { process } from '/env'


const setupTextarea = document.getElementById('setup-textarea')
const setupInputContainer = document.getElementById('setup-input-container')
const movieBossText = document.getElementById('movie-boss-text')

const apiKey = process.env.OPENAI_API_KEY
// const apiKey = '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'
const url = 'https://api.openai.com/v1/completions'

document.getElementById("send-btn").addEventListener("click", () => {
// if (setupTextarea.value) {
setupInputContainer.innerHTML = `<img src="images/loading.svg" class="loading" id="loading">`
movieBossText.innerText = `Ok, just wait a second while my digital brain digests that...`
// }
fetchBotReply()
})

function fetchBotReply(){
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
'model': 'text-davinci-003',
'prompt': 'Sound enthusiastic in five words or less.'
})
}).then(response => response.json()).then(data =>
movieBossText.innerText = data.choices[0].text
)
}

Console
/index.html
-2:53