Core concepts
Composing prompts
The Roseflow Prompt class provides a flexible way to create and extend prompts for your interactions with LLMs. In this documentation, we will go through a set of test classes demonstrating how to create and customize prompts using the provided Prompt class.
Basic Usage
Roseflow comes with a pre-defined Prompt class that serves as the base class for creating custom prompts. You can create a new prompt by extending the Prompt
class and overriding its template method.
Example: SimplePrompt
class SimplePrompt < Roseflow::Prompt
def template
plain "Hello, AI!"
end
end
To use the prompt, simply create an instance and use its #call
method to render the prompt.
prompt = SimplePrompt.new
prompt.call # Output: "Hello, AI!"
Adding Variables
You can add variables to the prompt by defining an initialize
method in your custom class and passing arguments to it. You can then use these variables in your prompt.
Example: VariablePrompt
class VariablePrompt < Roseflow::Prompt
def initialize(name:)
@name = name
end
def template
plain "Hello, #{@name}!"
end
end
This example creates a new VariablePrompt class that generates a personalized greeting. To use the prompt, create an instance, passing in the required arguments, and call it.
prompt = VariablePrompt.new(name: "John")
prompt.call # Output: "Hello, John!"
Nested Prompts
You can also nest prompts or prompt templates within other prompts to create more complex prompt templates. To nest a prompt, use the render
method followed by the nested prompt's class or its instance.
Example: PromptWithNestedPrompt
class PromptWithNestedPrompt < Roseflow::Prompt
def initialize(name:)
@name = name
end
def template
render NestedPromptTemplate
plain "Hello, #{@name}!"
end
end
This example creates a new PromptWithNestedPrompt
class that combines a nested NestedPromptTemplate
prompt and a personalized greeting.
prompt = PromptWithNestedPrompt.new(name: "John")
prompt.call # Output: "Instructions: Respond to my commands. Hello, John!"
Nested Prompts with Variables
You can also pass variables to nested prompts by creating an instance of the nested prompt and passing the required arguments.
Example: PromptWithNestedPromptWithVariables
class PromptWithNestedPromptWithVariables < Roseflow::Prompt
def initialize(name:, persona:)
@name = name
@persona = persona
end
def template
render NestedPromptWithVariableTemplate.new(persona: @persona)
plain "Hello, #{@name}!"
end
end
This example creates a new PromptWithNestedPromptWithVariables
class that combines a nested NestedPromptWithVariableTemplate
prompt with a variable and a personalized greeting.
prompt = PromptWithNestedPromptWithVariables.new(name: "John", persona: "a wizard")
prompt.call # Output: "Instructions: Respond to my commands as a wizard. Hello, John!"