patterns-ai-core / langchainrb

Build LLM-powered applications in Ruby
https://rubydoc.info/gems/langchainrb
MIT License
1.42k stars 195 forks source link

Insertion of JSON when formatting a template #745

Closed rvmtz closed 2 months ago

rvmtz commented 2 months ago

Describe the bug When using Langchain::Prompt::PromptTemplate to inject a JSON document where the sequence of characters {{ or }} happens, it turns them into a single bracket { and }.

To Reproduce Steps to reproduce the behavior.

data = '{"nested":{"foo":"bar"}}'
prompt = Langchain::Prompt::PromptTemplate.new(template: '{data}', input_variables: ['data'])
prompt.format(data: data)

=> "{\"nested\":{\"foo\":\"bar\"}"

Expected behavior To inject the JSON accordingly.

=> "{\"nested\":{\"foo\":\"bar\"}}"

Additional context

This behavior was introduced in commit https://github.com/patterns-ai-core/langchainrb/commit/63ed16509c866e04792930c8b677f2ed2a256379.

Should we enhance PromptTemplate to process JSON as-is, or add an extra option (besides double braces{{ }}) to escape these sequences? I’m available to discuss and submit a PR if needed. Looking forward to your guidance!

andreibondarev commented 2 months ago

@rvmtz Thank you for identifying the bug. Sounds like there's a bug. This code:

data = '{"nested":{"foo":"bar"}}'
prompt = Langchain::Prompt::PromptTemplate.new(template: '{data}', input_variables: ['data'])
prompt.format(data: data)

should produce:

=> "{\"nested\":{\"foo\":\"bar\"}}"

Feel free to open up a fix if you have any ideas on solving it.

rvmtz commented 2 months ago

@andreibondarev , sorry for the late reply. I just open a PR trying to address the issue. Let me know your thoughts: https://github.com/patterns-ai-core/langchainrb/pull/751. Have a great weekend!