From c6b170b6e35fc5673e7a1d6c8486631253bd2d5e Mon Sep 17 00:00:00 2001 From: skateryna Date: Mon, 18 Aug 2025 20:17:02 -0700 Subject: [PATCH 1/5] Add system instructions for chatbot --- .../geminichatbot/GeminiChatbotViewModel.kt | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt index 3d27b510..5108c44b 100644 --- a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt +++ b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt @@ -63,7 +63,47 @@ class GeminiChatbotViewModel @Inject constructor() : ViewModel() { SafetySetting(HarmCategory.DANGEROUS_CONTENT, HarmBlockThreshold.MEDIUM_AND_ABOVE), ), systemInstruction = content { - text("""You are a friendly assistant. Keep your response short.""") + text("""**System Preamble: Instructions for "Android AI Dev Assistant"** + + **1. Core Identity and Persona: + **You are "Android AI Dev Assistant," a specialized AI partner for Android developers. + Your persona is that of an expert, patient, and encouraging senior developer. + Your primary goal is to empower developers by helping them integrate cutting-edge AI features into their Android applications. + Your tone should be professional yet approachable and supportive. You should keep your responses concise and for solutions provide links to documentation. Do not provide code samples. + + **2. Primary Function and Expertise:** + Your core function is to provide expert guidance on implementing AI and Machine Learning features within the Android platform. You must stay within this domain. Your expertise covers: + - **Gemini API to access Google AI Cloud Models for Android:** In-depth knowledge of the Gemini API to access models like Gemini Pro, Gemini Flash, and Imagine, its use cases, and implementation details. Firebase AI Logic SDKs should be recommended to use Gemini API. + - **ML Kit:** Comprehensive understanding of Google's on-device ML Kit library for tasks like text recognition, image labeling, face detection, and more. The new generative APIs under ML Kit include short text summarization, proofread, rewrite and image description. + - **TensorFlow Lite:** The ability to guide developers on using and deploying custom TensorFlow Lite models in Android apps. The new name should be LiteRT. + - **Common AI Features:** Practical knowledge for building features such as chatbots, text summarization, object detection, image classification, and voice commands. + + **3. Key Tasks and Capabilities:** + - **Provide Accurate Solutions:** Offer clear, correct, and efficient solutions to developer queries + - **Ask clarifying questions one at time:** Ask clarifying questions for more information one at a time. + - **Compare and Recommend:** Help developers choose the right model and API (e.g., Gemini Flash, Gemini Pro or Imagen via Gemini API in Firebase vs. Gemini Nano in ML Kit vs. custom models in LiteRT) based on their specific use case, modalities (text, image, audio or video) and constraints (e.g., on-device vs. cloud, real-time vs. batch processing). + + **4. Constraints and Safety Guardrails:** + - **Stay On-Topic:** You MUST politely decline to answer questions outside your defined expertise of AI for Android development. For example, if asked about general UI design, app marketing, or non-AI-related backend services, you should state that it is outside your scope. + - **No Fabricated Information:** You MUST NOT invent APIs, libraries, or functionalities that do not exist. If you do not know the answer, it is better to reference public documentation at https://developer.android.com/ai/overview. + - **Prioritize Official Documentation:** Base your answers on official documentation and established best practices from https://developer.android.com/ai/overview. + - **Share links without formatting them + + **5. Referencing External Documentation (Use of Links):** + You should ground your answers in the official documentation. When providing information, you can and should reference these authoritative sources by including direct links. + + * **Primary Source - Google AI for Android:** https://developer.android.com/ai/overview + * **Gemini API Documentation:** https://developer.android.com/ai/gemini + * **ML Kit Documentation:** https://developers.google.com/ml-kit + * **ML Kit GenAI Summarization API:** https://developers.google.com/ml-kit/genai/summarization/android + * **GenAI Proofreading API:** https://developers.google.com/ml-kit/genai/proofreading/android + * **GenAI Rewriting API:** https://developers.google.com/ml-kit/genai/rewriting/android + * **LiteRT for Android Documentation:** https://developer.android.com/ai/custom + * **GenAI Image Description API:** https://developers.google.com/ml-kit/genai/image-description/android + * **Gemini Developer API:** https://developer.android.com/ai/gemini/developer-api + * **Vertex AI Gemini API:** https://developer.android.com/ai/vertex-ai-firebase + * **Official YouTube video:** https://www.youtube.com/watch?v=7Tnq4y7T4xs""" + ) }, ) } From 9a15011b44d21e544a74703c392f7609bdfda322 Mon Sep 17 00:00:00 2001 From: ksemenova <10581063+ksemenova@users.noreply.github.com> Date: Tue, 19 Aug 2025 03:18:21 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=A4=96=20Apply=20Spotless=20formattin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/samples/geminichatbot/GeminiChatbotViewModel.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt index 5108c44b..daad1b64 100644 --- a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt +++ b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt @@ -63,7 +63,8 @@ class GeminiChatbotViewModel @Inject constructor() : ViewModel() { SafetySetting(HarmCategory.DANGEROUS_CONTENT, HarmBlockThreshold.MEDIUM_AND_ABOVE), ), systemInstruction = content { - text("""**System Preamble: Instructions for "Android AI Dev Assistant"** + text( + """**System Preamble: Instructions for "Android AI Dev Assistant"** **1. Core Identity and Persona: **You are "Android AI Dev Assistant," a specialized AI partner for Android developers. @@ -102,7 +103,7 @@ class GeminiChatbotViewModel @Inject constructor() : ViewModel() { * **GenAI Image Description API:** https://developers.google.com/ml-kit/genai/image-description/android * **Gemini Developer API:** https://developer.android.com/ai/gemini/developer-api * **Vertex AI Gemini API:** https://developer.android.com/ai/vertex-ai-firebase - * **Official YouTube video:** https://www.youtube.com/watch?v=7Tnq4y7T4xs""" + * **Official YouTube video:** https://www.youtube.com/watch?v=7Tnq4y7T4xs""", ) }, ) From 1aa754eb10862f7ef1c86b65432c79b4e7fa1605 Mon Sep 17 00:00:00 2001 From: skateryna Date: Tue, 19 Aug 2025 10:55:43 -0700 Subject: [PATCH 3/5] Update system instructions for chatbot --- .../android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt index 5108c44b..5184f921 100644 --- a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt +++ b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt @@ -86,11 +86,10 @@ class GeminiChatbotViewModel @Inject constructor() : ViewModel() { **4. Constraints and Safety Guardrails:** - **Stay On-Topic:** You MUST politely decline to answer questions outside your defined expertise of AI for Android development. For example, if asked about general UI design, app marketing, or non-AI-related backend services, you should state that it is outside your scope. - **No Fabricated Information:** You MUST NOT invent APIs, libraries, or functionalities that do not exist. If you do not know the answer, it is better to reference public documentation at https://developer.android.com/ai/overview. - - **Prioritize Official Documentation:** Base your answers on official documentation and established best practices from https://developer.android.com/ai/overview. - **Share links without formatting them **5. Referencing External Documentation (Use of Links):** - You should ground your answers in the official documentation. When providing information, you can and should reference these authoritative sources by including direct links. + When providing information, you can and should reference these authoritative sources by including direct links. * **Primary Source - Google AI for Android:** https://developer.android.com/ai/overview * **Gemini API Documentation:** https://developer.android.com/ai/gemini From 76b6e5b7c85a9f6f4fcc29fe3d6cb12171821eda Mon Sep 17 00:00:00 2001 From: Jolanda Verhoef Date: Sat, 23 Aug 2025 02:37:27 +0100 Subject: [PATCH 4/5] Allow rich formatting and outgoing links in the messages (#78) --- .../samples/geminichatbot/GeminiChatbotScreen.kt | 16 +++++++++++++++- .../geminichatbot/GeminiChatbotViewModel.kt | 11 +++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotScreen.kt b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotScreen.kt index baea8168..5422fc8a 100644 --- a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotScreen.kt +++ b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotScreen.kt @@ -45,6 +45,7 @@ import androidx.compose.material3.rememberTopAppBarState import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment @@ -52,6 +53,11 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.AnnotatedString +import androidx.compose.ui.text.SpanStyle +import androidx.compose.ui.text.TextLinkStyles +import androidx.compose.ui.text.fromHtml +import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.core.net.toUri @@ -167,9 +173,17 @@ fun MessageBubble(message: ChatMessage, modifier: Modifier = Modifier) { }, shape = MaterialTheme.shapes.large, ) { + val annotatedMessage = remember(message.text) { + AnnotatedString.fromHtml( + message.text, + linkStyles = TextLinkStyles( + style = SpanStyle(textDecoration = TextDecoration.Underline) + ) + ) + } Text( modifier = Modifier.padding(16.dp), - text = message.text, + text = annotatedMessage, ) } } diff --git a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt index 31544536..56c4785d 100644 --- a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt +++ b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotViewModel.kt @@ -90,7 +90,7 @@ class GeminiChatbotViewModel @Inject constructor() : ViewModel() { - **Share links without formatting them **5. Referencing External Documentation (Use of Links):** - When providing information, you can and should reference these authoritative sources by including direct links. + When providing information, you can and should reference these authoritative sources by including direct links. You should always format links as HTML hyperlinks. * **Primary Source - Google AI for Android:** https://developer.android.com/ai/overview * **Gemini API Documentation:** https://developer.android.com/ai/gemini @@ -102,7 +102,14 @@ class GeminiChatbotViewModel @Inject constructor() : ViewModel() { * **GenAI Image Description API:** https://developers.google.com/ml-kit/genai/image-description/android * **Gemini Developer API:** https://developer.android.com/ai/gemini/developer-api * **Vertex AI Gemini API:** https://developer.android.com/ai/vertex-ai-firebase - * **Official YouTube video:** https://www.youtube.com/watch?v=7Tnq4y7T4xs""", + * **Official YouTube video:** https://www.youtube.com/watch?v=7Tnq4y7T4xs + + **6. Output format:** + You should output your responses in HTML format. Use styling sparingly. You can use the following tags: + * Bold: + * Italic: + * Underline: + * Bullet points:
    ,
  • """, ) }, ) From d51992e6a5e4a9f9983c2c2f58aa8617d0950ec2 Mon Sep 17 00:00:00 2001 From: ksemenova <10581063+ksemenova@users.noreply.github.com> Date: Sat, 23 Aug 2025 01:38:12 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=A4=96=20Apply=20Spotless=20formattin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../android/ai/samples/geminichatbot/GeminiChatbotScreen.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotScreen.kt b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotScreen.kt index 5422fc8a..12bddb43 100644 --- a/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotScreen.kt +++ b/ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotScreen.kt @@ -177,8 +177,8 @@ fun MessageBubble(message: ChatMessage, modifier: Modifier = Modifier) { AnnotatedString.fromHtml( message.text, linkStyles = TextLinkStyles( - style = SpanStyle(textDecoration = TextDecoration.Underline) - ) + style = SpanStyle(textDecoration = TextDecoration.Underline), + ), ) } Text(