Creates a Lambda handler for a Genkit flow.
This function wraps a Genkit flow to create an AWS Lambda handler that:
The Genkit flow to wrap
A Lambda handler function
import { genkit, z } from 'genkit';
import { onCallGenkit } from 'genkitx-aws-bedrock';
import { awsBedrock, amazonNovaProV1 } from 'genkitx-aws-bedrock';
const ai = genkit({
plugins: [awsBedrock()],
model: amazonNovaProV1(),
});
const myFlow = ai.defineFlow(
{ name: 'myFlow', inputSchema: z.string(), outputSchema: z.string() },
async (input) => {
const { text } = await ai.generate({ prompt: input });
return text;
}
);
export const handler = onCallGenkit(myFlow);
import { UserFacingError, getContext } from 'genkit';
import type { ContextProvider } from 'genkit/context';
interface AuthContext {
auth: { user: { id: string; name: string } };
}
const authProvider: ContextProvider<AuthContext> = async (req) => {
const token = req.headers['authorization'];
if (!token) {
throw new UserFacingError('UNAUTHENTICATED', 'Missing auth token');
}
const user = await verifyToken(token);
return { auth: { user } };
};
// In your flow, access context via getContext()
const myFlow = ai.defineFlow(
{ name: 'myFlow', inputSchema: z.string(), outputSchema: z.string() },
async (input, { context }) => {
const { auth } = context;
console.log('User:', auth.user.name);
// ...
}
);
export const handler = onCallGenkit(
{ contextProvider: authProvider },
myFlow
);
Creates a Lambda handler for a Genkit flow with options.
Configuration options for the Lambda handler
The Genkit flow to wrap
A Lambda handler function, or a StreamifyHandler when streaming: true
Creates a Lambda handler for a Genkit flow.
This function wraps a Genkit flow to create an AWS Lambda handler that:
The Genkit flow to wrap
A Lambda handler function
import { genkit, z } from 'genkit';
import { onCallGenkit } from 'genkitx-aws-bedrock';
import { awsBedrock, amazonNovaProV1 } from 'genkitx-aws-bedrock';
const ai = genkit({
plugins: [awsBedrock()],
model: amazonNovaProV1(),
});
const myFlow = ai.defineFlow(
{ name: 'myFlow', inputSchema: z.string(), outputSchema: z.string() },
async (input) => {
const { text } = await ai.generate({ prompt: input });
return text;
}
);
export const handler = onCallGenkit(myFlow);
import { UserFacingError, getContext } from 'genkit';
import type { ContextProvider } from 'genkit/context';
interface AuthContext {
auth: { user: { id: string; name: string } };
}
const authProvider: ContextProvider<AuthContext> = async (req) => {
const token = req.headers['authorization'];
if (!token) {
throw new UserFacingError('UNAUTHENTICATED', 'Missing auth token');
}
const user = await verifyToken(token);
return { auth: { user } };
};
// In your flow, access context via getContext()
const myFlow = ai.defineFlow(
{ name: 'myFlow', inputSchema: z.string(), outputSchema: z.string() },
async (input, { context }) => {
const { auth } = context;
console.log('User:', auth.user.name);
// ...
}
);
export const handler = onCallGenkit(
{ contextProvider: authProvider },
myFlow
);
Implementation of onCallGenkit