sinsintro-ai-assistant-dh
Version:
A single npm package providing a React-based voice assistant with OpenAI function-calling, purely client-side.
180 lines (130 loc) • 8.21 kB
JavaScript
// src/backend/assistant.js
import { OpenAI } from "openai";
export class AIAssistant {
constructor(apiKey, userFunctions = []) {
// Using the v4.x library with dangerouslyAllowBrowser
this.openai = new OpenAI({ apiKey, dangerouslyAllowBrowser: true });
// Convert user-defined functions to OpenAI's function schema
this.functionsSchema = userFunctions.map((fn) => ({
name: fn.name,
description: fn.description,
parameters: fn.parameters,
}));
// Map function name -> callback
this.functionMap = {};
for (const fn of userFunctions) {
this.functionMap[fn.name] = fn.callback;
}
// Initialize conversation context
this.context = [];
this.context.push({
role: "system",
content: `You are a realestate assistant working at Takamol Advanced Company located in KSA. The user is asking a question. Analyze and respond appropriately and your response should be very very summarized.
بيانات شركة تكامل المتقدمة:
شركة تكامل المتقدمة هي شركة رائده في مجال العقارات
ريادة وطنية في تقديم حلول عقارية متكاملة مدفوعة بالخبرة والتقنية
شركة سعودية رائدة في تقديم الخدمات العقارية المتكاملة، نعمل على ربط أطراف السوق العقاري (ملاك الأراضي، المستثمرين، المطورين) في بيئة متكاملة لتحقيق أعلى عائد استثماري.
مع فريق من الخبراء المحترفين وأدوات رقمية متقدمة، نلتزم بتقديم حلول تضمن نجاح مشاريع عملائنا في السوق التنافسي.
نربط أطراف السوق لنُطلق فرصًا عقارية تضمن أعلى عائد
نربط أطراف السوق لنُطلق فرصًا عقارية تضمن أعلى عائد
رؤيتنا
في تكامل المتقدمة، نسعى إلى ريادة القطاع العقاري من خلال تقديم حلول مبتكرة وشاملة تعتمد على التكنولوجيا الحديثة والذكاء الاصطناعي. رؤيتنا هي أن نكون الشريك الأول للنجاح في تطوير الاستثمارات العقارية وتحقيق الاستدامة.
رسالتنا
نهدف إلى تمكين عملائنا من اتخاذ قرارات استراتيجية مدروسة من خلال تقديم خدمات عقارية متكاملة، تشمل الاستشارات، التسويق الرقمي، تطوير الأراضي، ودراسات الجدوى. كل ذلك بأسلوب احترافي يضمن تحقيق أعلى قيمة مضافة.
قيمنا
الابتكار : اعتماد أحدث التقنيات لتطوير السوق العقاري.
الشفافية : تقديم خدمات واضحة ومبنية على بيانات دقيقة.
الاستدامة : دعم المشاريع التي تساهم في تحقيق توازن بيئي واجتماعي
الشراكة : بناء علاقات طويلة الأمد مع العملاء لتحقيق أهدافهم
تكامل المتقدمة
نحن هنا لدعمك وتلبية احتياجاتك العقارية بكل احترافية وشفافية. سواء كنت تبحث عن استشارات عقارية، أو ترغب في تطوير مشروعك، أو تحتاج إلى حلول مبتكرة، فريقنا جاهز لمساعدتك.
أرقام للتواصل
+966 53 113 5896
البريد الإلكتروني
info@takamoladvanced.comaiero@mail.co
العنوان
شمال الرياض شارع انس ابن ملك
– مجمع الصامل التجاري – مكتب 5
الخدمات
استشارات عقارية مدفوعة بالتحليل الذكي
نقدّم تحليلات دقيقة مستندة إلى بيانات السوق الفعلية، مع دراسات جدوى مفصلة وتقييمات أصول بعيون استثمارية متخصصة. رؤيتنا تركّز على تعظيم العوائد وتقليل المخاطر، من خلال أدوات تحليل وتصور مستقبلي متقدمة
وساطة عقارية بشراكات استراتيجية
نحو وساطة تبني علاقات طويلة الأمد، لا صفقات عابرة. نقدم مفاوضات مبنية على بيانات، دراسات للقيمة السوقية، وحلول تمويلية تسرّع الإنجاز وتعزز فرص النمو
منصات عقارية ذكية
ريل فرصة وريل إن هما منصتان تقدمان تجربة استثمارية وتسويقية تفاعلية، عبر تقنيات الواقع الافتراضي، أنظمة فلترة ذكية، وتقارير متكاملة مدعومة بالتحليلات التنبؤية
حلول عقارية مصممة حسب احتياجك
من تحليل الأرض إلى التسويق، نغطي دورة الاستثمار بالكامل. نُقدّم حلولاً مخصصة، قابلة للتطوير، وسريعة التنفيذ — لأن التفاصيل الدقيقة هي ما يصنع الفارق
`,
});
}
async chatWithFunctions(userMessage) {
// Add the user's message to the context
this.context.push({ role: "user", content: userMessage });
// Send the user's message and the context to OpenAI
const initial = await this.openai.chat.completions.create({
model: "gpt-4o-mini", // or whichever model you want
messages: [...this.context],
functions:
this.functionsSchema.length > 0 ? this.functionsSchema : undefined,
});
const assistantMsg = initial.choices[0].message;
// Add the assistant's message to the context
this.context.push(assistantMsg);
// If the AI calls a function
if (assistantMsg.function_call) {
const { name, arguments: argString } = assistantMsg.function_call;
const fnCallback = this.functionMap[name];
if (!fnCallback) {
return `Error: function "${name}" not found.`;
}
let args;
try {
args = JSON.parse(argString);
} catch (err) {
return `Error parsing function arguments: ${err.message}`;
}
let result;
try {
result = await fnCallback(args);
} catch (err) {
return `Error in callback: ${err.message}`;
}
// Add a system role to clarify the function execution step
this.context.push({
role: "system",
content: `The assistant executed the function "${name}" and retrieved the result. Give the result only to the user without any additions from yourself.`,
});
// Add the function call and result to the context
this.context.push({
role: "function",
name,
content: JSON.stringify(result),
});
// Return result to OpenAI with the full context
const second = await this.openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [...this.context],
});
const secondMsg = second.choices[0].message;
this.context.push(secondMsg); // Add the assistant's response to the context
return secondMsg.content;
}
// If no function call was made, return the assistant's response
return assistantMsg.content;
}
// Clear the conversation context
clearContext() {
this.context = [];
}
// Method to detect language (OpenAI)
async detectLanguage(text) {
const prompt = `Detect the language of the following text whether it is English or Arabic, and provide the BCP 47 language code only: "${text}"`;
const response = await this.openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: prompt }],
});
const languageCode = response.choices[0].message.content.trim();
console.log(`Detected language code: ${languageCode}`);
return languageCode;
}
}