Module: Langchain::Prompt::Loading::ClassMethods

Defined in:
lib/langchain/prompt/loading.rb

Instance Method Summary collapse

Instance Method Details

#load_few_shot_prompt(config) ⇒ FewShotPromptTemplate

Loads a prompt template with the given configuration.

Parameters:

  • config (Hash)

    A hash containing the configuration for the prompt.

Returns:



63
64
65
66
67
# File 'lib/langchain/prompt/loading.rb', line 63

def load_few_shot_prompt(config)
  prefix, suffix, example_prompt, examples, input_variables = config.values_at("prefix", "suffix", "example_prompt", "examples", "input_variables")
  example_prompt = load_prompt(example_prompt)
  FewShotPromptTemplate.new(prefix: prefix, suffix: suffix, example_prompt: example_prompt, examples: examples, input_variables: input_variables)
end

#load_from_path(file_path:) ⇒ Object

Load prompt from file.

Parameters:

  • file_path (String, Pathname)

    The path of the file to read the configuration data from.

Returns:

  • (Object)

    The loaded prompt loaded.

Raises:

  • (ArgumentError)

    If the file type of the specified file path is not supported.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/langchain/prompt/loading.rb', line 29

def load_from_path(file_path:)
  file_path = file_path.is_a?(String) ? Pathname.new(file_path) : file_path

  case file_path.extname
  when ".json"
    config = JSON.parse(File.read(file_path))
  when ".yaml", ".yml"
    config = YAML.safe_load_file(file_path)
  else
    raise ArgumentError, "Got unsupported file type #{file_path.extname}"
  end

  load_from_config(config)
end

#load_prompt(config) ⇒ PromptTemplate

Loads a prompt template with the given configuration.

Parameters:

  • config (Hash)

    A hash containing the configuration for the prompt.

Returns:



51
52
53
54
# File 'lib/langchain/prompt/loading.rb', line 51

def load_prompt(config)
  template, input_variables = config.values_at("template", "input_variables")
  PromptTemplate.new(template: template, input_variables: input_variables)
end