Update fetchReply
AboutCommentsNotes
Update fetchReply
Expand for more info
index.js
run
preview
console
import { initializeApp } from 'firebase/app'
import { getDatabase, ref, push } from 'firebase/database'

import { Configuration, OpenAIApi } from 'openai'
import { process } from './env'

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

const openai = new OpenAIApi(configuration)

const appSettings = {
databaseURL: 'https://knowitall-openai-default-rtdb.europe-west1.firebasedatabase.app/'
}

const app = initializeApp(appSettings)

const database = getDatabase(app)

const conversationInDb = ref(database)

const chatbotConversation = document.getElementById('chatbot-conversation')

const instructionObj = {
role: 'system',
content: 'You are an assistant that gives very short answers.'
}

document.addEventListener('submit', (e) => {
e.preventDefault()
const userInput = document.getElementById('user-input')
push(conversationInDb, {
role: 'user',
content: userInput.value
})
fetchReply()
const newSpeechBubble = document.createElement('div')
newSpeechBubble.classList.add('speech', 'speech-human')
chatbotConversation.appendChild(newSpeechBubble)
newSpeechBubble.textContent = userInput.value
userInput.value = ''
chatbotConversation.scrollTop = chatbotConversation.scrollHeight
})

async function fetchReply(){
const response = await openai.createChatCompletion({
model: 'gpt-4',
messages: conversationArr,
presence_penalty: 0,
frequency_penalty: 0.3
})
conversationArr.push(response.data.choices[0].message)
renderTypewriterText(response.data.choices[0].message.content)
}

function renderTypewriterText(text) {
const newSpeechBubble = document.createElement('div')
newSpeechBubble.classList.add('speech', 'speech-ai', 'blinking-cursor')
chatbotConversation.appendChild(newSpeechBubble)
let i = 0
const interval = setInterval(() => {
newSpeechBubble.textContent += text.slice(i-1, i)
if (text.length === i) {
clearInterval(interval)
newSpeechBubble.classList.remove('blinking-cursor')
}
i++
chatbotConversation.scrollTop = chatbotConversation.scrollHeight
}, 50)
}
Console
/index.html
-3:48