Class: Langchain::Prompt::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/langchain/prompt/base.rb

Overview

This class is abstract.

Prompts are structured inputs to the LLMs. Prompts provide instructions, context and other user input that LLMs use to generate responses.

Direct Known Subclasses

FewShotPromptTemplate, PromptTemplate

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_variables_from_template(template) ⇒ Array<String>

Extracts variables from a template string.

This method takes a template string and returns an array of input variable names contained within the template. Input variables are defined as text enclosed in curly braces (e.g. \{variable_name\}).

Content within two consecutive curly braces (e.g. \{\{ignore_me}}) are ignored.

Parameters:

  • template (String)

    The template string to extract variables from.

Returns:

  • (Array<String>)

    An array of input variable names.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/langchain/prompt/base.rb', line 83

def self.extract_variables_from_template(template)
  input_variables = []
  scanner = StringScanner.new(template)

  while scanner.scan_until(/\{([^}]*)\}/)
    variable = scanner[1].strip
    input_variables << variable unless variable.empty? || variable[0] == "{"
  end

  input_variables
end

Instance Method Details

#format(**kwargs) ⇒ Object

Raises:

  • (NotImplementedError)


11
12
13
# File 'lib/langchain/prompt/base.rb', line 11

def format(**kwargs)
  raise NotImplementedError
end

#prompt_typeString

Returns the type of the prompt.

Returns:

  • (String)

    the type of the prompt

Raises:

  • (NotImplementedError)


16
17
18
# File 'lib/langchain/prompt/base.rb', line 16

def prompt_type
  raise NotImplementedError
end

#save(file_path:) ⇒ void

This method returns an undefined value.

Save the object to a file in JSON or YAML format.

Parameters:

  • file_path (String, Pathname)

    The path to the file to save the object to

Raises:

  • (ArgumentError)

    If file_path doesn’t end with .json or .yaml or .yml



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/langchain/prompt/base.rb', line 55

def save(file_path:)
  save_path = file_path.is_a?(String) ? Pathname.new(file_path) : file_path
  directory_path = save_path.dirname
  FileUtils.mkdir_p(directory_path) unless directory_path.directory?

  case save_path.extname
  when ".json"
    File.write(file_path, to_h.to_json)
  when ".yaml", ".yml"
    File.write(file_path, to_h.to_yaml)
  else
    raise ArgumentError, "#{file_path} must be json or yaml file"
  end
end

#to_hHash

Returns a hash representation of the prompt.

Returns:

  • (Hash)

    a hash representation of the prompt

Raises:

  • (NotImplementedError)


21
22
23
# File 'lib/langchain/prompt/base.rb', line 21

def to_h
  raise NotImplementedError
end

#validate(template:, input_variables:) ⇒ void

This method returns an undefined value.

Validate the input variables against the template.

Parameters:

  • template (String)

    The template to validate against.

  • input_variables (Array<String>)

    The input variables to validate.

Raises:

  • (ArgumentError)

    If there are missing or extra variables.



35
36
37
38
39
40
41
42
43
44
# File 'lib/langchain/prompt/base.rb', line 35

def validate(template:, input_variables:)
  input_variables_set = input_variables.uniq
  variables_from_template = Langchain::Prompt::Base.extract_variables_from_template(template)

  missing_variables = variables_from_template - input_variables_set
  extra_variables = input_variables_set - variables_from_template

  raise ArgumentError, "Missing variables: #{missing_variables}" if missing_variables.any?
  raise ArgumentError, "Extra variables: #{extra_variables}" if extra_variables.any?
end