Thêm câu trả lời của AI tạo sinh vào ứng dụng mẫu SociaLite
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Ứng dụng mẫu SociaLite minh hoạ cách sử dụng các API nền tảng Android để triển khai các tính năng thường được triển khai trong ứng dụng mạng xã hội và ứng dụng liên lạc. Chúng tôi đã tích hợp API Gemini bằng cách sử dụng SDK Logic AI của Firebase để minh hoạ cách triển khai các tính năng của chatbot trong ứng dụng Android của riêng bạn.
Mã mẫu này sử dụng Gemini Flash nhanh chóng và tiết kiệm chi phí.
Tìm hiểu thêm về các mô hình Gemini. Để triển khai chatbot do AI điều khiển trong bản minh hoạ Socialite, chúng tôi đã sử dụng chức năng hướng dẫn hệ thống của API Gemini để sửa đổi hành vi của mô hình. Trong trường hợp này, chúng ta sử dụng câu lệnh "Vui lòng trả lời cuộc trò chuyện này như một chú mèo thân thiện". Phiên bản SociaLite tích hợp Gemini này cũng sử dụng các tính năng đa phương thức của mô hình để cho phép chatbot phản ứng với hình ảnh.
Triển khai Gemini API
Quá trình triển khai chatbot chủ yếu nằm trong lớp ChatRepository.
Lớp GenerativeModel cho phép bạn tương tác với Gemini API. Lớp này được tạo bản sao như sau:
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.")},)
Trong phạm vi coroutine, hãy bắt đầu cuộc trò chuyện bằng cách truyền pastMessages đến startChat() để đảm bảo rằng mô hình có quyền truy cập vào nhật ký trò chuyện. Điều này giúp chatbot duy trì ngữ cảnh và tạo ra các câu trả lời mạch lạc dựa trên các lượt trao đổi trước đó.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],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)."]]