Hi there!
Any help would be eternally appreciated!
I’m just working locally so no security issues to be concerned about just yet - but if I update this code to use 3.5 turbo or paste the code provided by 3.5’s playground it won’t work. I’m no expert and I’ve definitely thrown myself in at the deep end. I have picked it apart many times but can’t seem to get anywhere. Thanks for reading!
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
const conversationDiv = document.getElementById("conversation");
const inputForm = document.getElementById("input-form");
const userInput = document.getElementById("user-input");
inputForm.addEventListener("submit", (e) => {
e.preventDefault();
const message = userInput.value.trim();
if (message) {
addMessageToConversation("user", message);
userInput.value = "";
sendMessageToGPT(message);
}
});
function addMessageToConversation(sender, message) {
const messageDiv = document.createElement("div");
messageDiv.className = "message";
messageDiv.innerHTML = `<span class="${sender}">${sender === "user" ? "You" : "ChatGPT"}:</span> ${message}`;
conversationDiv.appendChild(messageDiv);
conversationDiv.scrollTop = conversationDiv.scrollHeight;
}
async function sendMessageToGPT(message) {
const apiKey = "APIKEY";
const apiUrl = "https://api.openai.com/v1/completions";
try {
const response = await axios.post(apiUrl, {
"model": "text-davinci-002",
"prompt": message,
"temperature": 0.9,
"max_tokens": 150,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0.6,
"stop": [" Human:", " AI:"],
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
}
});
debugger;
const data = response.data.choices[0].text.trim();
console.log(data);
addMessageToConversation("gpt", data);
} catch (error) {
console.error(error);
addMessageToConversation("gpt", "Error: Unable to fetch response, please come back later.");
}
}
</script>
P.s APIkey removed for this post.
Many thanks!
G