Skip to content

Commit

Permalink
added ai-test
Browse files Browse the repository at this point in the history
  • Loading branch information
xytrux committed Oct 20, 2023
1 parent d5108db commit 2d6da62
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions ai-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const chatContainer = document.getElementById("chat-container");
const chat = document.getElementById("chat");
const userInput = document.getElementById("user-input");

const apiKey = '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'; // Replace with your actual API key

function appendUserMessage(message) {
chat.innerHTML += `<div class="user-message">${message}</div>`;
}

function appendBotMessage(message) {
chat.innerHTML += `<div class="bot-message">${message}</div>`;
chat.scrollTop = chat.scrollHeight;
}

function simulateTyping() {
chat.innerHTML += `<div class="bot-typing">Mayzer is typing</div>`;
chat.scrollTop = chat.scrollHeight;
}

function removeTypingIndicator() {
const typingIndicator = document.querySelector(".bot-typing");
if (typingIndicator) {
chat.removeChild(typingIndicator);
}
}

function sendUserMessageToGPT3(userMessage) {
const apiUrl = 'https://free.churchless.tech/v1/chat/completions';

fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
prompt: userMessage,
max_tokens: 50, // Adjust max_tokens as needed
n: 1 // Number of responses to generate
})
})
.then(response => response.json())
.then(data => {
const botResponse = data.choices[0].text;
removeTypingIndicator();
appendBotMessage(botResponse);
})
.catch(error => {
console.error('Error:', error);
});
}

userInput.addEventListener("keyup", function(event) {
if (event.key === "Enter") {
const userMessage = userInput.value;
appendUserMessage(userMessage);
userInput.value = "";

// Simulate typing before bot response
simulateTyping();

// Send the user message to GPT-3.5
sendUserMessageToGPT3(userMessage);
}
});

0 comments on commit 2d6da62

Please sign in to comment.