Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks and corrections to cloud secrets info #520

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 63 additions & 23 deletions docs/firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,46 @@ deploying the default sample flow to Firebase.
1. [Generate an API key](https://aistudio.google.com/app/apikey) for the
Gemini API using Google AI Studio.

1. Set the `GOOGLE_GENAI_API_KEY` environment variable to your key:
1. Store your API key in Cloud Secret Manager:

```posix-terminal
export GOOGLE_GENAI_API_KEY=<your API key>
firebase functions:secrets:set GOOGLE_GENAI_API_KEY
```

This step is important to prevent accidentally leaking your API key,
which grants access to a potentially metered service.

See [Store and access sensitive configuration information](https://firebase.google.com/docs/functions/config-env?gen=2nd#secret-manager)
for more information on managing secrets.

1. Edit `src/index.ts` and add the following after the existing imports:

<!--See note above on prettier-ignore -->
<!-- prettier-ignore -->
```js
import {defineSecret} from "firebase-functions/params";
defineSecret("GOOGLE_GENAI_API_KEY");
```
<!--See note above on prettier-ignore -->
<!-- prettier-ignore -->
```js
import {defineSecret} from "firebase-functions/params";
const googleAIapiKey = defineSecret("GOOGLE_GENAI_API_KEY");
```

Then, in the flow definition, declare that the cloud function needs
access to this secret value:

<!--See note above on prettier-ignore -->
<!-- prettier-ignore -->
```js
export const menuSuggestionFlow = onFlow(
{
name: "menuSuggestionFlow",
// ...
httpsOptions: {
secrets: [googleAIapiKey], // Add this line.
},
},
async (subject) => {
// ...
}
);
```

Now, when you deploy this function, your API key will be stored in
Cloud Secret Manager, and available from the Cloud Functions
Expand All @@ -93,19 +119,6 @@ deploying the default sample flow to Firebase.
page, ensure that the **Default compute service account** is granted
the **Vertex AI User** role.

1. **Optional**: If you want to run your flow locally, as in the next
step, set some additional environment variables and use the
[`gcloud`](https://cloud.google.com/sdk/gcloud) tool to set up
application default credentials:

```posix-terminal
export GCLOUD_PROJECT=<your project ID>

export GCLOUD_LOCATION=us-central1

gcloud auth application-default login
```

The only secret you need to set up for this tutorial is for the model
provider, but in general, you must do something similar for each service
your flow uses.
Expand All @@ -120,7 +133,9 @@ deploying the default sample flow to Firebase.
{
name: "menuSuggestionFlow",
// ...
httpsOptions: {cors: true}, // Add this line.
httpsOptions: {
cors: true, // Add this line.
},
},
async (subject) => {
// ...
Expand All @@ -133,6 +148,31 @@ deploying the default sample flow to Firebase.

1. **Optional**: Try your flow in the developer UI:

1. Make API credentials available locally. Do one of the following,
depending on the model provider you chose:

- {Gemini (Google AI)}

Set the `GOOGLE_GENAI_API_KEY` environment variable to your key:

```posix-terminal
export GOOGLE_GENAI_API_KEY=<your API key>
```

- {Gemini (Vertex AI)}

Set some additional environment variables and use the
[`gcloud`](https://cloud.google.com/sdk/gcloud) tool to set up
application default credentials locally:

```posix-terminal
export GCLOUD_PROJECT=<your project ID>

export GCLOUD_LOCATION=us-central1

gcloud auth application-default login
```

1. Start the UI:

```posix-terminal
Expand All @@ -141,7 +181,7 @@ deploying the default sample flow to Firebase.
genkit start
```

2. In the developer UI (http://localhost:4000/), run the flow:
1. In the developer UI (http://localhost:4000/), run the flow:

1. Click **menuSuggestionFlow**.

Expand Down
Loading