Class: Langchain::Prompt::FewShotPromptTemplate
- 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
-
#example_prompt ⇒ Object
readonly
Returns the value of attribute example_prompt.
-
#example_separator ⇒ Object
readonly
Returns the value of attribute example_separator.
-
#examples ⇒ Object
readonly
Returns the value of attribute examples.
-
#input_variables ⇒ Object
readonly
Returns the value of attribute input_variables.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
Instance Method Summary collapse
-
#format(**kwargs) ⇒ String
Format the prompt with the inputs.
-
#initialize(examples:, example_prompt:, input_variables:, suffix:, prefix: "", example_separator: "\n\n", validate_template: true) ⇒ FewShotPromptTemplate
constructor
Initializes a new instance of the class.
-
#prompt_type ⇒ String
Returns the key type of prompt as a string.
- #to_h ⇒ Object
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.
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_prompt ⇒ Object (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_separator ⇒ Object (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 |
#examples ⇒ Object (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_variables ⇒ Object (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 |
#prefix ⇒ Object (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 |
#suffix ⇒ Object (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.
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_type ⇒ String
Returns the key type of prompt as a string.
104 105 106 |
# File 'lib/langchain/prompt/few_shot_prompt_template.rb', line 104 def prompt_type "few_shot" end |
#to_h ⇒ Object
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 |