SociaLite örnek uygulamasına üretken yapay zeka yanıtları ekleme
Koleksiyonlar ile düzeninizi koruyun
İçeriği tercihlerinize göre kaydedin ve kategorilere ayırın.
SociaLite örnek uygulaması, sosyal ağ ve iletişim uygulamalarında yaygın olarak kullanılan özellikleri uygulamak için Android platform API'lerinin nasıl kullanılacağını gösterir. Sohbet robotu özelliklerinin kendi Android uygulamalarınıza nasıl uygulanabileceğini göstermek için Firebase AI Logic SDK'sını kullanarak Gemini API'yi entegre ettik.
Bu örnek kodda hızlı ve uygun maliyetli olan Gemini Flash kullanılır.
Gemini modelleri hakkında daha fazla bilgi edinin. Sosyalite demosunda yapay zeka destekli bir sohbet botu uygulamak için modelin davranışını değiştirmek üzere Gemini API'nin sistem talimatları işlevini kullandık. Bu durumda, "Lütfen bu sohbet görüşmesini dost canlısı bir kedi gibi yanıtlayın" isteminden yararlanırız. SociaLite'ın Gemini ile desteklenen bu sürümünde, chatbot'un görüntülere tepki vermesi için modelin çoklu formatlı özellikleri de kullanılır.
Gemini API'yi uygulama
Sohbet robotu uygulaması temel olarak ChatRepository sınıfında bulunur.
GenerativeModel sınıfı, Gemini API ile etkileşim kurmanıza olanak tanır. Bu sınıf aşağıdaki şekilde örneklendirilir:
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.")},)
Modelin görüşme geçmişine erişebildiğinden emin olmak için bir coroutine kapsamında pastMessages değerini startChat() değerine ileterek sohbet başlatın. Bu sayede sohbet botunuz bağlamı koruyabilir ve önceki etkileşimleri temel alan tutarlı yanıtlar oluşturabilir.
Bu sayfadaki içerik ve kod örnekleri, İçerik Lisansı sayfasında açıklanan lisanslara tabidir. Java ve OpenJDK, Oracle ve/veya satış ortaklarının tescilli ticari markasıdır.
Son güncelleme tarihi: 2025-07-27 UTC.
[[["Anlaması kolay","easyToUnderstand","thumb-up"],["Sorunumu çözdü","solvedMyProblem","thumb-up"],["Diğer","otherUp","thumb-up"]],[["İhtiyacım olan bilgiler yok","missingTheInformationINeed","thumb-down"],["Çok karmaşık / çok fazla adım var","tooComplicatedTooManySteps","thumb-down"],["Güncel değil","outOfDate","thumb-down"],["Çeviri sorunu","translationIssue","thumb-down"],["Örnek veya kod sorunu","samplesCodeIssue","thumb-down"],["Diğer","otherDown","thumb-down"]],["Son güncelleme tarihi: 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)."]]