Skip to content

Composable syntax #2004

Description

@jordane95

Hi, I'm encountering some issues when writing a jinja code which compose custom filters and list index function.

jinja_code="""Question:{{ text | split("[INST]")[1] | split("[/INST]")[0] }}
Answer:{{ text | split("[/INST]")[1] }}"""

After defined split filter

def split_text(value, delimiter):
    return value.split(delimiter)

# Add the custom filter to the environment
env.filters['split'] = split_text

The line template = env.from_string(jinja_code) raises

>
    template = env.from_string(jinja_code)
  File "/usr/local/lib/python3.10/dist-packages/jinja2/environment.py", line 1105, in from_string
    return cls.from_code(self, self.compile(source), gs, None)
  File "/usr/local/lib/python3.10/dist-packages/jinja2/environment.py", line 768, in compile
    self.handle_exception(source=source_hint)
  File "/usr/local/lib/python3.10/dist-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got '['

I'm wondering if we could support composable operations? Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions