document.addEventListener("submit", function(event){
  event.preventDefault();
  
  var user_input = document.getElementById("user_input").value;
  var api_key = "YOUR_OPENAI_API_KEY";
  
  fetch("https://api.openai.com/v1/engines/davinci-codex/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Bearer ${api_key}`
    },
    body: JSON.stringify({
      prompt: user_input,
      max_tokens: 150
    })
  })
  .then(response => response.json())
  .then(data => {
    document.getElementById("response").innerText = data.choices[0].text;
  })
  .catch(error => {
    console.error("Error:", error);
  });
});