All skills
firebase avatar

/firebase-ai-logic-basics

@eca0362 official
by firebasefirebase/agent-skills461 stars
102

Official skill for integrating Firebase AI Logic (Gemini API) into web applications. Covers setup, multimodal inference, structured output, and security.

Use this Skill: https://skilld.dev/gh/firebase/agent-skills/firebase-ai-logic-basics

This session only. Nothing lands on disk.

referencesusage_patterns_android.md

≈1.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Firebase AI Logic on Android (Kotlin)

First, ensure you have initialized the Firebase App (see firebase-basics skill). Then, initialize the AI Logic service as below

0. Enable Firebase AI Logic via CLI

Before adding dependencies in your app, make sure you enable the AI Logic service in your Firebase Project using the Firebase CLI:

npx -y firebase-tools@latest init
# When prompted, select 'AI logic' to enable the Gemini API in your project.

1. Add Dependencies

In your module-level build.gradle.kts (usually app/build.gradle.kts), add the dependency for Firebase AI:

dependencies {
    // [AGENT] Fetch the latest available BoM version from https://firebase.google.com/support/release-notes/android before adding this
    implementation(platform("com.google.firebase:firebase-bom:<latest_bom_version>"))

    // Add the dependency for the Firebase AI library
    implementation("com.google.firebase:firebase-ai")
}

2. Initialize and Generate Content

In your Activity or Fragment, initialize the FirebaseAI service and generate content using a Gemini model:

import com.google.firebase.ai.FirebaseAI
import com.google.firebase.Firebase
import com.google.firebase.ai.ai

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize Firebase AI
        val ai = Firebase.ai

        // [AGENT] Replace "<latest_supported_model>" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
        val model = ai.generativeModel("<latest_supported_model>")

        // Generate content
        lifecycleScope.launch {
            try {
                val response = model.generateContent("Write a story about a magic backpack.")
                Log.d(TAG, "Response: ${response.text}")
            } catch (e: Exception) {
                Log.e(TAG, "Error generating content", e)
            }
        }
    }
}
Jetpack Compose (Modern)

Initialize inside a ComponentActivity and use setContent:

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.lifecycle.lifecycleScope
import com.google.firebase.Firebase
import com.google.firebase.ai.ai
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val ai = Firebase.ai
        // [AGENT] Replace with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
        val model = ai.generativeModel("<latest_supported_model>")
        
        lifecycleScope.launch {
            val response = model.generateContent("Hello Gemini!")
            setContent {
                MaterialTheme {
                    Text("AI Response: ${response.text}")
                }
            }
        }
    }
}

3. Multimodal Input (Text and Images)

Pass bitmap data along with text prompts:

val image1: Bitmap = ... // Load your bitmap
val image2: Bitmap = ...

val response = model.generateContent(
    content {
        image(image1)
        image(image2)
        text("Analyze these images for me. Compare these two items.")
    }
)
Log.d(TAG, response.text)

4. Chat Session (Multi-turn)

Maintain chat history automatically:

val chat = model.startChat(
    history = listOf(
        content("user") { text("Hello, I am a software engineer.") },
        content("model") { text("Hello! How can I help you today?") }
    )
)

lifecycleScope.launch {
    val response = chat.sendMessage("What should I learn next?")
    Log.d(TAG, response.text)
}

5. Streaming Responses

For faster display, stream the response:

lifecycleScope.launch {
    model.generateContentStream("Tell me a long story.")
        .collect { chunk ->
            print(chunk.text) // Update UI incrementally
        }
}

Source: SKILL.md on GitHub

No alerts13d3 checks · Risk SAFE
  • Gen Agent Trust Hub13d

    The skill provides legitimate instructions and code samples for integrating Firebase AI Logic into web, mobile, and Flutter applications. It correctly identifies security requirements such as App Check and encourages the use of managed secrets for CI/CD pipelines.

  • Socket13d

    No alerts

  • Snyk13d

    Risk: LOW · No issues

Signed by skilld at eca0362. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 2 weeks ago
version
1.0.1
metadata
{
  "category": "AiAndMachineLearning"
}

README badge

README badge for firebase/agent-skills/firebase-ai-logic-basics