Skip to content

Commit 85df0ba

Browse files
committed
Misc cleanup, send errors to web interface, update readmes, caveat the onRequest sample
1 parent 8580b48 commit 85df0ba

File tree

11 files changed

+223
-147
lines changed

11 files changed

+223
-147
lines changed
Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,57 @@
1-
# Use server-side Remote Config with Cloud Functions and Vertex AI
1+
Call the Vertex AI Gemini API with Remote Config and App Check
2+
==============================================================
23

3-
This Cloud Function (`generateWithVertex`) demonstrates how to generate text
4-
using Google's Vertex AI Gemini API, protected by Firebase App Check. It uses
5-
the Firebase Admin SDK for Node.js and Remote Config to manage model parameters,
6-
safety settings, and feature flags.
4+
Introduction
5+
------------
76

8-
## Setting up the sample
7+
This is a sample callable function that authenticates calling apps with App
8+
Check and then sends queries to Gemini using the Vertex AI Gemini API. Vertex
9+
AI model parameters are controlled using the Remote Config server
10+
functionality provided by the Firebase Admin SDK for Node.js. Use the client
11+
provided in `client/` to access the function through basic web page.
912

10-
Follow the [User server-side Remote Config with Cloud Functions and Vertex AI
11-
guide](https://firebase.google.com/docs/remote-config/solution-server) to:
13+
- [Read more about Remote Config for servers](https://firebase.google.com/docs/remote-config/server).
14+
- [Read more about App Check](https://firebase.google.com/docs/app-check).
15+
- [Read more about the Vertex AI Node.js Client library](https://cloud.google.com/nodejs/docs/reference/aiplatform/latest).
1216

13-
* Set up your Firebase project.
14-
* Enable required APIs and SDKs.
15-
* Configure IAM permissions.
16-
* Test your function in the Firebase emulator.
17-
* Deploy your function.
18-
19-
Important: App Check, Vertex AI and Cloud Functions require a billing
20-
account. Review
17+
Important: Vertex AI and Cloud Functions require a billing account. Review
2118
[Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing) and
2219
[Firebase pricing](https://firebase.google.com/pricing) before running
2320
this function. If you're new to Firebase and Google Cloud, check to see if
2421
you're eligible for a
2522
[$300 credit](https://firebase.google.com/support/faq#pricing-free-trial) and
2623
a Free Trial Cloud Billing account.
2724

28-
## Next steps
25+
Get Started
26+
---------------
27+
28+
1. Follow the instructions in client/README.md to create a Firebase project,
29+
enable ReCAPTCHA Enterprise, enable Firebase App Check, and add your
30+
Firebase config and ReCAPTCHA Enterprise key to the client config.
31+
2. Enable [recommended Vertex AI APIs](https://console.cloud.google.com/vertex-ai).
32+
3. Configure a Remote Config server template on the Firebase console. Use the template
33+
described in
34+
[Use server side Remote Config with Cloud Functions and Vertex
35+
AI](https://firebase.google.com/docs/remote-config/solution-server#implementation-create-template),
36+
which contains all of the parameters used in the function sample.
37+
4. Install dependencies: `cd functions && npm install`
38+
5. If you haven't already done so, install firebase-tools: `npm i firebase-tools@latest`
39+
6. Log into Firebase: `firebase login`
40+
7. Deploy the function. We recommend testing in the
41+
[Firebase emulator](https://firebase.google.com/docs/remote-config/solution-server#implementation-deploy-and-test-in-emulator).
42+
8. If testing in the emulator, verify that `testMode` is set to `true` in
43+
`client/main.ts`, then start the client: `cd public && npm run dev`
44+
9. Open the [client app in a browser](http://localhost:5173) and enter a
45+
prompt. To access the Vertex AI Gemini API, make sure that you have
46+
set the `is_vertex_enabled` boolean parameter in your Remote Config
47+
server template to `true`.
48+
49+
Support
50+
-------
51+
52+
- [Firebase Support](https://firebase.google.com/support/)
53+
54+
License
55+
-------
2956

30-
Learn more about Remote Config server implementations at
31-
[Use Remote Config in server
32-
environments](https://firebase.google.com/docs/remote-config/server).
57+
© Google, 2024. Licensed under an [Apache-2](../../LICENSE) license.
Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
1-
Test client for Remote Config server with Vertex AI and Firebase App Check callable function
2-
============================================================================================
1+
Test client for call-vertex-remote-config-server
2+
================================================
33

44
Introduction
55
------------
66

7-
This is a sample app to run with the Remote Config server with Vertex AI Node.js SDK, secured
8-
with Firebase App Check.
7+
This is a basic web app that calls the `callVertexWithRC` function. The
8+
function uses values stored in Remote Config server templates with
9+
the Firebase Admin SDK to change Vertex AI Gemini API parameters on the
10+
fly. Access is controlled using Firebase App Check.
911

10-
<!-- Introduction
11-
------------
12+
- [Read more about Remote Config for servers](https://firebase.google.com/docs/remote-config/server).
13+
- [Read more about App Check](https://firebase.google.com/docs/app-check).
14+
- [Read more about the Vertex AI Node.js Client library](https://cloud.google.com/nodejs/docs/reference/aiplatform/latest).
1215

13-
[Read more about Remote Config for servers](https://firebase.google.com/docs/remote-config/server).
16+
Important: Vertex AI and Cloud Functions require a billing account. Review
17+
[Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing) and
18+
[Firebase pricing](https://firebase.google.com/pricing) before running
19+
this function. If you're new to Firebase and Google Cloud, check to see if
20+
you're eligible for a
21+
[$300 credit](https://firebase.google.com/support/faq#pricing-free-trial) and
22+
a Free Trial Cloud Billing account.
1423

15-
Getting Started
24+
Get started
1625
---------------
1726

18-
1. Create your project on the [Firebase Console](https://console.firebase.google.com).
19-
2. Create a ReCAPTCHA Enterprise key in the same project.
20-
3. Enable App Check in the Firebase console with the ReCAPTCHA Enterprise site key you created.
21-
4. Copy your Firebase project config and your ReCAPTCHA Enterprise site key into the appropriate places in `config.ts` in this directory.
22-
5. Set up the function as described in the readme in the ../functions directory.
23-
5. In this directory, run `npm install`
24-
6. In this directory, run `npm run dev`
25-
26-
To run this app against the generateWithVertex function running in an emulator, set
27-
`testMode` to true.
28-
29-
<!-- Support
27+
1. Create a [Firebase project and register a web app](https://firebase.google.com/docs/web/setup#create-firebase-project-and-app).
28+
2. [Create a ReCAPTCHA Enterprise key](https://firebase.google.com/docs/app-check/web/recaptcha-enterprise-provider#project-setup)
29+
in the same project.
30+
3. [Enable App Check](https://firebase.google.com/docs/app-check/web/recaptcha-enterprise-provider)
31+
in the Firebase console with the ReCAPTCHA Enterprise site key you created.
32+
4. Copy your Firebase project config and your ReCAPTCHA Enterprise site key
33+
into the appropriate places in `config.ts` in this directory.
34+
5. In this directory, run `npm install`.
35+
6. Set up the function as described in [../README.md](../README.md).
36+
7. In this directory, run `npm run dev` to run the client.
37+
38+
We recommend running and testing functions locally before deploying to Google
39+
Cloud. To run this app against the `callVertexWithRC` function running in an
40+
emulator, set `testMode` to true.
41+
42+
Support
3043
-------
3144

32-
- [Firebase Support](https://firebase.google.com/support/) -->
45+
- [Firebase Support](https://firebase.google.com/support/)
3346

3447
License
3548
-------
3649

37-
© Google, 2024. Licensed under an [Apache-2](../LICENSE) license.
50+
© Google, 2024. Licensed under an [Apache-2](../../../LICENSE) license.
Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,72 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1.0">
67
<title>Client app for Remote Config server function with Vertex AI and App Check</title>
8+
<style>
9+
body {
10+
font-family: Arial, sans-serif;
11+
background-color: #f2f2f2;
12+
justify-content: center;
13+
align-items: center;
14+
height: 100vh;
15+
margin: 70px;
16+
}
17+
18+
h1 {
19+
margin-bottom: 20px;
20+
}
21+
22+
.container {
23+
background-color: #ffffff;
24+
border-radius: 8px;
25+
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
26+
padding: 20px;
27+
max-width: 400px;
28+
width: 100%;
29+
text-align: center;
30+
}
31+
32+
input[type="text"],
33+
input[type="submit"] {
34+
width: 35%;
35+
padding: 10px;
36+
margin-bottom: 10px;
37+
border: 1px solid #ccc;
38+
border-radius: 4px;
39+
box-sizing: border-box;
40+
}
41+
42+
input[type="submit"] {
43+
background-color: #FFA500;
44+
color: white;
45+
border: none;
46+
cursor: pointer;
47+
font-size: 16px;
48+
font-weight: bold;
49+
}
50+
51+
input[type="submit"]:hover {
52+
background-color: #45a049;
53+
}
54+
55+
#waitingMessage,
56+
#generatedText,
57+
#errorMessage {
58+
margin-top: 20px;
59+
text-align: left;
60+
}
61+
</style>
762
</head>
63+
864
<body>
65+
<div>
66+
<h1>Client app for Remote Config server function with Vertex AI and App Check</h1>
67+
<br />
68+
</div>
969
<script type="module" src="main.ts"></script>
1070
</body>
11-
</html>
71+
72+
</html>

Node/call-vertex-remote-config-server/client/main.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
} from "firebase/functions";
2929

3030
// Set to true to test in emulator.
31-
const testMode = false;
31+
const testMode = true;
3232

3333
// Use showdown to convert Gemini-provided Markdown to HTML
3434
import { Converter } from "showdown";
@@ -46,8 +46,8 @@ initializeAppCheck(app, {
4646
provider: new ReCaptchaEnterpriseProvider(RECAPTCHA_ENTERPRISE_SITE_KEY),
4747
});
4848

49-
// Define generateWithVertex as a call to the generateWithVertex function.
50-
const generateWithVertex = httpsCallable(getFunctions(), "generateWithVertex", {
49+
// Define callVertexWithRC as a call to the callVertexWithRC function.
50+
const callVertexWithRC = httpsCallable(getFunctions(), "callVertexWithRC", {
5151
limitedUseAppCheckTokens: true,
5252
});
5353

@@ -60,14 +60,16 @@ if (testMode) {
6060

6161
// Generate body for index.html.
6262
document.body.innerHTML += `
63+
64+
<div id="waitingMessage"></div>
65+
<div id="generatedText"></div>
66+
<div id="errorMessage"></div>
67+
<br/>
6368
<form id="promptForm">
64-
<label for="promptInput">Prompt:</label><br>
69+
<label for="promptInput">Ask Gemini a question!</label><br>
6570
<input type="text" id="promptInput" name="prompt"><br><br>
6671
<input type="submit" value="Submit">
6772
</form>
68-
<br/>
69-
<p id="generatedText"></p>
70-
7173
`;
7274

7375
const promptForm = document.getElementById("promptForm") as HTMLFormElement;
@@ -79,15 +81,22 @@ promptForm.addEventListener("submit", async (event) => {
7981
) as HTMLInputElement;
8082
const prompt = promptInput.value;
8183

84+
const waitingMessageElement = document.getElementById("waitingMessage");
85+
waitingMessageElement.textContent = "Waiting for response...";
86+
8287
try {
83-
const { data } = await generateWithVertex({ prompt });
88+
const { data } = await callVertexWithRC({ prompt });
8489
const generatedTextElement = document.getElementById("generatedText"); // Access the element
8590
const htmlContent = converter.makeHtml(data);
86-
if (!generatedTextElement){
91+
if (!generatedTextElement) {
8792
throw new Error("Missing generated text.");
8893
}
8994
generatedTextElement.innerHTML = htmlContent; // Set the element's content
95+
waitingMessageElement.textContent = "";
96+
9097
} catch (error) {
91-
console.error("Error calling generateWithVertex:", error);
98+
const errorMessageElement = document.getElementById("errorMessage");
99+
errorMessageElement.textContent = "Error calling generateWithVertex: " + error.message;
100+
waitingMessageElement.textContent = "";
92101
}
93102
});

Node/call-vertex-remote-config-server/client/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
},
2828
"dependencies": {
2929
"@firebase/functions": "^0.11.5",
30-
"@types/firebase": "^3.2.1",
3130
"firebase": "^10.12.1",
3231
"showdown": "^2.1.0"
3332
}

Node/call-vertex-remote-config-server/client/vite.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { defineConfig } from 'vite';
22

33
export default defineConfig({
4-
base: '/',
4+
base: '',
55
build: {
66
rollupOptions: {
7-
input: ['index.html'],
7+
input: ['index.html','main.ts'],
88
},
99
},
1010
logLevel: 'info',

Node/call-vertex-remote-config-server/firebase.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,24 @@
99
"firebase-debug.log",
1010
"firebase-debug.*.log",
1111
"*.local",
12-
"*.bak",
12+
"*.bak"
1313
]
1414
}
15-
]
15+
],
16+
"emulators": {
17+
"functions": {
18+
"port": 5001
19+
},
20+
"ui": {
21+
"enabled": true
22+
},
23+
"singleProjectMode": true
24+
},
25+
"hosting": {
26+
"public": "client/dist",
27+
"ignore": [
28+
"firebase.json",
29+
"**/node_modules/**"
30+
]
31+
}
1632
}

Node/call-vertex-remote-config-server/functions/README.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)