-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add sample for onCallGenkit
#1189
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1cf912a
first draft
jhuleatt ab4a920
re-camelcase jokeTeller
jhuleatt fa9fd59
add snippets
jhuleatt 4c1136b
add a snippet for just importing the trigger
jhuleatt 5d6ad35
address Daniel's review comments
jhuleatt 2b23cb1
refine comments
jhuleatt 23e0ef4
add README
jhuleatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
firebase-debug.log* | ||
firebase-debug.*.log* | ||
|
||
# Firebase cache | ||
.firebase/ | ||
|
||
# Firebase config | ||
|
||
# Uncomment this if you'd like others to create their own Firebase project. | ||
# For a team working on the same Firebase project(s), it is recommended to leave | ||
# it commented so all members can deploy to the same project(s) in .firebaserc. | ||
# .firebaserc | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"functions": { | ||
"codebase": "oncallgenkit-helloworld", | ||
"predeploy": [ | ||
"npm --prefix \"$RESOURCE_DIR\" run lint" | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
module.exports = { | ||
root: true, | ||
env: { | ||
es2020: true, | ||
node: true, | ||
}, | ||
extends: [ | ||
"eslint:recommended", | ||
"google", | ||
], | ||
rules: { | ||
quotes: ["error", "double"], | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// [START complete-example] | ||
// [START imports] | ||
// [START import-trigger] | ||
const {onCallGenkit} = require("firebase-functions/v2/https"); | ||
// [END import-trigger] | ||
// [START import-params] | ||
const {defineSecret} = require("firebase-functions/params"); | ||
// [END import-params] | ||
|
||
// [START import-genkit] | ||
// Dependencies for Genkit | ||
const {gemini15Flash, googleAI} = require("@genkit-ai/googleai"); | ||
const {genkit, z} = require("genkit"); | ||
// [END import-genkit] | ||
// [END imports] | ||
|
||
// [START define-secret] | ||
// Store the Gemini API key in Cloud Secret Manager | ||
const apiKey = defineSecret("GOOGLE_GENAI_API_KEY"); | ||
// [END define-secret] | ||
|
||
// [START flow] | ||
const ai = genkit({ | ||
plugins: [googleAI()], | ||
model: gemini15Flash, | ||
}); | ||
|
||
const jokeTeller = ai.defineFlow({ | ||
name: "jokeTeller", | ||
inputSchema: z.string().nullable(), | ||
outputSchema: z.string(), | ||
streamSchema: z.string(), | ||
}, async (jokeType = "knock-knock", response) => { | ||
const prompt = `Tell me a ${jokeType} joke.`; | ||
const {stream, response: aiResponse} = ai.generateStream(prompt); | ||
|
||
// Loop over the `stream` async iterable to | ||
// send new words of the AI response | ||
// to the client as they're generated | ||
for await (const chunk of stream) { | ||
response.sendChunk(chunk.text); | ||
} | ||
|
||
// Return the full response | ||
return (await aiResponse).text; | ||
}, | ||
); | ||
// [END flow] | ||
|
||
// [START trigger] | ||
exports.tellJoke = onCallGenkit({ | ||
// [START bind-secrets] | ||
// bind the Gemini API key secret parameter to the function | ||
jhuleatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
secrets: [apiKey], | ||
// [END bind-secrets] | ||
// [START auth-policy] | ||
// protect your endpoint with authPolicy | ||
authPolicy: (auth) => !!auth?.token.email_verified, | ||
// [END auth-policy] | ||
}, | ||
// pass in the genkit flow | ||
jokeTeller, | ||
); | ||
// [END trigger] | ||
// [END complete-example] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "functions", | ||
"description": "Cloud Functions for Firebase", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"lintfix": "eslint . --fix", | ||
"serve": "firebase emulators:start --only functions", | ||
"shell": "firebase functions:shell", | ||
"start": "npm run shell", | ||
"deploy": "firebase deploy --only functions", | ||
"logs": "firebase functions:log", | ||
"compile": "cp ../../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json" | ||
}, | ||
"engines": { | ||
"node": "22" | ||
}, | ||
"main": "index.js", | ||
"dependencies": { | ||
"@genkit-ai/googleai": "1.0.0-rc.12", | ||
"firebase-admin": "^13.0.2", | ||
"firebase-functions": "^6.3.0", | ||
"genkit": "1.0.0-rc.12" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.57.1", | ||
"eslint-config-google": "^0.14.0", | ||
"firebase-functions-test": "^3.4.0" | ||
}, | ||
"private": true | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.