Class ExecutablePrompt<I>

java.lang.Object
com.google.genkit.prompt.ExecutablePrompt<I>
Type Parameters:
I - the input type for the prompt

public class ExecutablePrompt<I> extends Object
ExecutablePrompt wraps a DotPrompt and provides direct generation capabilities.

This class allows prompts to be called directly for generation, similar to the JavaScript API: `const response = await helloPrompt({ name: 'John' });`

In Java, this becomes:


 ExecutablePrompt<HelloInput> helloPrompt = genkit.prompt("hello", HelloInput.class);
 ModelResponse response = helloPrompt.generate(new HelloInput("John"));
 

Or for streaming:


 helloPrompt.stream(input, chunk -> System.out.println(chunk.getText()));
 
  • Constructor Details

    • ExecutablePrompt

      public ExecutablePrompt(DotPrompt<I> dotPrompt, Registry registry, Class<I> inputClass)
      Creates a new ExecutablePrompt.
      Parameters:
      dotPrompt - the underlying DotPrompt
      registry - the Genkit registry
      inputClass - the input class for type checking
  • Method Details

    • withGenerateFunction

      public ExecutablePrompt<I> withGenerateFunction(ExecutablePrompt.GenerateFunction generateFunction)
      Sets the generate function to use Genkit.generate() for tool/interrupt support.
      Parameters:
      generateFunction - the generate function
      Returns:
      this for chaining
    • withGenerateObjectFunction

      public ExecutablePrompt<I> withGenerateObjectFunction(ExecutablePrompt.GenerateObjectFunction generateObjectFunction)
      Sets the generateObject function to use Genkit.generateObject() for typed structured output.
      Parameters:
      generateObjectFunction - the generateObject function
      Returns:
      this for chaining
    • generate

      public ModelResponse generate(I input) throws GenkitException
      Generates a response using the default model specified in the prompt.
      Parameters:
      input - the prompt input
      Returns:
      the model response
      Throws:
      GenkitException - if generation fails
    • generate

      public <T> T generate(I input, Class<T> outputClass) throws GenkitException
      Generates a structured output using the default model specified in the prompt.
      Type Parameters:
      T - the output type
      Parameters:
      input - the prompt input
      outputClass - the class to deserialize the response into
      Returns:
      the generated object
      Throws:
      GenkitException - if generation fails
    • generate

      public <T> T generate(I input, GenerateOptions options) throws GenkitException
      Generates a response with custom options.

      If outputClass is set in options, returns a typed object. Otherwise returns ModelResponse. If a generateFunction is set (via Genkit), this uses Genkit.generate() which supports tools and interrupts. Otherwise, it calls the model directly.

      Type Parameters:
      T - the return type (ModelResponse or typed object)
      Parameters:
      input - the prompt input
      options - optional generation options to override prompt defaults
      Returns:
      the model response or typed object
      Throws:
      GenkitException - if generation fails
    • stream

      public ModelResponse stream(I input, Consumer<ModelResponseChunk> streamCallback) throws GenkitException
      Generates a response with streaming.
      Parameters:
      input - the prompt input
      streamCallback - callback for streaming chunks
      Returns:
      the final model response
      Throws:
      GenkitException - if generation fails
    • stream

      public ModelResponse stream(I input, GenerateOptions options, Consumer<ModelResponseChunk> streamCallback) throws GenkitException
      Generates a response with streaming and custom options.
      Parameters:
      input - the prompt input
      options - optional generation options
      streamCallback - callback for streaming chunks
      Returns:
      the final model response
      Throws:
      GenkitException - if generation fails
    • render

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

      public ModelRequest toModelRequest(I input) throws GenkitException
      Gets the ModelRequest that would be sent to the model.
      Parameters:
      input - the prompt input
      Returns:
      the model request
      Throws:
      GenkitException - if conversion fails
    • toPrompt

      public Prompt<I> toPrompt()
      Converts this executable prompt to a Prompt action.
      Returns:
      the Prompt action
    • register

      public void register()
      Registers this prompt as an action in the registry.
    • getDotPrompt

      public DotPrompt<I> getDotPrompt()
      Gets the underlying DotPrompt.
      Returns:
      the DotPrompt
    • getName

      public String getName()
      Gets the prompt name.
      Returns:
      the name
    • getModel

      public String getModel()
      Gets the default model name.
      Returns:
      the model name
    • getTemplate

      public String getTemplate()
      Gets the template.
      Returns:
      the template
    • getConfig

      public GenerationConfig getConfig()
      Gets the generation config.
      Returns:
      the config