Using the Gemini API, you can build freeform conversations across
multiple turns. The Vertex AI in Firebase SDK simplifies the process by managing
the state of the conversation, so unlike with generateContent()
(or generateContentStream()
), you don't have to store the conversation history
yourself.
Before you begin
If you haven't already, complete the
getting started guide, which describes how to
set up your Firebase project, connect your app to Firebase, add the SDK,
initialize the Vertex AI service, and create a GenerativeModel
instance.
Send a chat prompt request
To build a multi-turn conversation (like chat), start off by initializing the
chat by calling startChat()
. Then use
sendMessage()
to send a new user message, which
will also append the message and the response to the chat history.
There are two possible options for role
associated with the content in a
conversation:
user
: the role which provides the prompts. This value is the default for calls tosendMessage()
, and the function throws an exception if a different role is passed.model
: the role which provides the responses. This role can be used when callingstartChat()
with existinghistory
.
Learn how to choose a model and optionally a location appropriate for your use case and app.
Stream the response
Make sure that you've completed the Before you begin section of this guide before trying this sample.
You can achieve faster interactions by not waiting for the entire result from
the model generation, and instead use streaming to handle partial results.
To stream the response, call sendMessageStream()
.
What else can you do?
- Learn how to count tokens before sending long prompts to the model.
- Set up Cloud Storage for Firebase so that you can include large files in your multimodal requests and have a more managed solution for providing files in prompts. Files can include images, PDFs, video, and audio.
- Start thinking about preparing for production, including setting up Firebase App Check to protect the Gemini API from abuse by unauthorized clients. Also, make sure to review the production checklist.
Try out other capabilities
- Generate text from text-only prompts.
- Generate text from multimodal prompts (including text, images, PDFs, video, and audio).
- Generate structured output (like JSON) from both text and multimodal prompts.
- Generate images from text prompts.
- Use function calling to connect generative models to external systems and information.
Learn how to control content generation
- Understand prompt design, including best practices, strategies, and example prompts.
- Configure model parameters like temperature and maximum output tokens (for Gemini) or aspect ratio and person generation (for Imagen).
- Use safety settings to adjust the likelihood of getting responses that may be considered harmful.
Learn more about the supported models
Learn about the models available for various use cases and their quotas and pricing.Give feedback about your experience with Vertex AI in Firebase