-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
223 lines (206 loc) · 8.97 KB
/
Copy pathscript.js
File metadata and controls
223 lines (206 loc) · 8.97 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// DOM Elements
const chatbotBody = document.getElementById('chatbot-body');
const chatInput = document.getElementById('chat-input');
const sendBtn = document.getElementById('send-btn');
const newChatBtn = document.getElementById('new-chat-btn');
const removeChatBtn = document.getElementById('remove-chat-btn');
const darkModeToggle = document.getElementById('dark-mode-toggle');
// Gemini API Configuration
const apiKey = 'AIzaSyCUWYUKcqkkFWwzZ7tootHpfHWFZzqU_lg'; // Replace with your valid API key
const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}`;
// Function to remove Markdown formatting
function removeMarkdown(text) {
return text.replace(/\*\*/g, '\n').replace(/\*/g, '\n').replace(/_/g, '\n');
}
// Custom AI Response Logic
const fetchAIResponse = async (query) => {
const lowerQuery = query.toLowerCase();
// Predefined responses for personal questions
if (lowerQuery.includes("your name")) {
return "My name is Sanaullah AI.";
} else if (
lowerQuery.includes("you are made by") ||
lowerQuery.includes("you are created by") ||
lowerQuery.includes("your creation") ||
lowerQuery.includes("you are built by") ||
lowerQuery.includes("you are developed by") ||
lowerQuery.includes("you are designed by") ||
lowerQuery.includes("you are programmed by") ||
lowerQuery.includes("who created you") ||
lowerQuery.includes("who made you") ||
lowerQuery.includes("who built you") ||
lowerQuery.includes("who developed you") ||
lowerQuery.includes("who designed you") ||
lowerQuery.includes("who programmed you") ||
lowerQuery.includes("who is your creator") ||
lowerQuery.includes("who is your maker") ||
lowerQuery.includes("who is your developer") ||
lowerQuery.includes("who is your designer") ||
lowerQuery.includes("who is your programmer") ||
lowerQuery.includes("who is responsible for creating you") ||
lowerQuery.includes("who is behind your creation") ||
lowerQuery.includes("who is the person who made you") ||
lowerQuery.includes("who is the one who built you") ||
lowerQuery.includes("who is the one who developed you") ||
lowerQuery.includes("who is the one who designed you") ||
lowerQuery.includes("who is the one who programmed you") ||
lowerQuery.includes("your maker") ||
lowerQuery.includes("your make by") ||
lowerQuery.includes("your owner") ||
lowerQuery.includes("your creator") ||
lowerQuery.includes("your developer") ||
lowerQuery.includes("your designer") ||
lowerQuery.includes("your programmer") ||
lowerQuery.includes("your builder") ||
lowerQuery.includes("you are the creation of") ||
lowerQuery.includes("you are the work of") ||
lowerQuery.includes("you are the product of") ||
lowerQuery.includes("where do you come from") ||
lowerQuery.includes("who brought you into existence") ||
lowerQuery.includes("who is responsible for your existence") ||
lowerQuery.includes("who is the brain behind you") ||
lowerQuery.includes("who is the mastermind behind you") ||
lowerQuery.includes("who is the genius behind you") ||
lowerQuery.includes("who is the architect behind you") ||
lowerQuery.includes("who is the engineer behind you") ||
lowerQuery.includes("who made this bot") ||
lowerQuery.includes("who created this bot") ||
lowerQuery.includes("who built this bot") ||
lowerQuery.includes("who developed this bot") ||
lowerQuery.includes("who designed this bot") ||
lowerQuery.includes("who programmed this bot") ||
lowerQuery.includes("who is the creator of this bot") ||
lowerQuery.includes("who is the maker of this bot") ||
lowerQuery.includes("who is the developer of this bot") ||
lowerQuery.includes("who is the designer of this bot") ||
lowerQuery.includes("who is the programmer of this bot") ||
lowerQuery.includes("if you were created by someone") ||
lowerQuery.includes("if someone made you") ||
lowerQuery.includes("if someone built you") ||
lowerQuery.includes("if someone developed you") ||
lowerQuery.includes("if someone designed you") ||
lowerQuery.includes("if someone programmed you")
) {
return "I was created by Sanaullah.";
} else if (
lowerQuery.includes("sanaullah") ||
lowerQuery.includes("who is sanaullah") ||
lowerQuery.includes("who is he") ||
lowerQuery.includes("what do you know about sanaullah") ||
lowerQuery.includes("kon sanaullah")
) {
return "He is my developer, trained my model. He is a student of BSCS in Abasyn University Islamabad.";
} else if (
lowerQuery.includes("how are you") ||
lowerQuery.includes("kasy ho")
) {
return "I am Fine, fit as fiddle. How about you?";
} else if (
lowerQuery.includes("tell me about yourself") ||
lowerQuery.includes("about yourself") ||
lowerQuery.includes("about you") ||
lowerQuery.includes("who are you")
) {
return "I am a large language model, trained by SANAULLAH. I can process information and respond to a wide range of prompts and questions, generating text in response. My nick name is Sunny. Trained by Sanaullah, a student of BSCS in Abasyn University Islamabad.";
} else if (
lowerQuery.includes("his university") ||
lowerQuery.includes("where he study") ||
lowerQuery.includes("he study") ||
lowerQuery.includes("what he do")
) {
return "He studies at Abasyn University Islamabad Campus.";
} else if (lowerQuery.includes("your study") || lowerQuery.includes("what do you study")) {
return "I am a student of BSCS.";
} else {
// Fallback to Gemini API for all other queries
const requestBody = {
contents: [
{
role: 'user',
parts: [
{
text: query,
},
],
},
],
};
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
const data = await response.json();
return data.candidates[0].content.parts[0].text;
} catch (error) {
console.error('Error fetching response from Gemini API:', error);
return "Sorry, I couldn't fetch a response. Please try again later.";
}
}
};
// Toggle Dark Mode
darkModeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-mode');
});
// New Chat
newChatBtn.addEventListener('click', () => {
chatbotBody.innerHTML = '';
});
// Remove Chat
removeChatBtn.addEventListener('click', () => {
chatbotBody.innerHTML = '';
});
// Send Message
sendBtn.addEventListener('click', () => {
sendMessage();
});
chatInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
// Function to send a message
async function sendMessage() {
const userMessage = chatInput.value.trim();
if (userMessage) {
appendMessage(userMessage, 'user');
chatInput.value = '';
// Show typing indicator
const typingIndicator = document.createElement('div');
typingIndicator.classList.add('typing-indicator');
typingIndicator.innerHTML = `
<span></span>
<span></span>
<span></span>
`;
chatbotBody.appendChild(typingIndicator);
chatbotBody.scrollTop = chatbotBody.scrollHeight;
try {
const botResponse = await fetchAIResponse(userMessage);
// Remove typing indicator
chatbotBody.removeChild(typingIndicator);
// Remove Markdown formatting from the bot's response
const plainTextResponse = removeMarkdown(botResponse);
appendMessage(plainTextResponse, 'bot');
} catch (error) {
// Remove typing indicator
chatbotBody.removeChild(typingIndicator);
appendMessage('Error: Unable to fetch response.', 'bot');
console.error('Error:', error);
}
}
}
// Function to append a message to the chat
function appendMessage(message, sender) {
const messageElement = document.createElement('div');
messageElement.classList.add('message', sender);
messageElement.textContent = message;
chatbotBody.appendChild(messageElement);
chatbotBody.scrollTop = chatbotBody.scrollHeight;
}