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

Adds onCallGenkit #1655

Merged
merged 18 commits into from
Jan 24, 2025
Merged

Adds onCallGenkit #1655

merged 18 commits into from
Jan 24, 2025

Conversation

inlined
Copy link
Member

@inlined inlined commented Dec 17, 2024

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.

@inlined inlined force-pushed the inlined.onCallGenkit branch from 56dc646 to 28f6bc0 Compare January 22, 2025 15:46
@inlined inlined requested a review from taeold January 22, 2025 17:31
@inlined
Copy link
Member Author

inlined commented Jan 22, 2025

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.

@inlined inlined requested a review from mbleigh January 22, 2025 17:47
Copy link
Contributor

@taeold taeold left a 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(
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member Author

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?

@inlined inlined added this pull request to the merge queue Jan 24, 2025
Merged via the queue into master with commit 2cc6936 Jan 24, 2025
17 checks passed
@taeold taeold mentioned this pull request Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants