-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.js
311 lines (278 loc) · 10 KB
/
ai.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
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
import axios from 'axios';
import dotenv from 'dotenv';
dotenv.config();
class BusinessAIAssistant {
constructor() {
this.COMPREHENSIVE_SERVICES = {
web_development: {
name: {
en: "Web Development",
fr: "Développement Web",
ht: "Devlopman Web"
},
categories: [
"E-commerce Websites",
"Corporate Websites",
"Portfolio Websites",
"Landing Pages",
"Blog Platforms"
],
pricing_tiers: [
{ tier: "Basic", price_range: "1000-3000 HTG", features: ["Single Page", "Responsive Design", "Basic SEO"] },
{ tier: "Standard", price_range: "3000-7000 HTG", features: ["Multi-Page", "Advanced SEO", "Contact Forms", "Basic Analytics"] },
{ tier: "Premium", price_range: "7000-15000 HTG", features: ["Custom Design", "E-commerce Integration", "Advanced Analytics", "SEO Optimization", "Performance Tuning"] }
],
technologies: ["React", "Vue.js", "WordPress", "Node.js", "PHP", "Laravel"]
},
digital_marketing: {
name: {
en: "Digital Marketing",
fr: "Marketing Digital",
ht: "Machenaj Dijital"
},
services: [
"Social Media Marketing",
"Google Ads Management",
"Content Marketing",
"Email Campaign Design",
"Brand Strategy Consulting"
],
pricing_range: "5000-50000 HTG"
},
mobile_app_development: {
name: {
en: "Mobile App Development",
fr: "Développement d'Applications Mobiles",
ht: "Devlopman Aplikasyon Mobil"
},
platforms: ["Android", "iOS", "Cross-Platform"],
pricing_tiers: [
{ tier: "Basic App", price_range: "10000-25000 HTG" },
{ tier: "Advanced App", price_range: "25000-75000 HTG" },
{ tier: "Enterprise Solution", price_range: "75000-250000 HTG" }
]
},
recruitment: {
name: {
en: "Talent Recruitment",
fr: "Recrutement de Talents",
ht: "Rekritman Talèn"
},
job_categories: [
"IT & Technology",
"Digital Marketing",
"Graphic Design",
"Business Development",
"Customer Support"
],
recruitment_link: "https://famous-tech-group.com/careers"
}
};
this.LANGUAGE_CONFIGURATIONS = {
'en': {
welcome_messages: [
"Welcome to RAIDEN AI! 🚀",
"Innovation starts here! How can we help you today?",
"Your digital transformation begins now!"
],
service_intro: "Explore our cutting-edge services:",
error_responses: [
"I'm processing your request...",
"Give me a moment to understand your needs.",
"Let me analyze that for you."
]
},
'fr': {
welcome_messages: [
"Bienvenue chez RAIDEN AI 🚀",
"L'innovation commence ici ! Comment pouvons-nous vous aider ?",
"Votre transformation digitale commence maintenant !"
],
service_intro: "Découvrez nos services de pointe :",
error_responses: [
"Je traite votre demande...",
"Donnez-moi un instant pour comprendre vos besoins.",
"Laissez-moi analyser cela pour vous."
]
},
'ht': {
welcome_messages: [
"Byenveni nan RAIDEN AI! 🚀",
"Inovasyon komanse isit! Koman nou ka ede ou?",
"Transformasyon dijital ou kòmanse kounye a!"
],
service_intro: "Eksplore sèvis avançe nou yo:",
error_responses: [
"Mwen ap trete demand ou...",
"Bay m yon moment pou m konprann bezwen ou.",
"Kite m analize sa pou ou."
]
}
};
this.INTENT_MATCHING = {
greeting: ['hello', 'hi', 'bonjou', 'hey', 'salut'],
services: ['service', 'sèvis', 'help', 'ede'],
pricing: ['price', 'cost', 'pri', 'kombyen'],
technical_support: ['problem', 'issue', 'technical', 'teknik'],
recruitment: ['job', 'career', 'work', 'travay', 'rekri']
};
}
detectLanguage(phoneNumber) {
const countryCodeMap = {
'1': 'en', '33': 'fr', '509': 'ht',
'228': 'fr', '242': 'fr', '221': 'fr',
'237': 'fr', '229': 'fr', '225': 'fr'
};
const countryCode = phoneNumber.substring(0, 3);
return countryCodeMap[countryCode] || 'en';
}
detectIntent(text) {
text = text.toLowerCase();
for (const [intent, keywords] of Object.entries(this.INTENT_MATCHING)) {
if (keywords.some(keyword => text.includes(keyword))) {
return intent;
}
}
return 'default';
}
async generateResponse(text, phoneNumber, isFirstInteraction) {
const lang = this.detectLanguage(phoneNumber);
const intent = this.detectIntent(text);
// First Interaction Logic
if (isFirstInteraction) {
const welcomeMessage = this.LANGUAGE_CONFIGURATIONS[lang].welcome_messages[
Math.floor(Math.random() * this.LANGUAGE_CONFIGURATIONS[lang].welcome_messages.length)
];
return {
text: `${welcomeMessage}\n\n` +
`${this.LANGUAGE_CONFIGURATIONS[lang].service_intro}\n` +
"1. 💻 Web Development\n" +
"2. 📱 Mobile Apps\n" +
"3. 📊 Digital Marketing\n" +
"4. 👥 Job Opportunities\n" +
"5. 📞 Contact Us"
};
}
// Intent-Based Responses
switch(intent) {
case 'greeting':
return this.handleGreeting(lang);
case 'services':
return this.handleServiceInquiry(text, lang);
case 'pricing':
return this.handlePricingInquiry(text, lang);
case 'technical_support':
return this.handleTechnicalSupport(text, lang);
case 'recruitment':
return this.handleRecruitment(lang);
default:
return await this.handleDefaultResponse(text, lang);
}
}
handleGreeting(lang) {
const greetings = {
'en': [
"Hello! How can I assist you today?",
"Hi there! What service are you interested in?"
],
'fr': [
"Bonjour ! Comment puis-je vous aider aujourd'hui ?",
"Salut ! Quel service vous intéresse ?"
],
'ht': [
"Bonjou! Koman m ka ede ou jodi a?",
"Alo! Ki sèvis ou enterese?"
]
};
return {
text: greetings[lang][Math.floor(Math.random() * greetings[lang].length)]
};
}
handleServiceInquiry(text, lang) {
const serviceDetails = Object.entries(this.COMPREHENSIVE_SERVICES).map(
([key, service]) => `• ${service.name[lang]}`
).join('\n');
return {
text: `🌐 Our Services:\n${serviceDetails}\n\nWhich one would you like to know more about?`
};
}
handlePricingInquiry(text, lang) {
const pricingInfo = Object.entries(this.COMPREHENSIVE_SERVICES)
.filter(([key, service]) => service.pricing_tiers || service.pricing_range)
.map(([key, service]) => {
if (service.pricing_tiers) {
return `• ${service.name[lang]}: ${service.pricing_tiers.map(t => t.price_range).join(', ')}`;
}
return `• ${service.name[lang]}: ${service.pricing_range}`;
}).join('\n');
return {
text: `💰 Pricing Overview:\n${pricingInfo}\n\nFor detailed quotes, please contact us.`
};
}
handleTechnicalSupport(text, lang) {
return {
text: "🛠️ Technical Support:\n" +
"Our team is ready to help you!\n" +
"Email: [email protected]\n" +
"Phone: +256709824720"
};
}
handleRecruitment(lang) {
const recruitmentService = this.COMPREHENSIVE_SERVICES.recruitment;
return {
text: `🌟 Job Opportunities:\n` +
`Categories: ${recruitmentService.job_categories.join(', ')}\n\n` +
`Apply now: ${recruitmentService.recruitment_link}`
};
}
async handleDefaultResponse(text, lang) {
try {
// Advanced contextual AI response generation
const aiResponse = await this.generateAIResponse(text, lang);
return { text: aiResponse };
} catch (error) {
console.error("AI Response Error:", error);
const errorResponses = this.LANGUAGE_CONFIGURATIONS[lang].error_responses;
return {
text: errorResponses[Math.floor(Math.random() * errorResponses.length)]
};
}
}
async generateAIResponse(text, lang) {
// Placeholder for advanced AI response generation
// You would integrate a more sophisticated AI service here
const systemPrompts = {
'en': "You are an AI assistant for a Haitian tech company. Provide helpful, professional responses.",
'fr': "Vous êtes un assistant IA pour une entreprise technologique haïtienne. Fournissez des réponses utiles et professionnelles.",
'ht': "Ou se yon asistan IA pou yon antrepriz teknoloji ayisyen. Bay repons ki ede ak ki pwofesyonèl."
};
try {
const response = await axios.post('https://api.openai.com/v1/chat/completions', {
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: systemPrompts[lang] },
{ role: "user", content: text }
]
}, {
headers: {
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json'
}
});
return response.data.choices[0].message.content;
} catch (error) {
console.error("Advanced AI Response Generation Failed:", error);
if (error.response && error.response.status === 429) {
return "I'm currently overloaded with requests. Please try again later.";
}
return "I'm having trouble understanding your request right now. Could you please rephrase?";
}
}
}
const businessAI = new BusinessAIAssistant();
export async function generateResponse(text, phoneNumber, isFirstInteraction) {
return await businessAI.generateResponse(text, phoneNumber, isFirstInteraction);
}
export async function callAIAPI(text, lang) {
return await businessAI.generateAIResponse(text, lang);
}