Skip to content

Commit

Permalink
docs: migration guide add onCallGenkit auth
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelgj committed Feb 7, 2025
1 parent ce23c4a commit c47c69c
Showing 1 changed file with 80 additions and 69 deletions.
149 changes: 80 additions & 69 deletions docs/migrating-from-0.9.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ We're introducing an unstable, Beta API channel, and leaving session, chat and G
* `ai.defineFormat`
* `ai.defineInterrupt`

Note: When using the APIs as part of the Beta API, you may experience breaking changes outside of SemVer. Breaking chanhes may occur on minor release (we'll try to avoid making breaking beta API changes on patch releases).
Note: When using the APIs as part of the Beta API, you may experience breaking changes outside of SemVer. Breaking changes may occur on minor release (we'll try to avoid making breaking beta API changes on patch releases).

**Old:**

Expand Down Expand Up @@ -149,23 +149,78 @@ ai.defineFlow({name: 'banana'}, async (input, { context }) => {
})
```

also, if you've been using auth policies, they've been removed from `defineFlow` and are not handled depending on the server you're using:
`onFlow` moved to `firebase-functions/https` package and got renamed to `onCallGenkit`. Here's how to use it:

**Old**

```ts
import { onFlow } from "@genkit-ai/firebase/functions";

export const generatePoem = onFlow(
ai,
{
name: "jokeTeller",
inputSchema: z.string().nullable(),
outputSchema: z.string(),
streamSchema: z.string(),
},
async (type, streamingCallback) => {
const { stream, response } = await ai.generateStream(
`Tell me a longish ${type ?? "dad"} joke.`
);
for await (const chunk of stream) {
streamingCallback(chunk.text);
}
return (await response).text;
}
);
```

**New**

```ts
import { onCallGenkit } from "firebase-functions/https";
import { defineSecret } from "firebase-functions/params";
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 longish ${type ?? "dad"} joke.`
);
for await (const chunk of stream) {
sendChunk(chunk.text);
}
return (await response).text;
}
);

export const tellJoke = onCallGenkit({ secrets: [apiKey] }, jokeTeller);
```

also, if you've been using auth policies, they've been removed from `defineFlow` and are now handled depending on the server you're using:

**Old:**

```ts
export const simpleFlow = ai.defineFlow(
{
name: 'simpleFlow',
inputSchema: z.object({ uid: z.string() }),
outputSchema: z.string(),
authPolicy: (auth, input) => {
if (!auth) {
throw new Error('Authorization required.');
}
if (input.uid !== auth.uid) {
throw new Error('You may only summarize your own profile data.');
}
// auth policy
},
},
async (input) => {
Expand All @@ -192,8 +247,6 @@ const context: ContextProvider<Context> = (req: RequestData) => {
export const simpleFlow = ai.defineFlow(
{
name: 'simpleFlow',
inputSchema: z.object({ uid: z.string() }),
outputSchema: z.string(),
},
async (input, { context }) => {
if (!context.auth) {
Expand All @@ -220,75 +273,33 @@ startFlowServer(
flows: [withContextProvider(simpleFlow, context)],
port: 8080
);

```

Refer to [auth documentation](https://firebase.google.com/docs/genkit/auth) for more details.

`onFlow` moved to `firebase-functions/https` package and got renamed to `onCallGenkit`. Here's how to use it:

**Old**

```ts
import { onFlow } from "@genkit-ai/firebase/functions";

export const generatePoem = onFlow(
ai,
{
name: "jokeTeller",
inputSchema: z.string().nullable(),
outputSchema: z.string(),
streamSchema: z.string(),
},
async (type, streamingCallback) => {
const { stream, response } = await ai.generateStream(
`Tell me a longish ${type ?? "dad"} joke.`
);
for await (const chunk of stream) {
streamingCallback(chunk.text);
}
return (await response).text;
}
);
```

**New**
or with Cloud Functions for Firebase:

```ts
import { onCallGenkit } from "firebase-functions/https";
import { defineSecret } from "firebase-functions/params";
import { genkit, z } from "genkit";
import { genkit } from 'genkit';
import { onCallGenkit } from 'firebase-functions/https';

const apiKey = defineSecret("GOOGLE_GENAI_API_KEY");
const ai = genkit({ ... });;

const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash,
const simpleFlow = ai.defineFlow({
name: 'simpleFlow',
}, async (input) => {
// Flow logic here...
});

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 longish ${type ?? "dad"} joke.`
);
for await (const chunk of stream) {
sendChunk(chunk.text);
}
return (await response).text;
}
);

export const tellJoke = onCallGenkit({ secrets: [apiKey] }, jokeTeller);
export const selfSummary = onCallGenkit({
authPolicy: (auth, data) => auth?.token?.['email_verified'] && auth?.token?.['admin'],
}, simpleFlow);
```


## Prompts

We've make several changes and improvements to prompts.
We've made several changes and improvements to prompts.

You can define separate templates for prompt and system messages:

Expand All @@ -311,7 +322,7 @@ or you can define multi-message prompts in the messages field
```ts
const hello = ai.definePrompt({
name: 'hello',
messages: '{{ role "system" }}talk like a pirate. {{ role "user" }} hello {{ name }}',
messages: '{{ role "system" }} talk like a pirate. {{ role "user" }} hello {{ name }}',
input: {
schema: z.object({
name: z.string()
Expand Down

0 comments on commit c47c69c

Please sign in to comment.