Skip to main content

No project description provided

Project description

SJ-Firebase-AI - Gemini API using Firebase AI Logic

Build AI-powered mobile apps and features with the Gemini and Imagen models using Firebase AI Logic through SimpleJnius python bridge

Firebase AI Logic gives you access to the latest generative AI models from Google: the Gemini models and Imagen models.

If you need to call the Gemini API or Imagen API directly from your mobile app — rather than server-side — you can use the Firebase AI Logic client SDKs. These client SDKs are built specifically for use with mobile apps, offering security options against unauthorized clients as well as integrations with other Firebase services.

Need more flexibility or server-side integration? Genkit is Firebase's open-source framework for sophisticated server-side AI development with broad access to models from Google, OpenAI, Anthropic, and more. It includes more advanced AI features and dedicated local tooling.

Read more for here for key capabilities and other important information

Get started with the Gemini API using the Firebase AI Logic SDKs through SimpleJnius python bridge

This guide shows you how to get started making calls to the Gemini API directly from your app using the Firebase AI Logic client SDKs for your chosen platform.

Step 1: Set up a Firebase project and connect your app

  1. Sign into the Firebase console, and then select your Firebase project.

    Don’t already have a Firebase project?

    If you don’t already have a Firebase project, click the button to create a new Firebase project, and then use either of the following options:

    • Option 1: Create a wholly new Firebase project (and its underlying Google Cloud project automatically) by entering a new project name in the first step of the workflow.

    • Option 2: “Add Firebase” to an existing Google Cloud project by clicking Add Firebase to Google Cloud project (at bottom of page).
      In the first step of the workflow, start entering the project name of the existing project, and then select the project from the displayed list.

    Complete the remaining steps of the on-screen workflow to create a Firebase project.
    Note: When prompted, you do not need to set up Google Analytics to use the Firebase AI Logic SDKs.

  2. In the Firebase console, go to the Firebase AI Logic page.

  3. Click Get started to launch a guided workflow that helps you set up the required APIs and resources for your project.

  4. Select the Gemini API provider that you’d like to use with the Firebase AI Logic SDKs.
    Gemini Developer API is recommended for first-time users.
    You can always add billing or set up Vertex AI Gemini API later, if you’d like.

    • Gemini Developer APIbilling optional
      (available on the no-cost Spark pricing plan, and you can upgrade later if desired)

      The console will enable the required APIs and create a Gemini API key in your project.
      Do not add this Gemini API key into your app’s codebase.
      Learn more

    • Vertex AI Gemini APIbilling required
      (requires the pay-as-you-go Blaze pricing plan)

      The console will help you set up billing and enable the required APIs in your project.

  5. If prompted in the console’s workflow, follow the on-screen instructions to register your app and connect it to Firebase.

  6. Continue to the next step in this guide to add the SDK to your app.

Step 2: Add the SDK

With your Firebase project set up and your app connected to Firebase (see previous step), you can now add the Firebase AI Logic SDK to your app.

In your buildozer.spec file, add the following dependencies

requirements = ... other python dependencies, sjfirebaseai, simplejnius
android.gradle_dependencies = ... other android dependencies,com.google.firebase:firebase-ai,
   com.google.guava:guava:31.0.1-android,org.reactivestreams:reactive-streams:1.0.4
p4a.fork = simplejnius
p4a.branch = firebase

Step 3: Initialize the service and create a model instance

from sjfirebaseai.jclass import (
    FirebaseAI,
    GenerativeModelFutures,
    GenerativeBackend
)


# Initialize the Gemini Developer API backend service
# Create a `GenerativeModel` instance with a model that supports your use case
ai = FirebaseAI.getInstance(GenerativeBackend.vertexAI())  # or GenerativeBackend.googleAI()

# Use the GenerativeModelFutures Java compatibility layer which offers
# support for ListenableFuture and Publisher APIs
model = GenerativeModelFutures.from_(ai)

Note that depending on the capability you're using, you might not always create a GenerativeModel instance.

