-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
196 lines (163 loc) · 5.61 KB
/
scripts.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const chatBox = document.querySelector('.chat-box');
const messageInput = document.querySelector('.message-input');
const sendButton = document.querySelector('.send-button');
let lastMessageTime = 0;
let mode = 'gpt';
function appendMessage(message, role, profilePicture) {
const newMessage = document.createElement('div');
newMessage.classList.add('message', `${role}-message`);
newMessage.innerHTML = `<img src="${profilePicture}" alt="${role} Profile" class="profile-picture">
<div class="message-content">${message}</div>`;
chatBox.appendChild(newMessage);
chatBox.scrollTop = chatBox.scrollHeight;
}
async function getAiResponse(userMessage) {
const thinkingMessage = displayThinkingMessage();
try {
let apiKey = '<apikey here>';
let apiUrl = 'https://api.openai.com/v1/chat/completions';
let requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify({
messages: [{ role: 'user', content: userMessage }],
model: 'gpt-3.5-turbo',
max_tokens: 4000,
temperature: 1,
frequency_penalty: 0.5,
}),
};
if (mode === 'dalle') {
apiKey = '<apikey here>';
apiUrl = 'https://api.openai.com/v1/images/generations';
requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: 'dall-e-3',
prompt: userMessage.replace('dallecreate', '').trim(),
size: '1024x1024',
quality: 'hd',
style: 'natural',
n: 1,
}),
};
}
const response = await fetch(apiUrl, requestOptions);
const data = await response.json();
let aiResponse = [];
if (mode === 'gpt') {
aiResponse = data.choices?.[0]?.message?.content ? [data.choices[0].message.content] : [];
} else {
if (Array.isArray(data.data)) {
aiResponse = data.data.map(item => item.url);
} else if (data.url) {
aiResponse = [data.url];
}
}
chatBox.removeChild(thinkingMessage);
if (aiResponse.length > 0) {
if (mode === 'gpt') {
appendAiMessage(aiResponse[0]);
} else {
appendDalleImages(aiResponse);
}
} else {
console.error('Invalid AI response format:', data);
appendAiMessage('An error occurred. Please try again later.');
}
} catch (error) {
console.error('Error:', error);
chatBox.removeChild(thinkingMessage);
appendAiMessage('An error occurred. Please try again later.');
} finally {
enableInput();
}
}
function appendDalleImages(images) {
const aiMessageContainer = document.createElement('div');
aiMessageContainer.classList.add('ai-message', 'message');
const profilePicture = 'https://flan.cafe/chatbot/ai-pfp2.png';
images.forEach((imageUrl) => {
const imageElement = document.createElement('img');
imageElement.src = imageUrl;
imageElement.classList.add('dalle-image');
imageElement.style.width = '300px';
imageElement.style.height = '300px';
imageElement.style.borderRadius = '10px';
imageElement.dataset.original = imageUrl;
aiMessageContainer.appendChild(imageElement);
});
const profilePictureElement = document.createElement('img');
profilePictureElement.src = profilePicture;
profilePictureElement.alt = 'AI Profile';
profilePictureElement.classList.add('profile-picture');
aiMessageContainer.appendChild(profilePictureElement);
chatBox.appendChild(aiMessageContainer);
chatBox.scrollTop = chatBox.scrollHeight;
}
function sendUserMessage() {
const userMessage = messageInput.value.trim();
if (userMessage.toLowerCase().includes('dallecreate')) {
mode = 'dalle';
} else {
mode = 'gpt';
}
if (userMessage !== '') {
appendUserMessage(userMessage);
getAiResponse(userMessage);
messageInput.value = '';
}
}
function displayThinkingMessage() {
const thinkingMessageContainer = document.createElement('div');
thinkingMessageContainer.classList.add('ai-message', 'message');
const profilePicture = document.createElement('img');
profilePicture.src = 'https://flan.cafe/chatbot/ai-pfp2.png';
profilePicture.alt = 'AI Profile';
profilePicture.classList.add('profile-picture');
const thinkingMessage = document.createElement('div');
thinkingMessage.classList.add('message-content', 'thinking-message');
if (mode === 'dalle') {
thinkingMessage.textContent = 'Generating an image for you. Please wait...';
} else {
thinkingMessage.textContent = 'Generating a response for you. Please wait...';
}
thinkingMessageContainer.appendChild(profilePicture);
thinkingMessageContainer.appendChild(thinkingMessage);
chatBox.appendChild(thinkingMessageContainer);
chatBox.scrollTop = chatBox.scrollHeight;
disableInput();
return thinkingMessageContainer;
}
function removeThinkingMessage(thinkingMessageContainer) {
thinkingMessageContainer.remove();
enableInput();
}
function appendUserMessage(message) {
const customProfilePicture = localStorage.getItem('customProfilePicture');
appendMessage(message, 'user', customProfilePicture || 'https://flan.cafe/chatbot/user-pfp.png');
}
function appendAiMessage(message) {
appendMessage(message, 'ai', 'https://flan.cafe/chatbot/ai-pfp2.png');
}
function disableInput() {
messageInput.disabled = true;
sendButton.disabled = true;
}
function enableInput() {
messageInput.disabled = false;
sendButton.disabled = false;
}
sendButton.addEventListener('click', sendUserMessage);
messageInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
sendUserMessage();
}
});