پاسخهای هوش مصنوعی مولد را به برنامه نمونه SociaLite اضافه کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
برنامه نمونه SociaLite نحوه استفاده از API های پلتفرم اندروید را برای پیاده سازی ویژگی هایی که معمولاً در شبکه های اجتماعی و برنامه های ارتباطی مستقر می شوند، نشان می دهد. ما Gemini API را با استفاده از Firebase AI Logic SDK ادغام کردهایم تا نشان دهیم که چگونه میتوان قابلیتهای چت بات را در برنامههای اندرویدی شما پیادهسازی کرد.
این کد نمونه از Gemini Flash استفاده می کند که سریع و مقرون به صرفه است. درباره مدل های جمینی بیشتر بدانید . برای پیادهسازی یک ربات گفتگوی مبتنی بر هوش مصنوعی در نسخه آزمایشی 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.")},)
در یک محدوده معمولی، یک چت را با ارسال pastMessages به startChat() آغاز کنید تا مطمئن شوید که مدل به تاریخچه مکالمه دسترسی دارد. این به ربات چت شما این توانایی را می دهد که زمینه را حفظ کند و پاسخ های منسجمی را ایجاد کند که بر اساس تبادلات قبلی است.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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)."]]