DEV Community

Evan Lin
Evan Lin

Posted on • Originally published at evanlin.com on

LINE Bot with Long Memory: Firebase Database, Gemini Pro, and Cloud Functions

image-20240413210750427

Preface:

This is the second in a series of articles for the BUILD WITH AI (BWAI) WORKSHOP, in collaboration with the Google Developer Group on 04/18 (it's unknown how many more articles are needed).

This article will focus on the following aspects:

  • Firebase Database setup
  • How to access Firebase through the official Golang on Cloud Function
  • Using Firebase Database to make your Gemini remember everything that has been said, optimizing the LINE Bot built in the last time

Article List:

Preparation

  • LINE Developer Account: You only need a LINE account to apply for a developer account.
  • Google Cloud Functions: The deployment platform for Go code, generating the webhook address for LINEBot.
  • Firebase: Create a Realtime database, LINE Bot can remember your previous conversations and even answer many interesting questions.
  • Google AI Studio: You can get the Gemini Key here.

Applying for Firebase Database Service

  • Remember to go to Firebase Console and create a project.

  • Create a Firebase Realtime Database, which will be used later

  • Select the US region

  • Start in “lock mode”

  • For ease of development, set it to read and write in “Rules”. Pay close attention:

image-20240413213202354

  • Remember the URL (Note! You need to change the permissions back when you go live), and add an item: “ BwAI

image-20240413213802313

Applying for Services Account Credential to connect Cloud Function to Google services

You can actually refer to another article of mine for this part of the tutorial. [Learning Document] How to use Golang to access Google Cloud services on Heroku, but I'll quickly go through it here.

  • Enter Google Cloud Console, go to IAM & Admin and select Create Services Account

image-20240413221505536

  • Decide on the Services Account Name yourself, pay attention (the project and Firebase project names must be consistent)

image-20240413222847247

  • Grant this service account access to project. When setting the identity, it is recommended to use Editor first (it is larger and needs to be used with caution)

image-20240413223055288

  • “Grant users access to this service account” does not need to be specifically set
  • Press “Manage Keys” to prepare to download Credential

image-20240413223225404

  • Select Add Key -> Create new Key -> Download JSON

image-20240413223613244

Things to note when using Golang Google Options package:

Although Firebase Realtime Database has been set to allow everyone to read and write, if you access it through Golang, you will get an Unauthorized request error message. This is because the Project of your JSON file is different from your Firebase Project. Just recreate a Services Account and update the JSON content.

image-20240413220630196

How to import Services Account Credential in Google Cloud Function?

Next, I will share how to correctly use it within Cloud Function. If you want to directly use Cloud Function to open the Credential JSON file, you will always get an error message that you cannot get the credential correctly.

At this time, you need to add it through environment variables:

  • Copy all the content in the JSON file
  • Set the GOOGLE_APPLICATION_CREDENTIALS parameter, and then paste all the content into the environment parameter.

image-20240413225710980

  • Next, I will tell you how to modify the relevant code?
    // Init firebase related variables
    ctx := context.Background()
    opt := option.WithCredentialsJSON([]byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")))
    config := &firebase.Config{DatabaseURL: os.Getenv("FIREBASE_URL")}
    app, err := firebase.NewApp(ctx, config, opt)
    if err != nil {
        log.Fatalf("error initializing app: %v", err)
    }
    client, err := app.Database(ctx)
    if err != nil {
        log.Fatalf("error initializing database: %v", err)
    }

Enter fullscreen mode Exit fullscreen mode
  • First, option.WithCredentialsJSON([]byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"))) allows you to read the credential from the environment variable.
  • Next, &firebase.Config{DatabaseURL: os.Getenv("FIREBASE_URL")} sets the FIREBASE_URL content.
  • This can be executed correctly, and then we will look at the relevant processing of Gemini chat history.

How to correctly process Gemini Pro Chat History?

Full Source Code

You can find the relevant open source code here: https://github.com/kkdai/linebot-cf-firebase

Top comments (0)