google / langfun

OO for LLMs
Apache License 2.0
100 stars 17 forks source link

Support `{{DEFAULT}}` expression in Langfun template. #194

Closed copybara-service[bot] closed 3 months ago

copybara-service[bot] commented 3 months ago

Support {{DEFAULT}} expression in Langfun template.

This allows template override to refer to their default value without copying their content.

Scenario 1: Use instance-level template_str to override the class default.

class Foo(lf.Template):
    '''Foo template.

    This is {{x}}.
    '''

f = Foo(template_str='<h1>{{DEFAULT}}</h1>', x=1)
f.render()

>> <h1>This is 1.</h1>

Scenario 2: Use an ad-hoc template to override a predefined field.

class Bar(lf.Template):
    '''Bar template.

    {{preamble}}
    {{prompt}}
    '''
    preamble: lf.Template = lf.Template('You are a chat bot.')
    prompt: lf.Template = lf.Template('User: hi')

b = Bar(preamble=lf.Template('<h1>{{DEFAULT}}<h1>'),
        prompt=lf.Template('<h2>{{DEFAULT}}</h2>')
b.render()

>> <h1>You are a chat bot.<h1>
>> <h2>User: hi</h2>