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

Conversation

timrwilliams
Copy link
Contributor

Resolves #42


Simple approach to not include input variables denoted by double curly braces. As per the f-string syntax double curly braces are replaces by single curly braces in the output.

prompt = Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke. Return in JSON in the format {{joke: 'The joke'}}", input_variables: ["adjective"])
prompt.format
=> "Tell me a {adjective} joke. Return in JSON in the format {joke: 'The joke'}"
irb(main):006:0>

Tested via new spec and also ran against all current Prompt examples on langchain-hub to ensure compatibility with wider ecosystem.

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.

@andreibondarev
Copy link
Collaborator

@timrwilliams Just 1 comment.

Could you please upmerge your branch and also fix the failing specs? I think it's just the standardrb check failing.

Also -- would you please add some code comments referencing the intention behind this fix? Thank you!

Copy link
Collaborator

@andreibondarev andreibondarev left a comment

Choose a reason for hiding this comment

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

@timrwilliams Looks great, thank you!!

Copy link
Collaborator

@andreibondarev andreibondarev left a comment

Choose a reason for hiding this comment

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

@timrwilliams Looks great, thank you!!

@andreibondarev andreibondarev merged commit c881b65 into patterns-ai-core:main May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for JSON examples in PromptTemplate
2 participants