Class DotPrompt<I>

java.lang.Object
com.google.genkit.prompt.DotPrompt<I>

public class DotPrompt<I> extends Object
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 Details

    • DotPrompt

      public DotPrompt(String name, String model, String template, Map<String,Object> inputSchema, GenerationConfig config)
      Creates a new DotPrompt.
      Parameters:
      name - the prompt name
      model - the default model name
      template - the Handlebars template
      inputSchema - the input JSON schema
      config - the default generation config
  • Method Details

    • registerPartial

      public static void registerPartial(String name, String source) throws GenkitException
      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

      public static void loadPartialFromResource(String resourcePath) throws GenkitException
      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

      public static Set<String> getRegisteredPartialNames()
      Returns the names of all registered partials.
      Returns:
      set of partial names
    • loadFromResource

      public static <I> DotPrompt<I> loadFromResource(String resourcePath) throws GenkitException
      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

      public static <I> DotPrompt<I> parse(String name, String content) throws GenkitException
      Parses a DotPrompt from its string content.
      Type Parameters:
      I - the input type
      Parameters:
      name - the prompt name
      content - the prompt file content
      Returns:
      the parsed DotPrompt
      Throws:
      GenkitException - if parsing fails
    • render

      public String render(I input) throws GenkitException
      Renders the prompt with the given input.
      Parameters:
      input - the input data
      Returns:
      the rendered prompt text
      Throws:
      GenkitException - if rendering fails
    • toModelRequest

      public ModelRequest toModelRequest(I input) throws GenkitException
      Renders the prompt and creates a ModelRequest.
      Parameters:
      input - the input data
      Returns:
      the model request
      Throws:
      GenkitException - if rendering fails
    • toPrompt

      public Prompt<I> toPrompt(Class<I> inputClass)
      Creates a Prompt action from this DotPrompt.
      Parameters:
      inputClass - the input class
      Returns:
      the Prompt action
    • register

      public void register(Registry registry, Class<I> inputClass)
      Registers this DotPrompt as an action.
      Parameters:
      registry - the registry
      inputClass - the input class
    • generate

      public ModelResponse generate(Registry registry, I input) throws GenkitException
      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 model
      input - 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 model
      input - the prompt input
      options - 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 model
      input - the prompt input
      options - optional generation options
      streamCallback - callback for streaming chunks
      Returns:
      the model response
      Throws:
      GenkitException - if generation fails
    • getName

      public String getName()
    • getModel

      public String getModel()
    • getTemplate

      public String getTemplate()
    • getInputSchema

      public Map<String,Object> getInputSchema()
    • getConfig

      public GenerationConfig getConfig()