Package com.google.genkit.prompt
Class DotPrompt<I>
java.lang.Object
com.google.genkit.prompt.DotPrompt<I>
DotPrompt provides support for .prompt files using Handlebars templating.
.prompt files are structured text files with YAML frontmatter containing
configuration options and a Handlebars template body.
Partials are supported by files starting with underscore (e.g.,
_style.prompt). Partials are automatically loaded when referenced in
templates.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGenerates a response using this prompt with the given registry.generate(Registry registry, I input, GenerateOptions options) Generates a response using this prompt with custom options.generate(Registry registry, I input, GenerateOptions options, Consumer<ModelResponseChunk> streamCallback) Generates a response using this prompt with streaming.getModel()getName()Returns the names of all registered partials.static <I> DotPrompt<I> loadFromResource(String resourcePath) Loads a DotPrompt from a resource file.static voidloadPartialFromResource(String resourcePath) Loads and registers a partial from a resource file.static <I> DotPrompt<I> Parses a DotPrompt from its string content.voidRegisters this DotPrompt as an action.static voidregisterPartial(String name, String source) Registers a partial template that can be included in other prompts.Renders the prompt with the given input.toModelRequest(I input) Renders the prompt and creates a ModelRequest.Creates a Prompt action from this DotPrompt.
-
Constructor Details
-
DotPrompt
public DotPrompt(String name, String model, String template, Map<String, Object> inputSchema, GenerationConfig config) Creates a new DotPrompt.- Parameters:
name- the prompt namemodel- the default model nametemplate- the Handlebars templateinputSchema- the input JSON schemaconfig- the default generation config
-
-
Method Details
-
registerPartial
Registers a partial template that can be included in other prompts. Partials are referenced using {{>partialName}} syntax in templates.- Parameters:
name- the partial name (without underscore prefix or .prompt extension)source- the partial template source- Throws:
GenkitException- if registration fails
-
loadPartialFromResource
Loads and registers a partial from a resource file. The partial name is derived from the filename (without underscore prefix and .prompt extension).- Parameters:
resourcePath- the resource path (e.g., "/prompts/_style.prompt")- Throws:
GenkitException- if loading fails
-
getRegisteredPartialNames
Returns the names of all registered partials.- Returns:
- set of partial names
-
loadFromResource
Loads a DotPrompt from a resource file. Automatically loads any partials referenced in the template from the same directory. Partials should be named with underscore prefix (e.g., _style.prompt).- Type Parameters:
I- the input type- Parameters:
resourcePath- the resource path- Returns:
- the loaded DotPrompt
- Throws:
GenkitException- if loading fails
-
parse
Parses a DotPrompt from its string content.- Type Parameters:
I- the input type- Parameters:
name- the prompt namecontent- the prompt file content- Returns:
- the parsed DotPrompt
- Throws:
GenkitException- if parsing fails
-
render
Renders the prompt with the given input.- Parameters:
input- the input data- Returns:
- the rendered prompt text
- Throws:
GenkitException- if rendering fails
-
toModelRequest
Renders the prompt and creates a ModelRequest.- Parameters:
input- the input data- Returns:
- the model request
- Throws:
GenkitException- if rendering fails
-
toPrompt
Creates a Prompt action from this DotPrompt.- Parameters:
inputClass- the input class- Returns:
- the Prompt action
-
register
Registers this DotPrompt as an action.- Parameters:
registry- the registryinputClass- the input class
-
generate
Generates a response using this prompt with the given registry.This method allows generating directly from a DotPrompt without needing to go through ExecutablePrompt. The model is looked up from the registry using the model name specified in the prompt.
- Parameters:
registry- the registry to look up the modelinput- the prompt input- Returns:
- the model response
- Throws:
GenkitException- if generation fails
-
generate
public ModelResponse generate(Registry registry, I input, GenerateOptions options) throws GenkitException Generates a response using this prompt with custom options.- Parameters:
registry- the registry to look up the modelinput- the prompt inputoptions- optional generation options to override prompt defaults- Returns:
- the model response
- Throws:
GenkitException- if generation fails
-
generate
public ModelResponse generate(Registry registry, I input, GenerateOptions options, Consumer<ModelResponseChunk> streamCallback) throws GenkitException Generates a response using this prompt with streaming.- Parameters:
registry- the registry to look up the modelinput- the prompt inputoptions- optional generation optionsstreamCallback- callback for streaming chunks- Returns:
- the model response
- Throws:
GenkitException- if generation fails
-
getName
-
getModel
-
getTemplate
-
getInputSchema
-
getConfig
-