scrimba
AI Apps
Build a movie idea generator with ChatGPT and Dall-E
Tokens
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

index.js
run
preview
console
import { process } from '/env'
import { Configuration, OpenAIApi } from 'openai'

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

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY
})

const openai = new OpenAIApi(configuration)

document.getElementById("send-btn").addEventListener("click", () => {
if (setupTextarea.value) {
const userInput = 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(userInput)
}
})

async function fetchBotReply(outline) {
const response = await openai.createCompletion({
model: 'text-davinci-003',
prompt: `Generate a short message to enthusiastically say "${outline}" sounds interesting and that you need some minutes to think about it. Mention one aspect of the sentence."`,
max_tokens: 60
})
movieBossText.innerText = response.data.choices[0].text.trim()
console.log(response)
}
Console
{data:
{id:
"cmpl-6hzpsbiOPbgNJvS5Fw2xH3a05kzYQ"
, object:
"text_completion"
, created:
1675943820
, model:
"text-davinci-003"
, choices:
[
{text:
" Wow, that's an intriguing thought - trees learning to walk and take"
, index:
0
, logprobs:
null
, finish_reason:
"length"
}
]
, usage:
{prompt_tokens:
41
, completion_tokens:
16
, total_tokens:
57
}
}
, status:
200
, statusText:
""
, headers:
{cache-control:
"no-cache, must-revalidate"
, content-length:
"335"
, content-type:
"application/json"
}
, config:
{transitional:
{silentJSONParsing:
true
, forcedJSONParsing:
true
, clarifyTimeoutError:
false
}
, adapter:
xhrAdapter(e)
, transformRequest:
[
transformRequest(e,t)
]
, transformResponse:
[
transformResponse(e)
]
, timeout:
0
, xsrfCookieName:
"XSRF-TOKEN"
, xsrfHeaderName:
"X-XSRF-TOKEN"
, maxContentLength:
-1
, maxBodyLength:
-1
, validateStatus:
validateStatus(e)
, headers:
{Accept:
"application/json, text/plain, */*"
, Content-Type:
"application/json"
, User-Agent:
"OpenAI/NodeJS/3.1.0"
, Authorization:
"Bearer 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"
}
, method:
"post"
, data:
"{"model":"text-davinci-003","prompt":"Generate a short message to enthusiastically say \"Trees learn to walk and take over the country.\" sounds interesting and that you need some minutes to think about it. Mention one aspect of the sentence.\""}"
, url:
"https://api.openai.com/v1/completions"
}
, request:
XMLHttpRequest {}
}
,
/index.html
-4:02