diff --git a/docs/migrating-from-0.9.md b/docs/migrating-from-0.9.md index cfec75a46..a1267fead 100644 --- a/docs/migrating-from-0.9.md +++ b/docs/migrating-from-0.9.md @@ -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:** @@ -149,7 +149,69 @@ 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:** @@ -157,15 +219,8 @@ also, if you've been using auth policies, they've been removed from `defineFlow` 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) => { @@ -192,8 +247,6 @@ const context: ContextProvider = (req: RequestData) => { export const simpleFlow = ai.defineFlow( { name: 'simpleFlow', - inputSchema: z.object({ uid: z.string() }), - outputSchema: z.string(), }, async (input, { context }) => { if (!context.auth) { @@ -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: @@ -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()