Also, after you finish this getting started guide, learn how to choose a model for your use case and app.

[!IMPORTANT] Before going to production, we strongly recommend implementing Firebase Remote Config so that you can remotely change the model name used in your app.

Step 4: Send a prompt request to a model

from sjfirebaseai.jclass import (
    FirebaseAI,
    GenerativeModelFutures,
    GenerativeBackend,
    Content
)
from simplejnius.guava.jinterface import FutureCallback
from simplejnius.guava.jclass import Futures


# Initialize the Gemini Developer API backend service
# Create a `GenerativeModel` instance with a model that supports your use case
ai = FirebaseAI.getInstance(GenerativeBackend.vertexAI())  # or GenerativeBackend.googleAI()

# Use the GenerativeModelFutures Java compatibility layer which offers
# support for ListenableFuture and Publisher APIs
model = GenerativeModelFutures.from_(ai)

# Provide a prompt that contains text
prompt = (
    Content.Builder()
    .addText("Write a story about a Kivy Framework.")
    .build()
)

# To generate text output, call generateContent with the text input
response = model.generateContent(prompt)

def on_success(result):
    print(result.getText())
    
def on_failure(error):
    print(error.getLocalizedMessage())

# To avoid garbage collection, make sure the FutureCallback instance is not stored in a 
# function scope variable
future_callback = FutureCallback(on_success, on_failure)
Futures.addCallback(response, future_callback)

Stream the response

from sjfirebaseai.jclass import (
    FirebaseAI,
    GenerativeModelFutures,
    GenerativeBackend,
    Content
)
from simplejnius.reactivestreams.jinterface import Subscriber


# Initialize the Gemini Developer API backend service
# Create a `GenerativeModel` instance with a model that supports your use case
ai = FirebaseAI.getInstance(GenerativeBackend.vertexAI())  # or GenerativeBackend.googleAI()

# Use the GenerativeModelFutures Java compatibility layer which offers
# support for ListenableFuture and Publisher APIs
model = GenerativeModelFutures.from_(ai)

# Provide a prompt that contains text
prompt = (
    Content.Builder()
    .addText("Write a story about a Kivy Framework.")
    .build()
)

# To stream generated text output, call generateContentStream with the text input
streaming_response = model.generateContentStream(prompt)

# Subscribe to partial results from the response
full_response = ""

def on_next(generate_content_response):
    global full_response
    chunk = generate_content_response.getText()
    full_response += chunk

# To avoid garbage collection, make sure the Subscriber instance is not stored in a 
# function scope variable
subscriber = Subscriber(
    {
        "on_next": on_next,
        "on_complete": lambda: print(full_response),
        "on_error": lambda error: None,
        "on_subscribe": lambda sub: None
    }
)
streaming_response.subscribe(subscriber)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sjfirebaseai-1.1.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sjfirebaseai-1.1.0-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file sjfirebaseai-1.1.0.tar.gz.

File metadata

  • Download URL: sjfirebaseai-1.1.0.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.0 CPython/3.13.8 Linux/6.14.0-36-generic

File hashes

Hashes for sjfirebaseai-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f0cc53d59d119e86ae9a4a22bdfe8d8de16fae0d15a962d24f857947cd67f949
MD5 8a9a7bbd07911bce9cc8aeba3c4eccaa
BLAKE2b-256 025fdb6388935d5840c6942d36f659a4e96a7678b1d085064861ef166a503b9a

See more details on using hashes here.

File details

Details for the file sjfirebaseai-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: sjfirebaseai-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.0 CPython/3.13.8 Linux/6.14.0-36-generic

File hashes

Hashes for sjfirebaseai-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b846a10d12ecdc0de9c6959a6f7235670474f7dbe7b58e07e29b8b6cc42ffc8
MD5 c19f45752f4b9ffef9c9c47a07d7df8a
BLAKE2b-256 e872b0e5c37ec59410096cad6ef3f8d70ed1838ad7b67b0970c5c4122f1b39ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page