More
How to Integrate Generative AI in Your Flutter App Using Firebase Vertex AI
11 min
introduction generative ai is fast becoming a normal thing in today's apps; it allows developers to create smart and interactive experiences be that text generation, content analysis, or media creation firebase vertex ai offers a quite powerful set of tools for realizing these functionalities within your flutter app we will walk you through how to bring generative ai into your flutter app using the firebase vertexai package in this tutorial by the end of this tutorial, at the very least, you'll have a functioning application that can tap into gemini's api to do all manner of generative ai tasks prerequisites to complete this tutorial, you will need a firebase account and a firebase project set up you can sign up for free at firebase https //firebase google com a flutter development environment is set up on your local machine follow the flutter installation guide https //flutter dev/docs/get started/install if you haven't set it up yet basic knowledge of firebase and flutter if you're new to firebase, check out the firebase for flutter guide https //firebase flutter dev/docs/overview step 1 – setting up firebase and vertex ai create a firebase project log in to the firebase console https //console firebase google com/ , create a new project, and enable the necessary apis for vertex ai enable firebase app check navigate to your project settings in firebase, and enable firebase app check to ensure that all api calls are coming from your app add firebase to your flutter app follow the instructions to connect your flutter app to your firebase project this typically involves downloading the google services json file for android or googleservice info plist for ios and placing it in the correct directory of your flutter project step 2 – adding required dependencies open your pubspec yaml file and add the following dependencies dependencies flutter sdk flutter firebase core latest version firebase vertexai latest version 2\ run flutter pub get to install the dependencies step 3 – initializing firebase and vertex ai in lib/main dart , import the necessary firebase packages import 'package\ flutter/material dart'; import 'package\ firebase core/firebase core dart'; import 'package\ firebase vertexai/firebase vertexai dart'; 2\ initialize firebase and vertex ai in the main function void main() async { widgetsflutterbinding ensureinitialized(); await firebase initializeapp(); runapp(myapp()); } 3\ in your myapp widget, create a function to initialize the generative model class myapp extends statelesswidget { @override widget build(buildcontext context) { return materialapp( home scaffold( appbar appbar(title text('generative ai with firebase')), body center( child elevatedbutton( onpressed () async { final result = await generatecontent(); print(result); }, child text('generate content'), ), ), ), ); } future\<string> generatecontent() async { // initialize the generative model final vertexai = firebasevertexai instance; final generativemodel = vertexai getgenerativemodel( modelname 'projects/your project id/locations/your location/models/your model name', ); // construct your prompt final prompt = vertexaitextprompt(text 'write a story about a magic backpack'); // optionally, you can set generation configurations final generationconfig = vertexaigenerationconfig( temperature 0 7, responsemimetype 'application/json', ); // generate content final response = await generativemodel generatecontent( prompt prompt, generationconfig generationconfig, ); return response generatedtext; } } replace 'your project id' , 'your location' , and 'your model name' with your actual project details the generatecontent function constructs a prompt, sends it to the vertex ai model, and returns the generated content step 4 – constructing prompts and handling responses creating a custom prompt you can create more complex prompts by combining different types of data (text, images, audio, etc ) for example final prompt = vertexaitextprompt( text 'summarize the following text "the history of ai is fascinating "', ); 2\ handling large files if your prompt involves large files, store the files in firebase storage and include the file's cloud storage url in your request final prompt = vertexaitextprompt( text 'analyze the content of this document', fileurl 'gs\ //your bucket name/your file pdf', ); 3\ streaming responses if you want to stream the ai's output as it becomes available, you can use the generatecontentstream method await for (final partialresponse in generativemodel generatecontentstream(prompt prompt)) { print(partialresponse generatedtext); } step 5 – testing and deploying the app run your app using flutter run and test the ai generation features you should see the generated content printed in the console once you've confirmed that everything works, you can deploy your app or continue to enhance it with additional features conclusion in this tutorial, you learned how to include generative ai in your flutter app using the firebase vertex ai package you can use gemini to power excellent artificial intelligence capabilities in an app for text generation, analysis of content data, and many more ai driven features because of the flexibility of firebase vertex ai, you can change at will the ai responses, making it perfect for developing intelligent and interactive apps see pub dev for firebase vertex ai, flutterfire, and other flutter packages happy coding!