langchain
langchain copied to clipboard
escape curly brackets before f-string formatting in FewShotPromptTemplate
In the current version(0.0.147), we should escape curly brackets before f-string formatting (FewShotPromptTemplate) by ourselves.
please make it a default behavior!
https://colab.research.google.com/drive/16_pCJIWK88AXpCh6xsSriJmLJKrNE8Fv?usp=share_link
Test Case
from langchain import FewShotPromptTemplate, PromptTemplate
example={'instruction':'do something', 'input': 'question',}
examples=[
{'input': 'question a', 'output':'answer a'},
{'input': 'question b', 'output':'answer b'},
]
example_prompt = PromptTemplate(
input_variables=['input', 'output'],
template='input: {input}\noutput:{output}',
)
fewshot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
input_variables=['instruction', 'input'],
prefix='{instruction}\n',
suffix='\ninput: {input}\noutput:',
example_separator='\n\n',
)
fewshot_prompt.format(**example)
that's ok !
example={'instruction':'do something', 'input': 'question',}
examples_with_curly_brackets=[
{'input': 'question a{}', 'output':'answer a'},
{'input': 'question b', 'output':'answer b'},
]
fewshot_prompt = FewShotPromptTemplate(
examples=examples_with_curly_brackets,
example_prompt=example_prompt,
input_variables=['instruction', 'input'],
prefix='{instruction}\n',
suffix='\ninput: {input}\noutput:',
example_separator='\n\n',
)
fewshot_prompt.format(**example)
some errors like
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
[<ipython-input-9-95e0dc90fc4d>](https://localhost:8080/#) in <cell line: 16>()
14 )
15
---> 16 fewshot_prompt.format(**example)
6 frames
[/usr/lib/python3.9/string.py](https://localhost:8080/#) in get_value(self, key, args, kwargs)
223 def get_value(self, key, args, kwargs):
224 if isinstance(key, int):
--> 225 return args[key]
226 else:
227 return kwargs[key]
IndexError: tuple index out of range
if we do escape firstly!
# What should we do: escape brackets in examples
def escape_examples(examples):
return [{k: escape_f_string(v) for k, v in example.items()} for example in examples]
def escape_f_string(text):
return text.replace('{', '{{').replace('}', '}}')
fewshot_prompt = FewShotPromptTemplate(
examples=escape_examples(examples_with_curly_brackets),
example_prompt=example_prompt,
input_variables=['instruction', 'input'],
prefix='{instruction}\n',
suffix='\ninput: {input}\noutput:',
example_separator='\n\n',
)
fewshot_prompt.format(**example)
everything is ok now!
i was struggling with this issue and your work around helped a lot! Thanks for putting it!
I am struggling with having curly braces on the template. Langchain is treating it as a variable even after I double them.
template = """ input: {input} context:{{foo}} """
Thoughts?
fix my problem , thank u
Hi, @yuto3o,
I'm helping the LangChain team manage their backlog and am marking this issue as stale. From what I understand, you requested to make the behavior of manually escaping curly brackets before f-string formatting in FewShotPromptTemplate the default. The issue includes test cases and a proposed solution for escaping brackets in examples. Comments from users bhanu-Bigdata, rbarcelos, and peter064226 express gratitude for the workaround provided and discuss struggles with the current behavior.
Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days. Thank you!