Package com.google.genkit.prompt
Class ExecutablePrompt<I>
java.lang.Object
com.google.genkit.prompt.ExecutablePrompt<I>
- Type Parameters:
I- the input type for the prompt
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()));
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for the generate function.static interfaceFunctional interface for the generateObject function. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGenerates a response using the default model specified in the prompt.<T> Tgenerate(I input, GenerateOptions options) Generates a response with custom options.<T> TGenerates a structured output using the default model specified in the prompt.Gets the generation config.Gets the underlying DotPrompt.getModel()Gets the default model name.getName()Gets the prompt name.Gets the template.voidregister()Registers this prompt as an action in the registry.Renders the prompt template without generating.stream(I input, GenerateOptions options, Consumer<ModelResponseChunk> streamCallback) Generates a response with streaming and custom options.stream(I input, Consumer<ModelResponseChunk> streamCallback) Generates a response with streaming.toModelRequest(I input) Gets the ModelRequest that would be sent to the model.toPrompt()Converts this executable prompt to a Prompt action.withGenerateFunction(ExecutablePrompt.GenerateFunction generateFunction) Sets the generate function to use Genkit.generate() for tool/interrupt support.withGenerateObjectFunction(ExecutablePrompt.GenerateObjectFunction generateObjectFunction) Sets the generateObject function to use Genkit.generateObject() for typed structured output.
-
Constructor Details
-
ExecutablePrompt
Creates a new ExecutablePrompt.- Parameters:
dotPrompt- the underlying DotPromptregistry- the Genkit registryinputClass- the input class for type checking
-
-
Method Details
-
withGenerateFunction
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
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
Generates a structured output using the default model specified in the prompt.- Type Parameters:
T- the output type- Parameters:
input- the prompt inputoutputClass- the class to deserialize the response into- Returns:
- the generated object
- Throws:
GenkitException- if generation fails
-
generate
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 inputoptions- 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 inputstreamCallback- 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 inputoptions- optional generation optionsstreamCallback- callback for streaming chunks- Returns:
- the final model response
- Throws:
GenkitException- if generation fails
-
render
Renders the prompt template without generating.- Parameters:
input- the prompt input- Returns:
- the rendered prompt text
- Throws:
GenkitException- if rendering fails
-
toModelRequest
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
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
Gets the underlying DotPrompt.- Returns:
- the DotPrompt
-
getName
Gets the prompt name.- Returns:
- the name
-
getModel
Gets the default model name.- Returns:
- the model name
-
getTemplate
Gets the template.- Returns:
- the template
-
getConfig
Gets the generation config.- Returns:
- the config
-