Optionaloptions: GenkitOptionsReadonlyflowsList of flows that have been registered in this instance.
ReadonlyoptionsDeveloper-configured options.
ReadonlyregistryChecks the status of of a given operation. Returns a new operation which will contain the updated status.
let operation = await ai.generateOperation({
model: googleAI.model('veo-2.0-generate-001'),
prompt: 'A banana riding a bicycle.',
});
while (!operation.done) {
operation = await ai.checkOperation(operation!);
await new Promise((resolve) => setTimeout(resolve, 5000));
}
Returns current action (or flow) invocation context. Can be used to access things like auth
data set by HTTP server frameworks. If invoked outside of an action (e.g. flow or tool) will
return undefined.
Defines a new background model and adds it to the registry.
Defines and registers a dynamic action provider (e.g. mcp host)
Creates embedder model for the provided EmbedderFn model implementation.
Creates evaluator action for the provided EvaluatorFn implementation.
Defines and registers a flow function.
create a handlebars helper (https://handlebarsjs.com/guide/block-helpers.html) to be used in dotprompt templates.
Creates an indexer action for the provided IndexerFn implementation.
Defines and registers a schema from a JSON schema.
Defined schemas can be referenced by name in prompts in place of inline schemas.
Defines a new model and adds it to the registry.
Defines a new model and adds it to the registry.
Creates a handlebars partial (https://handlebarsjs.com/guide/partials.html) to be used in dotprompt templates.
Defines and registers a prompt based on a function.
This is an alternative to defining and importing a .prompt file, providing the most advanced control over how the final request to the model is made.
Prompt metadata including model, model params, input/output schemas, etc
OptionaltemplateOrFn: string | PromptFn<I, z.ZodTypeAny>Creates a reranker action for the provided RerankerFn implementation.
Creates a retriever action for the provided RetrieverFn implementation.
Defines and registers a schema from a Zod schema.
Defined schemas can be referenced by name in prompts in place of inline schemas.
defineSimpleRetriever makes it easy to map existing data into documents that can be used for prompt augmentation.
A Genkit retriever.
Defines and registers a tool that can return multiple parts of content.
Tools can be passed to models by name or value during generate calls to be called automatically based on the prompt and situation.
Defines and registers a tool.
Tools can be passed to models by name or value during generate calls to be called automatically based on the prompt and situation.
Defines a dynamic tool. Dynamic tools are just like regular tools (Genkit.defineTool) but will not be registered in the Genkit registry and can be defined dynamically at runtime.
Optionalfn: ToolFn<I, O>Embeds the given content using the specified embedder.
A veneer for interacting with embedder models in bulk.
Evaluates the given dataset using the specified evaluator.
Make a generate call to the default model with a simple text prompt.
const ai = genkit({
plugins: [googleAI()],
model: googleAI.model('gemini-flash-latest'), // default model
})
const { text } = await ai.generate('hi');
Make a generate call to the default model with a multipart request.
const ai = genkit({
plugins: [googleAI()],
model: googleAI.model('gemini-flash-latest'), // default model
})
const { text } = await ai.generate([
{ media: {url: 'http://....'} },
{ text: 'describe this image' }
]);
Generate calls a generative model based on the provided prompt and configuration. If
messages is provided, the generation will include a conversation history in its
request. If tools are provided, the generate method will automatically resolve
tool calls returned from the model unless returnToolRequests is set to true.
See GenerateOptions for detailed information about available options.
const ai = genkit({
plugins: [googleAI()],
})
const { text } = await ai.generate({
system: 'talk like a pirate',
prompt: [
{ media: { url: 'http://....' } },
{ text: 'describe this image' }
],
messages: conversationHistory,
tools: [ userInfoLookup ],
model: googleAI.model('gemini-flash-latest'),
});
Make a streaming generate call to the default model with a simple text prompt.
const ai = genkit({
plugins: [googleAI()],
model: googleAI.model('gemini-flash-latest'), // default model
})
const { response, stream } = ai.generateStream('hi');
for await (const chunk of stream) {
console.log(chunk.text);
}
console.log((await response).text);
Make a streaming generate call to the default model with a multipart request.
const ai = genkit({
plugins: [googleAI()],
model: googleAI.model('gemini-flash-latest'), // default model
})
const { response, stream } = ai.generateStream([
{ media: {url: 'http://....'} },
{ text: 'describe this image' }
]);
for await (const chunk of stream) {
console.log(chunk.text);
}
console.log((await response).text);
Streaming generate calls a generative model based on the provided prompt and configuration. If
messages is provided, the generation will include a conversation history in its
request. If tools are provided, the generate method will automatically resolve
tool calls returned from the model unless returnToolRequests is set to true.
See GenerateOptions for detailed information about available options.
const ai = genkit({
plugins: [googleAI()],
})
const { response, stream } = ai.generateStream({
system: 'talk like a pirate',
prompt: [
{ media: { url: 'http://....' } },
{ text: 'describe this image' }
],
messages: conversationHistory,
tools: [ userInfoLookup ],
model: googleAI.model('gemini-flash-latest'),
});
for await (const chunk of stream) {
console.log(chunk.text);
}
console.log((await response).text);
Indexes documents using the provided indexer.
Looks up a prompt by name (and optionally variant). Can be used to lookup
.prompt files or prompts previously defined with Genkit.definePrompt
Optionaloptions: { variant?: string }Reranks documents from a RerankerArgument based on the provided query.
Retrieves documents from the retriever based on the provided query.
A flow step that executes the provided function. Each run step is recorded separately in the trace.
ai.defineFlow('hello', async() => {
await ai.run('step1', async () => {
// ... step 1
});
await ai.run('step2', async () => {
// ... step 2
});
return result;
})
A flow step that executes the provided function. Each run step is recorded separately in the trace.
ai.defineFlow('hello', async(name) => {
const greeting = await ai.run('step1', name, async (input) => {
return `Hello, ${input}!`;
});
const result = await ai.run('step2', greeting, async (input) => {
// ... step 2
});
return result;
Stops all servers.
Genkitencapsulates a single Genkit instance including the Registry, ReflectionServer, FlowServer, and configuration.Do not instantiate this class directly. Use genkit.
Registry keeps track of actions, flows, tools, and many other components. Reflection server exposes an API to inspect the registry and trigger executions of actions in the registry. Flow server exposes flows as HTTP endpoints for production use.
There may be multiple Genkit instances in a single codebase.