Skip to content

Commit 8e35cdc

Browse files
authored
Add sample for onCallGenkit (#1189)
1 parent 814a99a commit 8e35cdc

File tree

8 files changed

+836
-3
lines changed

8 files changed

+836
-3
lines changed

Node/pnpm-lock.yaml

+584-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
firebase-debug.*.log*
9+
10+
# Firebase cache
11+
.firebase/
12+
13+
# Firebase config
14+
15+
# Uncomment this if you'd like others to create their own Firebase project.
16+
# For a team working on the same Firebase project(s), it is recommended to leave
17+
# it commented so all members can deploy to the same project(s) in .firebaserc.
18+
# .firebaserc
19+
20+
# Runtime data
21+
pids
22+
*.pid
23+
*.seed
24+
*.pid.lock
25+
26+
# Directory for instrumented libs generated by jscoverage/JSCover
27+
lib-cov
28+
29+
# Coverage directory used by tools like istanbul
30+
coverage
31+
32+
# nyc test coverage
33+
.nyc_output
34+
35+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
36+
.grunt
37+
38+
# Bower dependency directory (https://bower.io/)
39+
bower_components
40+
41+
# node-waf configuration
42+
.lock-wscript
43+
44+
# Compiled binary addons (http://nodejs.org/api/addons.html)
45+
build/Release
46+
47+
# Dependency directories
48+
node_modules/
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Optional REPL history
57+
.node_repl_history
58+
59+
# Output of 'npm pack'
60+
*.tgz
61+
62+
# Yarn Integrity file
63+
.yarn-integrity
64+
65+
# dotenv environment variables file
66+
.env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Genkit quickstart
2+
================================================
3+
4+
This quickstart demonstrates how to initialize a [Genkit flow](https://firebase.google.com/docs/genkit/flows) and serve it with Cloud Functions for Firebase.
5+
6+
[Read more about Cloud Functions for Firebase](https://firebase.google.com/docs/functions/)
7+
8+
9+
Getting Started
10+
---------------
11+
12+
1. Install dependencies with `npm install`
13+
1. Start the functions emulator with `firebase emulators:start --only functions`
14+
1. The emulator will output the function URL. It is usually of the form:
15+
16+
```
17+
https://127.0.0.1:5001/{$PROJECT}/us-central1/tellJoke
18+
```
19+
20+
1. Call the function from a terminal, replacing the `url` argument with your function's URL:
21+
22+
```bash
23+
$ curl -X POST \
24+
--url https://127.0.0.1:5001/{$PROJECT}/us-central1/tellJoke \
25+
--header "Content-Type: application/json" \
26+
--header "Accept: text/event-stream" \
27+
--data '{"data": "Observational comedy"}'
28+
```
29+
30+
License
31+
-------
32+
33+
© Google, 2025. Licensed under an [Apache-2](../../../LICENSE) license.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"functions": {
3+
"codebase": "oncallgenkit-helloworld",
4+
"predeploy": [
5+
"npm --prefix \"$RESOURCE_DIR\" run lint"
6+
]
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module.exports = {
18+
root: true,
19+
env: {
20+
es2020: true,
21+
node: true,
22+
},
23+
extends: [
24+
"eslint:recommended",
25+
"google",
26+
],
27+
rules: {
28+
quotes: ["error", "double"],
29+
},
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START complete-example]
18+
// [START imports]
19+
// [START import-trigger]
20+
const {onCallGenkit} = require("firebase-functions/v2/https");
21+
// [END import-trigger]
22+
// [START import-params]
23+
const {defineSecret} = require("firebase-functions/params");
24+
// [END import-params]
25+
26+
// [START import-genkit]
27+
// Dependencies for Genkit.
28+
const {gemini15Flash, googleAI} = require("@genkit-ai/googleai");
29+
const {genkit, z} = require("genkit");
30+
// [END import-genkit]
31+
// [END imports]
32+
33+
// [START define-secret]
34+
// Store the Gemini API key in Cloud Secret Manager.
35+
const apiKey = defineSecret("GOOGLE_GENAI_API_KEY");
36+
// [END define-secret]
37+
38+
// [START flow]
39+
const ai = genkit({
40+
plugins: [googleAI()],
41+
model: gemini15Flash,
42+
});
43+
44+
const jokeTeller = ai.defineFlow({
45+
name: "jokeTeller",
46+
inputSchema: z.string().nullable(),
47+
outputSchema: z.string(),
48+
streamSchema: z.string(),
49+
}, async (jokeType = "knock-knock", response) => {
50+
const prompt = `Tell me a ${jokeType} joke.`;
51+
52+
// Call the `generateStream()` method to
53+
// receive the `stream` async iterable.
54+
const {stream, response: aiResponse} = ai.generateStream(prompt);
55+
56+
// Send new words of the generative AI response
57+
// to the client as they're generated.
58+
for await (const chunk of stream) {
59+
response.sendChunk(chunk.text);
60+
}
61+
62+
// Return the full generative AI response
63+
// to clients that may not support streaming.
64+
return (await aiResponse).text;
65+
},
66+
);
67+
// [END flow]
68+
69+
// [START trigger]
70+
exports.tellJoke = onCallGenkit({
71+
// [START bind-secrets]
72+
// Bind the Gemini API key secret parameter to the function.
73+
secrets: [apiKey],
74+
// [END bind-secrets]
75+
// [START auth-policy]
76+
// Protect your endpoint with authPolicy.
77+
// authPolicy: (auth) => !!auth?.token.email_verified,
78+
// [END auth-policy]
79+
},
80+
// Pass in the genkit flow.
81+
jokeTeller,
82+
);
83+
// [END trigger]
84+
// [END complete-example]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "functions",
3+
"description": "Cloud Functions for Firebase",
4+
"scripts": {
5+
"lint": "eslint .",
6+
"lintfix": "eslint . --fix",
7+
"serve": "firebase emulators:start --only functions",
8+
"shell": "firebase functions:shell",
9+
"start": "npm run shell",
10+
"deploy": "firebase deploy --only functions",
11+
"logs": "firebase functions:log",
12+
"compile": "cp ../../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
13+
},
14+
"engines": {
15+
"node": "22"
16+
},
17+
"main": "index.js",
18+
"dependencies": {
19+
"@genkit-ai/googleai": "1.0.0-rc.12",
20+
"firebase-admin": "^13.0.2",
21+
"firebase-functions": "^6.3.0",
22+
"genkit": "1.0.0-rc.12"
23+
},
24+
"devDependencies": {
25+
"eslint": "^8.57.1",
26+
"eslint-config-google": "^0.14.0",
27+
"firebase-functions-test": "^3.4.0"
28+
},
29+
"private": true
30+
}

0 commit comments

Comments
 (0)