Class: Langchain::Prompt::FewShotPromptTemplate

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

Overview

Few Shot Prompt Templates

Create a prompt with a few shot examples:

prompt = Langchain::Prompt::FewShotPromptTemplate.new(
  prefix: "Write antonyms for the following words.",
  suffix: "Input: <code>{adjective}</code>\nOutput:",
  example_prompt: Langchain::Prompt::PromptTemplate.new(
    input_variables: ["input", "output"],
    template: "Input: {input}\nOutput: {output}"
  ),
  examples: [
    { "input": "happy", "output": "sad" },
    { "input": "tall", "output": "short" }
  ],
   input_variables: ["adjective"]
)

prompt.format(adjective: "good")

# Write antonyms for the following words.
#
# Input: happy
# Output: sad
#
# Input: tall
# Output: short
#
# Input: good
# Output:

Save prompt template to JSON file:

prompt.save(file_path: "spec/fixtures/prompt/few_shot_prompt_template.json")

Loading a new prompt template using a JSON file:

prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/few_shot_prompt_template.json")
prompt.prefix # "Write antonyms for the following words."

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

Instance Method Summary collapse

Methods inherited from Base

extract_variables_from_template, #save, #validate

Constructor Details

#initialize(examples:, example_prompt:, input_variables:, suffix:, prefix: "", example_separator: "\n\n", validate_template: true) ⇒ FewShotPromptTemplate

Initializes a new instance of the class.

Parameters:

  • examples (Array<Hash>)

    Examples to format into the prompt.

  • example_prompt (PromptTemplate)

    PromptTemplate used to format an individual example.

  • suffix (String)

    A prompt template string to put after the examples.

  • input_variables (Array<String>)

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

  • example_separator (String) (defaults to: "\n\n")

    String separator used to join the prefix, the examples, and suffix.

  • prefix (String) (defaults to: "")

    A prompt template string to put before the examples.

  • validate_template (Boolean) (defaults to: true)

    Whether or not to try validating the template.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 63

def initialize(
  examples:,
  example_prompt:,
  input_variables:,
  suffix:,
  prefix: "",
  example_separator: "\n\n",
  validate_template: true
)
  @examples = examples
  @example_prompt = example_prompt
  @input_variables = input_variables
  @prefix = prefix
  @suffix = suffix
  @example_separator = example_separator
  @validate_template = validate_template

  validate(template: @prefix + @suffix, input_variables: @input_variables) if @validate_template
end

Instance Attribute Details

#example_promptObject (readonly)

Returns the value of attribute example_prompt.



50
51
52
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 50

def example_prompt
  @example_prompt
end

#example_separatorObject (readonly)

Returns the value of attribute example_separator.



50
51
52
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 50

def example_separator
  @example_separator
end

#examplesObject (readonly)

Returns the value of attribute examples.



50
51
52
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 50

def examples
  @examples
end

#input_variablesObject (readonly)

Returns the value of attribute input_variables.



50
51
52
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 50

def input_variables
  @input_variables
end

#prefixObject (readonly)

Returns the value of attribute prefix.



50
51
52
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 50

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



50
51
52
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 50

def suffix
  @suffix
end

Instance Method Details

#format(**kwargs) ⇒ String

Format the prompt with the inputs.

Parameters:

  • kwargs (Hash)

    Any arguments to be passed to the prompt template.

Returns:

  • (String)

    A formatted string.



90
91
92
93
94
95
96
97
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 90

def format(**kwargs)
  example_string = @examples.map { |example| @example_prompt.format(**example) }

  suffix_string = @suffix
  kwargs.each { |key, value| suffix_string = suffix_string.gsub(/\{#{key}\}/, value.to_s) }

  [@prefix, *example_string, suffix_string].join(@example_separator)
end

#prompt_typeString

Returns the key type of prompt as a string.

Returns:

  • (String)

    the prompt type key



104
105
106
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 104

def prompt_type
  "few_shot"
end

#to_hObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 108

def to_h
  {
    _type: prompt_type,
    input_variables: @input_variables,
    prefix: @prefix,
    example_prompt: @example_prompt.to_h,
    examples: @examples,
    suffix: @suffix
  }
end