langchain icon indicating copy to clipboard operation
langchain copied to clipboard

escape curly brackets before f-string formatting in FewShotPromptTemplate

Open yuto3o opened this issue 1 year ago • 1 comments

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!

yuto3o avatar Apr 23 '23 06:04 yuto3o

i was struggling with this issue and your work around helped a lot! Thanks for putting it!

bhanu-Bigdata avatar May 06 '23 09:05 bhanu-Bigdata

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?

rbarcelos avatar Aug 15 '23 16:08 rbarcelos

fix my problem , thank u

peter064226 avatar Oct 23 '23 07:10 peter064226

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!

dosubot[bot] avatar Feb 05 '24 16:02 dosubot[bot]