-
Notifications
You must be signed in to change notification settings - Fork 205
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
Adds onCallGenkit #1655
Adds onCallGenkit #1655
Conversation
This reverts commit b460ad1.
56dc646
to
28f6bc0
Compare
Tested manually, though it requires a CLI with firebase/firebase-tools#8127 Code: import { defineSecret } from 'firebase-functions/params';
import { onCallGenkit } from "firebase-functions/https";
import { gemini15Flash, googleAI } from "@genkit-ai/googleai";
import { genkit, z } from "genkit";
const apiKey = defineSecret("GOOGLE_GENAI_API_KEY")
const ai = genkit({
plugins: [ googleAI(), ],
model: gemini15Flash,
});
export const jokeTeller = ai.defineFlow({
name: "jokeTeller",
inputSchema: z.string().nullable(),
outputSchema: z.string(),
streamSchema: z.string(),
}, async (type, { sendChunk }) => {
const { stream, response } = ai.generateStream(`Tell me a ${type ?? "dad"} joke.`);
for await (const chunk of stream) {
sendChunk(chunk.text);
}
return (await response).text;
});
export const tellJoke = onCallGenkit({ secrets: [apiKey] }, jokeTeller); Tests: ❯ curl -X POST -H "Content-Type: application/json" -d '{"data": "software"}' https://us-central1-inlined-junkdrawer.cloudfunctions.net/tellJoke
{"result":"Why do programmers prefer dark mode?\n\nBecause light attracts bugs!\n"}%
❯ curl -X POST -H "Content-Type: application/json" -H "Accept: text/event-stream" -d '{"data": "software"}' https://us-central1-inlined-junkdrawer.cloudfunctions.net/tellJoke
data: {"message":"Why"}
data: {"message":" do programmers prefer dark mode?\n\nBecause light attracts bugs!\n"}
data: {"result":"Why do programmers prefer dark mode?\n\nBecause light attracts bugs!\n"} Additionally tested that a second test without using onCallGenkit was able to compile successfully without taking a dependency on Genkit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks awesome!
action = optsOrAction as A; | ||
} | ||
if (!opts.secrets?.length) { | ||
logger.debug( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: is this visible during firebase deploy
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only if you do --debug I think. Can figure out for sure later, but I have to run and want to let you cut a release so I can build upon it over the weekend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we could push this into the CLI where it notices a genkit onCall with no secrets?
Adds support for a callable function specialization just for Genkit!
This design chooses to implement callable functions by locally redefining necessary interfaces to avoid a dependency on Genkit that is otherwise only needed for type definitions. Once RC.4 is released (fixing compiler errors in firebase-functions when taking a dependency on genkit) we can add genkit as a dev dependency and add a "succeeds if compiles" test to detect changes in the Genkit API.