Class: Langchain::Prompt::PromptTemplate

Inherits:
Base
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • template (String)

    The prompt template.

  • input_variables (Array<String>)

    A list of the names of the variables the prompt template expects.

  • validate_template (Boolean) (defaults to: true)

    Whether or not to try validating the template.



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_variablesObject (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

#templateObject (readonly)

Returns the value of attribute template.



36
37
38
# File 'lib/langchain/prompt/prompt_template.rb', line 36

def template
  @template
end

#validate_templateObject (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.

Parameters:

  • template (String)

    The template to use

Returns:

  • (Object)

    A new instance of the class



89
90
91
# File 'lib/langchain/prompt/prompt_template.rb', line 89

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.

Parameters:

  • kwargs (Hash)

    Any arguments to be passed to the prompt template.

Returns:

  • (String)

    A formatted string.



59
60
61
62
63
# File 'lib/langchain/prompt/prompt_template.rb', line 59

def format(**kwargs)
  result = @template
  kwargs.each { |key, value| result = result.gsub(/\{#{key}\}/, value.to_s) }
  result.gsub(/{{/, "{").gsub(/}}/, "}")
end

#prompt_typeString

Returns the key type of prompt as a string.

Returns:

  • (String)

    the prompt type key



70
71
72
# File 'lib/langchain/prompt/prompt_template.rb', line 70

def prompt_type
  "prompt"
end

#to_hObject



74
75
76
77
78
79
80
# File 'lib/langchain/prompt/prompt_template.rb', line 74

def to_h
  {
    _type: prompt_type,
    input_variables: @input_variables,
    template: @template
  }
end