Skip to content

Commit

Permalink
prompt: support nested json data in #format
Browse files Browse the repository at this point in the history
Close: #745
  • Loading branch information
rvmtz committed Sep 1, 2024
1 parent cb610e5 commit 5ec3954
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/langchain/prompt/prompt_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def initialize(template:, input_variables:, validate_template: true)
#
def format(**kwargs)
result = @template
kwargs.each { |key, value| result = result.gsub(/\{#{key}\}/, value.to_s) }
result.gsub(/{{/, "{").gsub(/}}/, "}")
kwargs.each { |key, value| result = result.gsub(/\{#{key}\}/, value.to_s) }
result
end

#
Expand Down
11 changes: 11 additions & 0 deletions spec/langchain/prompts/prompt_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@

expect(prompt.format(adjective: "funny", content: "chickens")).to eq("Tell me a funny joke about chickens.")
end

it "handles nested JSON data correctly" do
schema = '{"nested":{"foo":"bar"}}'

prompt = described_class.new(
template: "This is a JSON schema: {schema}",
input_variables: ["schema"]
)

expect(prompt.format(schema: schema)).to eq("This is a JSON schema: #{schema}")
end
end

describe "#from_template" do
Expand Down

0 comments on commit 5ec3954

Please sign in to comment.