Home / Attachments / JavaScript
Duplicate Snippet

Embed Snippet on Your Site

JavaScript

document.getElementById('chat-form').addEventListener('submit', async function(e) {
e.preventDefault();

// Captura el texto del usuario
const userInput = document.getElementById('user-input').value;

// Muestra un mensaje de carga
const responseDiv = document.getElementById('response');
responseDiv.innerHTML = "Cargando...";

// Llama a la API de OpenAI
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer http://www.npmjs.com/package/openai' // Cambia TU_API_KEY por tu clave API
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [{"role": "user", "content": userInput}]
})
});

// Procesa la respuesta
const data = await response.json();
responseDiv.innerHTML = data.choices[0].message.content;
});

Code Preview
php
<?php

Comments

Add a Comment