Class: Langchain::Prompt::Base Abstract
- Inherits:
-
Object
- Object
- Langchain::Prompt::Base
- Defined in:
- lib/langchain/prompt/base.rb
Overview
Prompts are structured inputs to the LLMs. Prompts provide instructions, context and other user input that LLMs use to generate responses.
Direct Known Subclasses
Class Method Summary collapse
-
.extract_variables_from_template(template) ⇒ Array<String>
Extracts variables from a template string.
Instance Method Summary collapse
- #format(**kwargs) ⇒ Object
-
#prompt_type ⇒ String
The type of the prompt.
-
#save(file_path:) ⇒ void
Save the object to a file in JSON or YAML format.
-
#to_h ⇒ Hash
A hash representation of the prompt.
-
#validate(template:, input_variables:) ⇒ void
Validate the input variables against the template.
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.
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
11 12 13 |
# File 'lib/langchain/prompt/base.rb', line 11 def format(**kwargs) raise NotImplementedError end |
#prompt_type ⇒ String
Returns the type of the prompt.
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.
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_h ⇒ Hash
Returns a hash representation of the prompt.
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.
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 |