Class: Langchain::Prompt::PromptTemplate
- Defined in:
- lib/langchain/prompt/prompt_template.rb
Overview
Prompt Templates
Create a prompt with one input variable:
prompt = Langchain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke.", input_variables: ["adjective"])
prompt.format(adjective: "funny") # "Tell me a funny joke."
Create a prompt with multiple input variables:
prompt = Langchain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke about {content}.", input_variables: ["adjective", "content"])
prompt.format(adjective: "funny", content: "chickens") # "Tell me a funny joke about chickens."
Creating a PromptTemplate using just a prompt and no input_variables:
prompt = Langchain::Prompt::PromptTemplate.from_template("Tell me a {adjective} joke about {content}.")
prompt.input_variables # ["adjective", "content"]
prompt.format(adjective: "funny", content: "chickens") # "Tell me a funny joke about chickens."
Save prompt template to JSON file:
prompt.save(file_path: "spec/fixtures/prompt/prompt_template.json")
Loading a new prompt template using a JSON file:
prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.json")
prompt.input_variables # ["adjective", "content"]
Loading a new prompt template using a YAML file:
prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.yaml")
prompt.input_variables #=> ["adjective", "content"]
Instance Attribute Summary collapse
-
#input_variables ⇒ Object
readonly
Returns the value of attribute input_variables.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
-
#validate_template ⇒ Object
readonly
Returns the value of attribute validate_template.
Class Method Summary collapse
-
.from_template(template) ⇒ Object
Creates a new instance of the class using the given template.
Instance Method Summary collapse
-
#format(**kwargs) ⇒ String
Format the prompt with the inputs.
-
#initialize(template:, input_variables:, validate_template: true) ⇒ PromptTemplate
constructor
Initializes a new instance of the class.
-
#prompt_type ⇒ String
Returns the key type of prompt as a string.
- #to_h ⇒ Object
Methods inherited from Base
extract_variables_from_template, #save, #validate
Constructor Details
#initialize(template:, input_variables:, validate_template: true) ⇒ PromptTemplate
Initializes a new instance of the class.
45 46 47 48 49 50 51 |
# File 'lib/langchain/prompt/prompt_template.rb', line 45 def initialize(template:, input_variables:, validate_template: true) @template = template @input_variables = input_variables @validate_template = validate_template validate(template: @template, input_variables: @input_variables) if @validate_template end |
Instance Attribute Details
#input_variables ⇒ Object (readonly)
Returns the value of attribute input_variables.
36 37 38 |
# File 'lib/langchain/prompt/prompt_template.rb', line 36 def input_variables @input_variables end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
36 37 38 |
# File 'lib/langchain/prompt/prompt_template.rb', line 36 def template @template end |
#validate_template ⇒ Object (readonly)
Returns the value of attribute validate_template.
36 37 38 |
# File 'lib/langchain/prompt/prompt_template.rb', line 36 def validate_template @validate_template end |
Class Method Details
.from_template(template) ⇒ Object
Creates a new instance of the class using the given template.
90 91 92 |
# File 'lib/langchain/prompt/prompt_template.rb', line 90 def self.from_template(template) new(template: template, input_variables: extract_variables_from_template(template)) end |
Instance Method Details
#format(**kwargs) ⇒ String
Format the prompt with the inputs. Double {{}}
replaced with single {}
to adhere to f-string spec.
59 60 61 62 63 64 |
# File 'lib/langchain/prompt/prompt_template.rb', line 59 def format(**kwargs) result = @template result = result.gsub(/{{/, "{").gsub(/}}/, "}") kwargs.each { |key, value| result = result.gsub(/\{#{key}\}/, value.to_s) } result end |
#prompt_type ⇒ String
Returns the key type of prompt as a string.
71 72 73 |
# File 'lib/langchain/prompt/prompt_template.rb', line 71 def prompt_type "prompt" end |
#to_h ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/langchain/prompt/prompt_template.rb', line 75 def to_h { _type: prompt_type, input_variables: @input_variables, template: @template } end |