Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape double curly braces in prompt template strings #43

Merged
merged 4 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/prompt/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def save(file_path:)
# 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.
#
# @param template [String] The template string to extract variables from.
#
# @return [Array<String>] An array of input variable names.
Expand All @@ -74,9 +76,9 @@ def self.extract_variables_from_template(template)
input_variables = []
scanner = StringScanner.new(template)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking: variable.start_with?("{") to improve the readability perhaps.

end

input_variables
Expand Down
4 changes: 2 additions & 2 deletions lib/prompt/prompt_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def initialize(template:, input_variables:, validate_template: true)
end

#
# Format the prompt with the inputs.
# Format the prompt with the inputs. Double {{}} replaced with single {} to adhere to f-string spec.
#
# @param kwargs [Hash] Any arguments to be passed to the prompt template.
# @return [String] A formatted string.
#
def format(**kwargs)
result = @template
kwargs.each { |key, value| result = result.gsub(/\{#{key}\}/, value.to_s) }
result
result.gsub(/{{/, "{").gsub(/}}/, "}")
end

#
Expand Down
15 changes: 15 additions & 0 deletions spec/prompts/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@
expect(File.directory?(non_existent_dir)).to be_truthy
end
end

describe "#extract_variables_from_template" do
let(:basic_template) { "Tell me a {adjective} joke." }
let(:escaped_template) { "Tell me a {adjective} joke. Return in JSON in the format {{joke: 'The joke'}}" }

it "extracts variables" do
input_variables = Prompt::Base.extract_variables_from_template(basic_template)
expect(input_variables).to eq(%w[adjective])
end

it "excludes double curly brace variables" do
input_variables = Prompt::Base.extract_variables_from_template(escaped_template)
expect(input_variables).to eq(%w[adjective])
end
end
end