Explorer
project
images
env.js
index.css
index.html
index.js
Dependencies
openai@3.2.1
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 chatbotConversation = document.getElementById('chatbot-conversation')
let conversationStr = ''
document.addEventListener('submit', (e) => {
e.preventDefault()
const userInput = document.getElementById('user-input')
conversationStr += userInput.value
console.log(conversationStr)
// 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({
/*
Challenge:
1. Swap out the model for your fine-tuned model.
2. Swap 'messages' for 'prompt' and update the reference
to the old conversationArr.
3. Test.
*/
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)
}