PromptBuilder
Typesafe API for creating prompts
Last updated
Typesafe API for creating prompts
Last updated
The Prompt
class is a simple class that represents a string template with named placeholders. It provides a toString
method that replaces the placeholders with values from an input object, producing a new string.
To use the Prompt
class, you'll need to create an instance of it with a template string that contains named placeholders. Here's an example:
In this example, we create a new Prompt
instance with a template string that contains three named placeholders: {{me}}
, {{num}}
, and {{jokeType}}
. We also provide an input object with values for each of these placeholders.
When we call the toString
method on the prompt
instance, the placeholders are replaced with the corresponding values from the input object, producing the output string literally typed as "Tell Brett 1 funny joke"
.
The template string passed to the Prompt
constructor can contain named placeholders enclosed in double curly braces ({{}}
). The names inside the placeholders should match the keys of the input object passed to the Prompt
instance.
For example, if the input object has a property named firstName
, you can use the {{firstName}}
placeholder in the template string to include the value of that property in the output string.
If you want to validate the input object against a specific type, you can use the addInputValidation
method of the PromptBuilder
class to create a new PromptBuilder
instance with the validation applied. Here's an example:
In this example, we create a new PromptBuilder
instance with a template string that contains three named placeholders. We then use the addInputValidation
method to create a new PromptBuilder
instance that validates the input object against a specific type.
The type is defined as an object with properties that correspond to the named placeholders in the template string. The values of these properties are the expected types of the corresponding input values.
When we call the build
method on the validatedBuilder
instance with an input object that matches the expected type, we get a new Prompt
instance with the placeholders replaced by the input values.
If the input object does not match the expected type, a type error will be thrown at compiletime.
addInputValidation only validates at compile time. To check values at runtime use addZodInputValidation
The PromptBuilder
class also provides a method called addZodInputValidation
that allows you to use the Zod library for input validation. This method creates a new PromptBuilder
instance with the Zod validation schema applied.
Here's an example of how to use the addZodInputValidation
method:
In this example, we create a new PromptBuilder
instance with a template string that contains three named placeholders. We then define a Zod schema for the input object, specifying the expected types for each property.
Next, we use the addZodValidation
method to create a new PromptBuilder
instance that validates the input object against the Zod schema.
When we call the build
method on the validatedBuilder
instance with an input object that matches the expected schema, we get a new Prompt
instance with the placeholders replaced by the input values.
If the input object does not match the expected schema, a typescript error will be thrown at build time and a Zod validation error will be thrown at runtime.