Skip to content

yukinagae/genkit-firebase-functions-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

genkit-firebase-functions-sample

genkit-firebase-functions-sample is your starting point for learning about Firebase Genkit, an open-source framework that helps developers create AI-powered applications. This sample project not only introduces you to the basics of Genkit but also guides you through the process of integrating Firebase Functions.

Requirements

Before you start, make sure you have these installed:

  • Node.js version 22 or later
  • npm
  • Genkit
  • Firebase CLI

For Genkit installation, see the Firebase Genkit - Get started. For Firebase CLI installation, see the Firebase - Firebase CLI reference.

Check your installations by running:

$ node --version # the below version is on my environment
v22.4.1
$ npm --version # the below version is on my environment
10.7.0
$ genkit --version # the below version is on my environment
0.5.4
$ firebase --version # the below version is on my environment
13.13.0

Important: Ensure all subsequent commands are executed within the functions directory. To navigate to this directory, use the command cd functions and verify your current working directory if necessary.

Setup

Install Project Dependencies: Open your terminal, navigate to this project's functions folder, and run:

$ npm install

Usage

Running Genkit Locally

Before running the project locally, you need to provide your OpenAI API key. This key allows your application to communicate with OpenAI's services. Replace your_api_key with the actual API key you obtained from OpenAI.

$ export OPENAI_API_KEY=your_api_key

To start the Genkit server on your local machine and automatically open your default web browser to http://localhost:4000, execute the following command in your terminal:

$ npm run genkit

Firebase Functions Quickstart

  • Setup: Initialize your project with Firebase.
  • Local Emulator: Test functions locally.
  • Deploy: Publish your functions.

Setup

Before deploying your application, complete the following preparatory steps:

  1. Create a Firebase project:

Navigate to the Firebase Console. Click on Create a project and follow the prompts to create a new Firebase project.

  1. Switch to the Blaze plan:

Firebase Functions require the Blaze (Pay as you go) plan for deployment. In the Firebase Console, select your project, then navigate to the left side bar section to change your plan.

  1. Configure your Firebase project locally:

Update the .firebaserc file in your project's root directory to include your Firebase project name:

{
  "projects": {
    "default": "your_project_name"
  }
}

Local Emulator

To facilitate local development and testing of Firebase Functions, use the Firebase Emulator Suite. Follow these steps to run your functions locally:

To run Firebase Functions locally using the emulator, set your OpenAI API key as an environment variable and start the emulator:

$ export OPENAI_API_KEY=your_api_key
$ npm run emulator

To test the function on the emulator, use the following curl command with a valid token (token1234 in this project):

$  curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer token1234" \
-d '{"data":{"url":"https://firebase.blog/posts/2024/04/next-announcements/","lang":"English"}}' \
http://127.0.0.1:5001/[your_project_name]/us-central1/summarizeFlow
{"result":"Firebase announced new features at Cloud Next '24, including Firestore vector search, Vertex AI SDKs, and public preview of Gemini integration."}

With an invalid token:

$  curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer invalid_token" \
-d '{"data":{"url":"https://firebase.blog/posts/2024/04/next-announcements/","lang":"English"}}' \
http://127.0.0.1:5001/[your_project_name]/us-central1/summarizeFlow
Unauthorized

Deploy

To authenticate with Firebase and access your projects, use the Firebase CLI login command:

$ firebase login

To keep your OpenAI API key safe when using Firebase Functions, store it as a secret in Google Clooud Secret Manger:

$ firebase functions:secrets:set OPENAI_API_KEY
? Enter a value for OPENAI_API_KEY [input is hidden]

To confirm your OpenAI API key is correctly stored as a secret, use the following command:

$ firebase functions:secrets:access OPENAI_API_KEY
your_api_key

After securing your API key, you're ready to deploy your application to Firebase Functions:

$ npm run deploy

To monitor the behavior and troubleshoot your deployed functions, view the logs:

$ npm run logs

To test your deployed function, execute the curl command below:

$  curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer token1234" \
-d '{"data":{"url":"https://firebase.blog/posts/2024/04/next-announcements/","lang":"English"}}' \
https://summarizeflow-[your_function_id]-uc.a.run.app
{"result":"Firebase announced new features at Cloud Next '24, including Firestore vector search, Vertex AI SDKs, and public preview of Gemini integration."}

Replace [your_function_id] with your Firebase project value, found in the Firebase Console under the Functions Dashboard.

Making Changes

Building the Project

After making changes, you might need to build the project to see your changes in action:

$ npm run build

Formatting and Linting

To ensure your code follows the project's coding standards, run the formatting and linting tools:

$ npm run typecheck # type check without modifying files
$ npm run check     # scan without modifying files
$ npm run fix       # modify files

License

MIT