-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.js
More file actions
68 lines (55 loc) · 2.38 KB
/
chat.js
File metadata and controls
68 lines (55 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const apiKey= "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";// TODO: Add your key
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: apiKey,
});
const openai = new OpenAIApi(configuration);
const inputElement = document.getElementById('bot_prompts"');
const sendButton = document.getElementById('send');
const BotResponse = async (text) =>{
const message = [{
role: "system", content: `You are a college admissions officer. Do not include information about being an AI.
You will guide the student through admissions process and deciding the best program and university for the student.`
}, {
role: "user", content: text
}];
// Make a request to the OpenAI API
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: message,
temperature: 0.2,
max_tokens: 2000,
n: 1
});
const messageOutput = completion?.data?.choices?.[0]?.message?.content;
console.log(messageOutput);
return messageOutput;
}
// create a function to handle sending a message and getting a response
const handleSend = async () => {
const message = inputElement.value;
// create a new message bubble for the user's message
const userBubble = document.createElement('div');
userBubble.classList.add('chat-message', 'user-message');
userBubble.innerText = message;
document.getElementById('chat-body').appendChild(userBubble);
// clear the input field
inputElement.value = '';
// send the message to the bot and get the response
const answer = await BotResponse(message);
// create a new message bubble for the bot's response
const botBubble = document.createElement('div');
botBubble.classList.add('chat-message', 'bot-message');
botBubble.innerText = answer;
document.getElementById('chat-body').appendChild(botBubble);
// scroll to the bottom of the chat window
document.getElementById('chat-body').scrollTop = document.getElementById('chat-body').scrollHeight;
};
// add an event listener to the send button
sendButton.addEventListener('click', handleSend);
// add an event listener to the input field to allow sending a message by pressing enter
inputElement.addEventListener('keypress', async (event) => {
if (event.key === 'Enter') {
await handleSend();
}
});