myavana-bot-test-core 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env +19 -0
- package/index.js +4 -0
- package/package.json +1 -1
- package/src/postResponseProcessing.js +83 -2
- package/src/utils.js +65 -4
package/.env
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# PostgreSQL
|
|
2
|
+
DB_HOST=34.55.140.120
|
|
3
|
+
DB_USER=myavana
|
|
4
|
+
DB_PASSWORD=higibertigibet
|
|
5
|
+
DB_NAME=sienna-naturals-custom-bot
|
|
6
|
+
DB_PORT=5432
|
|
7
|
+
|
|
8
|
+
# Redis
|
|
9
|
+
REDIS_HOST=redis-15330.c1.us-central1-2.gce.redns.redis-cloud.com
|
|
10
|
+
REDIS_PORT=15330
|
|
11
|
+
REDIS_PASSWORD=OyLRjkTPuCGzQ2mNzA3Uhx5HIqWza8QC
|
|
12
|
+
REDIS_USERNAME=default
|
|
13
|
+
|
|
14
|
+
#GENKIT
|
|
15
|
+
GENKIT_API_KEY=AIzaSyBB5ZYwktOFI3R3j_vs8U7CxwKgS3XNgM0
|
|
16
|
+
|
|
17
|
+
#OpenAI
|
|
18
|
+
OPENAI_API_KEY=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
|
|
19
|
+
|
package/index.js
CHANGED
|
@@ -21,8 +21,12 @@ module.exports = {
|
|
|
21
21
|
postResponseProcessing: require('./src/postResponseProcessing').postResponseProcessing,
|
|
22
22
|
saveHairIssue: require('./src/hairIssues').saveHairIssue,
|
|
23
23
|
getHairIssuesForUser: require('./src/hairIssues').getHairIssuesForUser,
|
|
24
|
+
postResponseProductCheck: require('./src/postResponseProcessing').postResponseProductCheck,
|
|
24
25
|
|
|
25
26
|
// Utilities
|
|
26
27
|
getAllProducts: require('./src/utils').getAllProducts,
|
|
27
28
|
getAllFaqs: require('./src/utils').getAllFaqs,
|
|
29
|
+
getAllYoutubeVideos: require('./src/utils').getAllYoutubeVideos,
|
|
30
|
+
getAllTestimonials: require('./src/utils').getAllTestimonials,
|
|
31
|
+
getAdditionalInstructions: require('./src/utils').getAdditionalInstructions
|
|
28
32
|
};
|
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ const DatabaseSessionStore = require("./session");
|
|
|
5
5
|
const { pgClient, redisClient } = require('./database');
|
|
6
6
|
const {generateAndSaveSummary, saveConversationSummary, saveConversation} = require("./conversation");
|
|
7
7
|
const {saveHairIssue} = require("./hairIssues");
|
|
8
|
+
const {getProductByName, addProductRecommendation} = require("./utils");
|
|
8
9
|
|
|
9
10
|
const postProcessingPrompt = (chatHistory, message) => {
|
|
10
11
|
return `
|
|
@@ -313,7 +314,7 @@ const updateUserProfile = async (userId, updates) => {
|
|
|
313
314
|
}
|
|
314
315
|
};
|
|
315
316
|
|
|
316
|
-
const postResponseProcessing = async (postProcessData, userId, output, allchatsSummary, conversationId, chatHistory )=>{
|
|
317
|
+
const postResponseProcessing = async (postProcessData, userId, output, allchatsSummary, conversationId, chatHistory, botMessage )=>{
|
|
317
318
|
if (postProcessData.user_profile_updates) {
|
|
318
319
|
// TODO: Update user profile in database based on postProcessData.user_profile_updates
|
|
319
320
|
console.log("User profile updates to be applied:", postProcessData.user_profile_updates);
|
|
@@ -331,14 +332,94 @@ const postResponseProcessing = async (postProcessData, userId, output, allchatsS
|
|
|
331
332
|
if (!allchatsSummary) {
|
|
332
333
|
await generateAndSaveSummary(userId, conversationId, chatHistory);
|
|
333
334
|
}
|
|
335
|
+
const recommendedProductsData = await postResponseProductCheck(chatHistory, botMessage); // Get array of product objects
|
|
336
|
+
//console.log("Recommended Products (with reasons):", recommendedProductsData);
|
|
337
|
+
|
|
338
|
+
if (recommendedProductsData && recommendedProductsData.length > 0) {
|
|
339
|
+
// --- Loop through recommended products and save to DB (Corrected for reasons) ---
|
|
340
|
+
for (const productRecommendation of recommendedProductsData) { // Loop through objects
|
|
341
|
+
const { productName, reason } = productRecommendation; // Extract productName and reason
|
|
342
|
+
const product = await getProductByName(productName); // Lookup product by name
|
|
343
|
+
|
|
344
|
+
if (product) {
|
|
345
|
+
await addProductRecommendation(userId, conversationId, product.product_id, reason); // Pass the reason to addProductRecommendation
|
|
346
|
+
} else {
|
|
347
|
+
console.warn(`Product "${productName}" not found in database, cannot save recommendation.`);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
334
351
|
|
|
335
352
|
await saveConversationSummary(conversationId, userId, allchatsSummary);
|
|
336
353
|
return await saveConversation(conversationId, userId, chatHistory, allchatsSummary);
|
|
337
354
|
};
|
|
338
355
|
|
|
356
|
+
const postResponseProductCheckPrompt = async (chatHistory, message) => {
|
|
357
|
+
return `
|
|
358
|
+
Analyze the following chatbot conversation history and the bot's last response to identify if any specific products were recommended to the user.
|
|
359
|
+
|
|
360
|
+
Chat History:
|
|
361
|
+
${JSON.stringify(chatHistory, null, 2)}
|
|
362
|
+
|
|
363
|
+
Bot's Last Response:
|
|
364
|
+
${JSON.stringify(message, null, 2)}
|
|
365
|
+
|
|
366
|
+
Identify and list the names of the products recommended in the bot's last response AND provide a brief reason for each recommendation based on the conversation.
|
|
367
|
+
If no products were explicitly recommended, return an empty list.
|
|
368
|
+
|
|
369
|
+
Response Format:
|
|
370
|
+
[
|
|
371
|
+
{ "productName": "Product Name 1", "reason": "Reason for recommendation 1" },
|
|
372
|
+
{ "productName": "Product Name 2", "reason": "Reason for recommendation 2" },
|
|
373
|
+
...
|
|
374
|
+
] or [] if no products recommended.
|
|
375
|
+
`;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const postResponseProductCheck = async (chatHistory, message) => {
|
|
379
|
+
const extractionPrompt = await postResponseProductCheckPrompt(chatHistory, message);
|
|
380
|
+
//console.log('Extraction prompt for product check (with reason): ', extractionPrompt); // Updated log message
|
|
381
|
+
try {
|
|
382
|
+
const chat = ai.chat({
|
|
383
|
+
model: gemini15Flash, // or gemini20FlashExp
|
|
384
|
+
config: {
|
|
385
|
+
temperature: 0.9,
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
const { output } = await chat.send(extractionPrompt);
|
|
389
|
+
console.log("Raw post-processing output for product check (with reason):", JSON.stringify(output)); // Updated log
|
|
390
|
+
|
|
391
|
+
if (!output) {
|
|
392
|
+
console.warn("Post-processing output for product check (with reason) is empty. Returning default empty array."); // Updated log
|
|
393
|
+
return [];
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
try {
|
|
397
|
+
// Attempt to parse the output as JSON (array of product objects)
|
|
398
|
+
const recommendedProductsData = JSON.parse(output); // Renamed variable
|
|
399
|
+
if (Array.isArray(recommendedProductsData)) {
|
|
400
|
+
return recommendedProductsData; // Return the array of product objects
|
|
401
|
+
} else {
|
|
402
|
+
console.warn("Post-processing output is not an array. Returning empty array.");
|
|
403
|
+
return [];
|
|
404
|
+
}
|
|
405
|
+
} catch (jsonError) {
|
|
406
|
+
console.error("Error parsing post-processing JSON output:", jsonError);
|
|
407
|
+
console.warn("Returning default empty array due to JSON parsing error.");
|
|
408
|
+
return [];
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
} catch (error) {
|
|
412
|
+
console.error("Error in postResponseProductCheck:", error);
|
|
413
|
+
return [];
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
|
|
339
418
|
module.exports = {
|
|
340
419
|
postResponseProcessing,
|
|
341
420
|
postProcessConversation,
|
|
342
421
|
processBotResponse,
|
|
343
|
-
updateUserProfile
|
|
422
|
+
updateUserProfile,
|
|
423
|
+
postResponseProductCheckPrompt,
|
|
424
|
+
postResponseProductCheck
|
|
344
425
|
}
|
package/src/utils.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
// app/api/utils.js
|
|
2
|
+
const { pgClient } = require('./database'); // Or your DB connection
|
|
2
3
|
|
|
3
4
|
async function getAllProducts() {
|
|
4
5
|
try {
|
|
@@ -20,13 +21,73 @@ async function getAllFaqs() {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
async function
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
async function getAllYoutubeVideos() { // New function to get ALL YouTube Videos
|
|
25
|
+
try {
|
|
26
|
+
const result = await pgClient.query('SELECT * FROM youtube_videos');
|
|
27
|
+
return result.rows;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.error("Error fetching all YouTube videos:", error);
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function getAllTestimonials() { // New function to get ALL Testimonials
|
|
35
|
+
try {
|
|
36
|
+
const result = await pgClient.query('SELECT * FROM testimonials');
|
|
37
|
+
return result.rows;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error("Error fetching all testimonials:", error);
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function getAdditionalInstructions() { // New function to get ALL Bot Instructions
|
|
45
|
+
try {
|
|
46
|
+
const result = await pgClient.query('SELECT * FROM bot_instructions');
|
|
47
|
+
return result.rows;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error("Error fetching bot instructions:", error);
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function getHairIssuesForUser(userId){
|
|
54
|
+
//return empty for now
|
|
55
|
+
return []
|
|
56
|
+
}
|
|
57
|
+
async function getProductByName(productName) {
|
|
58
|
+
try {
|
|
59
|
+
const result = await pgClient.query('SELECT product_id FROM products WHERE product_name = $1', [productName]);
|
|
60
|
+
return result.rows[0]; // Returns the product object, or undefined if not found
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.error("Error fetching product by name:", error);
|
|
63
|
+
return null; // or undefined, depending on your preference
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Function to add product recommendation to the database
|
|
68
|
+
async function addProductRecommendation(userId, conversationId, productId, reasonForRecommending) {
|
|
69
|
+
try {
|
|
70
|
+
const query = `
|
|
71
|
+
INSERT INTO product_recommendations (user_id, conversation_id, product_id, reason_for_recommending)
|
|
72
|
+
VALUES ($1, $2, $3, $4)
|
|
73
|
+
RETURNING *;
|
|
74
|
+
`;
|
|
75
|
+
const values = [userId, conversationId, productId, reasonForRecommending];
|
|
76
|
+
const result = await pgClient.query(query, values);
|
|
77
|
+
return { success: true, recommendation: result.rows[0] }; // Return success and the new recommendation
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error("Error adding product recommendation:", error);
|
|
80
|
+
return { success: false, error: error.message };
|
|
81
|
+
}
|
|
26
82
|
}
|
|
27
83
|
|
|
28
84
|
module.exports = {
|
|
29
85
|
getAllProducts,
|
|
30
86
|
getAllFaqs,
|
|
87
|
+
getAllYoutubeVideos,
|
|
88
|
+
getAllTestimonials,
|
|
89
|
+
getAdditionalInstructions,
|
|
31
90
|
getHairIssuesForUser,
|
|
91
|
+
getProductByName,
|
|
92
|
+
addProductRecommendation
|
|
32
93
|
};
|