إضافة ردود من إنشاء الذكاء الاصطناعي التوليدي إلى نموذج تطبيق SociaLite
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يوضّح نموذج تطبيق SociaLite كيفية استخدام واجهات برمجة تطبيقات منصّة Android
لتطبيق الميزات التي يتم نشرها بشكل شائع في تطبيقات الشبكات الاجتماعية
وتطبيقات الاتصالات. لقد دمجنا واجهة Gemini API باستخدام حزمة تطوير البرامج (SDK) لتكنولوجيات الذكاء الاصطناعي
Logic من Firebase لإظهار كيفية تنفيذ إمكانات محادثات chatbot في
تطبيقات Android الخاصة بك.
يستخدِم نموذج الرمز البرمجي هذا نموذج Gemini Flash السريع والفعّال من حيث التكلفة.
مزيد من المعلومات حول نماذج Gemini لتنفيذ برنامج محادثة آلي مستند إلى الذكاء الاصطناعي في
العرض التجريبي لتطبيق Socialite، استخدمنا تعليمات النظام
وظيفة واجهة برمجة التطبيقات Gemini API لتعديل سلوك النموذج. في هذه الحالة، نستخدم الطلب "يُرجى الردّ على محادثة المحادثة هذه مثل
قطة ودودة". يستخدم هذا الإصدار من SociaLite المزوّد بخدمة Gemini أيضًا ميزات التفاعل المتعدّد الوسائط
في النموذج للسماح لروبوتات الدردشة بالتفاعل مع الصور.
تنفيذ Gemini API
يمكن العثور على تنفيذ محادثات chatbot بشكل أساسي في فئة ChatRepository.
تتيح لك فئة GenerativeModel التفاعل مع Gemini API، والتي يتم
إنشاء مثيل لها على النحو التالي:
valgenerativeModel=GenerativeModel(// Set the model name to the latest Gemini model.modelName="gemini-2.0-flash-lite-001",// Set a system instruction to set the behavior of the model.systemInstruction=content{text("Please respond to this chat conversation like a friendly cat.")},)
في نطاق دالة coroutine، ابدأ محادثة من خلال تمرير pastMessages إلى startChat()
لضمان وصول النموذج إلى سجلّ المحادثات. يمنح ذلك
الروبوتات المتحكّمة بالذكاء الاصطناعي القدرة على الحفاظ على السياق وإنشاء ردود متّسقة تتكامل مع التبادلات السابقة.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Add generative AI responses the SociaLite sample app\n\nThe [SociaLite sample app](https://github.com/android/socialite) demonstrates how to use Android\nplatform APIs to implement features that are commonly deployed in social network\nand communications apps. We have integrated the Gemini API using the Firebase AI\nLogic SDK to demonstrate how chatbot capabilities can be implemented in your\nown Android apps.\n\nThis sample code uses Gemini Flash which fast and cost-effective.\n[Learn more about the Gemini models](https://firebase.google.com/docs/ai-logic/models). To implement an AI-driven chatbot in\nthe Socialite demo, we used the [*system instructions*](https://firebase.google.com/docs/ai-logic/system-instructions)\nfunctionality of the Gemini API to modify the behavior of the model. In this\ncase, we use the prompt \"Please respond to this chat conversation like a\nfriendly cat\". This Gemini-infused version of SociaLite also uses the multimodal\ncapabilities of the model to let the chatbot react to images.\n\nImplement the Gemini API\n------------------------\n\nThe chatbot implementation is primarily located in the `ChatRepository` class.\nThe `GenerativeModel` class lets you interact with the Gemini API, which is\ninstantiated as follows: \n\n val generativeModel = GenerativeModel(\n // Set the model name to the latest Gemini model.\n modelName = \"gemini-2.0-flash-lite-001\",\n // Set a system instruction to set the behavior of the model.\n systemInstruction = content {\n text(\"Please respond to this chat conversation like a friendly cat.\")\n },\n )\n\nIn a coroutine scope, initiate a chat by passing `pastMessages` to `startChat()`\nto ensure that the model has access to conversation history. This gives your\nchatbot the ability to maintain context and generate coherent responses that\nbuild on previous exchanges. \n\n val pastMessages = getMessageHistory(chatId)\n val chat = generativeModel.startChat(\n history = pastMessages,\n )\n\nUse the `sendMessage()` method to pass messages to the model.\n\nTest the AI chatbot\n-------------------\n\nYou can test it yourself by following these steps:\n\n1. Check out the code for the [SociaLite sample app](https://github.com/android/socialite) and open it in Android Studio.\n2. Set up a Firebase Project, connect your app to the *Gemini Developer API* by following [these steps](https://firebase.google.com/docs/ai-logic/get-started?platform=android&api=dev),\n3. Replace google-services.json with your own \\& Run `app` configuration,\n4. Sync and run your app.\n5. In the SociaLite app, tap **Settings** and then tap **AI Chatbot** so that the button label reads \"*AI Chatbot: enabled*\".\n\nYou are now ready to chat!\n\nAdditional resources\n--------------------\n\n[Learn more about the Firebase AI Logic SDK](/ai/gemini)."]]