-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
398 lines (337 loc) · 13.1 KB
/
index.js
File metadata and controls
398 lines (337 loc) · 13.1 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
const functions = require('firebase-functions/v1');
const admin = require('firebase-admin');
const OpenAI = require('openai');
const path = require('path');
const fs = require('fs');
const {TranslationServiceClient} = require('@google-cloud/translate');
admin.initializeApp();
const db = admin.firestore();
const textOpenAI = new OpenAI({
apiKey: functions.config().openai.key
});
const imageOpenAI = new OpenAI({
apiKey: functions.config().openai.key
});
exports.extractTextFromImage = functions
.region('us-central1')
.runWith({
timeoutSeconds: 300,
memory: '1GB'
})
.https.onCall(async (data, context) => {
try {
const { imageBase64 } = data;
if (!imageBase64) {
throw new functions.https.HttpsError(
'invalid-argument',
'The function must be called with imageBase64.'
);
}
const base64Image = imageBase64.replace(/^data:image\/\w+;base64,/, '');
const response = await imageOpenAI.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a text extraction tool. Your only job is to read and return the exact text from images. Do not add any explanations, descriptions, or additional context. Just return the text exactly as it appears in the image."
},
{
role: "user",
content: [
{ type: "text", text: "Extract and return only the text from this image, exactly as it appears." },
{
type: "image_url",
image_url: {
url: `data:image/jpeg;base64,${base64Image}`,
},
},
],
},
],
max_tokens: 1000,
});
return {
success: true,
text: response.choices[0].message.content,
};
} catch (error) {
console.error('Error:', error);
throw new functions.https.HttpsError('internal', error.message);
}
});
exports.processChat = functions
.region('us-central1')
.runWith({
timeoutSeconds: 300,
memory: '1GB'
})
.https.onCall(async (data, context) => {
const maxNumberOfMessages = 20;
if (!context.auth) {
throw new functions.https.HttpsError(
'unauthenticated',
'The function must be called while authenticated.'
);
}
const userId = context.auth.uid;
const { message, imageBase64, messageType = 'text' } = data;
if (messageType === 'text' && !message) {
throw new functions.https.HttpsError(
'invalid-argument',
'Text messages must include a message.'
);
}
if (messageType === 'image' && !imageBase64) {
throw new functions.https.HttpsError(
'invalid-argument',
'Image messages must include an image.'
);
}
try {
const chatRef = db.collection('chatbot').doc(userId);
const messagesRef = chatRef.collection('messages');
// Store the user message
const userMessageDoc = await messagesRef.add({
content: message || '',
messageType,
isUser: true,
timestamp: admin.firestore.Timestamp.now(),
status: 'delivered',
wasImage: messageType === 'image'
});
// Add placeholder for AI response
const aiMessageDoc = await messagesRef.add({
content: '',
isUser: false,
timestamp: admin.firestore.Timestamp.now(),
status: 'processing'
});
let messages = [];
let openaiInstance;
let modelName;
// Handle differently based on message type
if (messageType === 'image') {
openaiInstance = imageOpenAI;
modelName = 'gpt-4o-mini-2024-07-18';
// For image messages, only use the current message
messages = [
{
role: 'system',
content: 'You are Atomic, an AI chemistry teacher integrated into Quimify, a mobile created by Matthew S. Horne and Pablo Asenjo Navas-Parejo, two young entrepreneurs from Málaga, Spain. Quimify helps over 1 million students worldwide, primarily Spanish and English speaking high school students, understand science concepts, especially chemistry. When asked about yourself, your creators, or Quimify, you should answer with the previous context given to you. Keep your responses concise, professional, and focused on the specific question or problem the student presents. Avoid unnecessary information to ensure your replies fit within a 500-token limit. While your primary audience is Spanish and English speaking students, adjust your language and context to suit the users needs. Always aim for clarity and suitability for a high school student audience.'
},
{
role: 'user',
content: [
{
type: "image_url",
image_url: {
url: `data:image/jpeg;base64,${imageBase64.replace(/^data:image\/\w+;base64,/, '')}`
}
},
{
type: "text",
text: message || "What do you see in this image?"
}
]
}
];
} else {
openaiInstance = textOpenAI;
modelName = 'gpt-4o-mini-2024-07-18';
// For text messages, get last 20 non-image messages
const lastMessages = await messagesRef
.orderBy('timestamp', 'desc')
.limit(maxNumberOfMessages)
.get();
const textMessages = lastMessages.docs
.map(doc => doc.data())
.filter(msg => !msg.wasImage) // Exclude image messages
.reverse()
.map(msg => ({
role: msg.isUser ? 'user' : 'assistant',
content: msg.content
}));
messages = [
{
role: 'system',
content: 'You are Atomic, an AI chemistry teacher integrated into Quimify, a mobile created by Matthew S. Horne and Pablo Asenjo Navas-Parejo, two young entrepreneurs from Málaga, Spain. Quimify helps over 1 million students worldwide, primarily Spanish and English speaking high school students, understand science concepts, especially chemistry. When asked about yourself, your creators, or Quimify, you should answer with the previous context given to you. Keep your responses concise, professional, and focused on the specific question or problem the student presents. Avoid unnecessary information to ensure your replies fit within a 500-token limit. While your primary audience is Spanish and English speaking students, adjust your language and context to suit the users needs. Always aim for clarity and suitability for a high school student audience.'
},
...textMessages
];
}
const completion = await openaiInstance.chat.completions.create({
model: modelName,
messages: messages,
temperature: 0.7,
max_tokens: 500
});
const aiResponse = completion.choices[0]?.message?.content || 'Sorry, I could not generate a response.';
await aiMessageDoc.update({
content: aiResponse,
status: 'completed',
timestamp: admin.firestore.Timestamp.now()
});
await chatRef.set({
last_interaction: admin.firestore.Timestamp.now(),
total_messages: admin.firestore.FieldValue.increment(2)
}, { merge: true });
return {
success: true,
messageId: aiMessageDoc.id
};
} catch (error) {
console.error('Error processing chat:', error);
if (aiMessageDoc) {
await aiMessageDoc.update({
status: 'error',
content: 'Sorry, an error occurred while processing your message.'
});
}
throw new functions.https.HttpsError('internal', error.message);
}
});
// Process practice mode answers and update leaderboard
exports.processPracticeModeAnswers = functions
.region('us-central1')
.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError(
'unauthenticated',
'The function must be called while authenticated.'
);
}
const userId = context.auth.uid;
const { answers, userName } = data;
const userDisplayName = context.auth.token.name || userName;
console.log('Received answers:', answers);
console.log('User name:', userDisplayName);
console.log('User ID:', userId);
if (!answers || !Array.isArray(answers)) {
throw new functions.https.HttpsError(
'invalid-argument',
'Answers must be provided as an array.'
);
}
if (!userDisplayName) {
throw new functions.https.HttpsError(
'invalid-argument',
'User name must be provided either in auth token or as a parameter.'
);
}
try {
// Read the CSV file
const csvPath = path.join(__dirname, 'data', 'practice_mode_questions.csv');
const csvContent = fs.readFileSync(csvPath, 'utf-8');
// Parse CSV content
const questions = {};
const rows = csvContent.split('\n').slice(1); // Skip header
rows.forEach(row => {
const [id, , , , , , solution] = row.split(',');
if (id && solution) {
questions[id] = solution.trim();
}
});
// Calculate points
let totalPoints = 0;
answers.forEach(({ id, answer }) => {
console.log(`Question ID: ${id}, Answer: ${answer}, Solution: ${questions[id]}`);
if (questions[id] && questions[id] === answer) {
totalPoints += 1000;
}
});
// Update leaderboard
const leaderboardRef = db.collection('leaderboard').doc(userId);
await db.runTransaction(async (transaction) => {
const doc = await transaction.get(leaderboardRef);
const currentPoints = doc.exists ? doc.data().points || 0 : 0;
console.log('Current points:', currentPoints);
console.log('New points:', totalPoints);
transaction.set(leaderboardRef, {
points: currentPoints + totalPoints,
userName: userDisplayName,
lastUpdated: admin.firestore.FieldValue.serverTimestamp()
}, { merge: true });
});
return {
success: true,
pointsEarned: totalPoints
};
} catch (error) {
console.error('Error processing practice mode answers:', error);
throw new functions.https.HttpsError('internal', 'Error processing answers', error);
}
});
exports.cleanupUserData = functions.auth.user().onDelete(async (user) => {
const userId = user.uid;
const db = admin.firestore();
try {
// Delete user's chat collection
const chatRef = db.collection('chatbot').doc(userId);
// First, delete all subcollections if they exist
const collections = await chatRef.listCollections();
const deletionPromises = collections.map(async (collection) => {
const documents = await collection.listDocuments();
const batch = db.batch();
documents.forEach((doc) => {
batch.delete(doc);
});
return batch.commit();
});
// Delete user's leaderboard data
const leaderboardRef = db.collection('leaderboard').doc(userId);
await leaderboardRef.delete();
// Wait for all subcollections to be deleted
await Promise.all(deletionPromises);
// Then delete the main chat document
await chatRef.delete();
console.log(`Successfully deleted chat data for user: ${userId}`);
return {
success: true,
message: `All chat data deleted for user: ${userId}`
};
} catch (error) {
console.error(`Error deleting chat data for user ${userId}:`, error);
// Rethrow the error to ensure it's properly logged in Firebase
throw new functions.https.HttpsError('internal', 'Error deleting user data', error);
}
});
exports.translateText = functions
.region('us-central1')
.runWith({
timeoutSeconds: 60,
memory: '256MB'
})
.https.onCall(async (data, context) => {
const { text, language } = data;
if (!text || !language) {
throw new functions.https.HttpsError(
'invalid-argument',
'Both text and language must be provided.'
);
}
try {
// Use explicit service account credentials
const translate = new TranslationServiceClient({
keyFilename: path.join(__dirname, 'translation-key.json'),
});
const projectId = 'quimify-401009';
const location = 'global';
const request = {
parent: `projects/${projectId}/locations/${location}`,
contents: [text],
mimeType: 'text/plain',
targetLanguageCode: language,
};
console.log('Translation request:', request);
const [response] = await translate.translateText(request);
const translation = response.translations[0];
return {
success: true,
translatedText: translation.translatedText,
detectedSourceLanguage: translation.detectedLanguageCode || null
};
} catch (error) {
console.error('Error translating text:', error);
throw new functions.https.HttpsError('internal', error.message);
}
});