Class Prompt<I>

java.lang.Object
com.google.genkit.ai.Prompt<I>
Type Parameters:
I - the input type for the prompt
All Implemented Interfaces:
Action<I,ModelRequest,Void>, Registerable

public class Prompt<I> extends Object implements Action<I,ModelRequest,Void>
Prompt is a template that generates ModelRequests from input variables. Prompts are registered as actions and can be rendered with different input values to create model requests.
  • Constructor Details

    • Prompt

      public Prompt(String name, String model, String template, Map<String,Object> inputSchema, GenerationConfig config, Class<I> inputClass, BiFunction<ActionContext,I,ModelRequest> renderer)
      Creates a new Prompt.
      Parameters:
      name - the prompt name
      model - the default model name
      template - the prompt template
      inputSchema - the input JSON schema
      config - the default generation config
      inputClass - the input class for JSON deserialization
      renderer - the function that renders the prompt
  • Method Details

    • builder

      public static <I> Prompt.Builder<I> builder()
      Creates a builder for Prompt.
      Type Parameters:
      I - the input type
      Returns:
      a new builder
    • getName

      public String getName()
      Description copied from interface: Action
      Returns the name of the action.
      Specified by:
      getName in interface Action<I,ModelRequest,Void>
      Returns:
      the action name
    • getType

      public ActionType getType()
      Description copied from interface: Action
      Returns the type of the action.
      Specified by:
      getType in interface Action<I,ModelRequest,Void>
      Returns:
      the action type
    • getDesc

      public ActionDesc getDesc()
      Description copied from interface: Action
      Returns the descriptor of the action containing metadata, schemas, etc.
      Specified by:
      getDesc in interface Action<I,ModelRequest,Void>
      Returns:
      the action descriptor
    • run

      public ModelRequest run(ActionContext ctx, I input) throws GenkitException
      Description copied from interface: Action
      Runs the action with the given input.
      Specified by:
      run in interface Action<I,ModelRequest,Void>
      Parameters:
      ctx - the action context
      input - the input to the action
      Returns:
      the output of the action
      Throws:
      GenkitException - if the action fails
    • run

      public ModelRequest run(ActionContext ctx, I input, Consumer<Void> streamCallback) throws GenkitException
      Description copied from interface: Action
      Runs the action with the given input and streaming callback.
      Specified by:
      run in interface Action<I,ModelRequest,Void>
      Parameters:
      ctx - the action context
      input - the input to the action
      streamCallback - callback for receiving streaming chunks, may be null
      Returns:
      the output of the action
      Throws:
      GenkitException - if the action fails
    • runJson

      public com.fasterxml.jackson.databind.JsonNode runJson(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException
      Description copied from interface: Action
      Runs the action with JSON input and returns JSON output.
      Specified by:
      runJson in interface Action<I,ModelRequest,Void>
      Parameters:
      ctx - the action context
      input - the JSON input
      streamCallback - callback for receiving streaming JSON chunks, may be null
      Returns:
      the JSON output
      Throws:
      GenkitException - if the action fails
    • runJsonWithTelemetry

      public ActionRunResult<com.fasterxml.jackson.databind.JsonNode> runJsonWithTelemetry(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException
      Description copied from interface: Action
      Runs the action with JSON input and returns the result with telemetry information.
      Specified by:
      runJsonWithTelemetry in interface Action<I,ModelRequest,Void>
      Parameters:
      ctx - the action context
      input - the JSON input
      streamCallback - callback for receiving streaming JSON chunks, may be null
      Returns:
      the action result including telemetry data
      Throws:
      GenkitException - if the action fails
    • getInputSchema

      public Map<String,Object> getInputSchema()
      Description copied from interface: Action
      Returns the JSON schema for the action's input type.
      Specified by:
      getInputSchema in interface Action<I,ModelRequest,Void>
      Returns:
      the input schema as a map, or null if not defined
    • getOutputSchema

      public Map<String,Object> getOutputSchema()
      Description copied from interface: Action
      Returns the JSON schema for the action's output type.
      Specified by:
      getOutputSchema in interface Action<I,ModelRequest,Void>
      Returns:
      the output schema as a map, or null if not defined
    • getMetadata

      public Map<String,Object> getMetadata()
      Description copied from interface: Action
      Returns additional metadata for the action.
      Specified by:
      getMetadata in interface Action<I,ModelRequest,Void>
      Returns:
      the metadata map
    • register

      public void register(Registry registry)
      Description copied from interface: Registerable
      Registers this primitive with the given registry.
      Specified by:
      register in interface Registerable
      Parameters:
      registry - the registry to register with
    • getModel

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

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

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