OpenAI GPT Chat App

OpenAI GPT Chat App

    Send
    const chatForm = document.querySelector('#chat-form'); const chatInput = document.querySelector('#chat-input'); const chatList = document.querySelector('#chat-list'); // create a new instance of OpenAI's GPT const gpt = new OpenAI('YOUR_API_KEY'); // function to add a new chat message to the list function addMessage(message, sender) { const li = document.createElement('li'); li.textContent = `${sender}: ${message}`; chatList.appendChild(li); } // function to send a user message to the GPT and add the response to the chat list async function sendMessage(event) { event.preventDefault(); const message = chatInput.value; addMessage(message, 'You'); chatInput.value = ''; const response = await gpt.complete({ engine: 'davinci', prompt: `User: ${message}\nAI:`, maxTokens: 100, n: 1, stop: '\n' }); addMessage(response.choices[0].text.trim(), 'AI'); } // add an event listener to the chat form to send messages when the form is submitted chatForm.addEventListener('submit', sendMessage); const chatForm = document.querySelector('#chat-form'); const chatInput = document.querySelector('#chat-input'); const chatList = document.querySelector('#chat-list'); // create a new instance of OpenAI's GPT const gpt = new OpenAI('YOUR_API_KEY'); // function to add a new chat message to the list function addMessage(message, sender) { const li = document.createElement('li'); li.textContent = `${sender}: ${message}`; chatList.appendChild(li); } // function to send a user message to the GPT and add the response to the chat list async function sendMessage(event) { event.preventDefault(); const message = chatInput.value; addMessage(message, 'You'); chatInput.value = ''; const response = await gpt.complete({ engine: 'davinci', prompt: `User: ${message}\nAI:`, maxTokens: 100, n: 1, stop: '\n' }); addMessage(response.choices[0].text.trim(), 'AI'); } // add an event listener to the chat form to send messages when the form is submitted chatForm.addEventListener('submit', sendMessage);

    تعليقات

    المشاركات الشائعة من هذه المدونة