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

add filters to prompt function #1371

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

derfred
Copy link

@derfred derfred commented Jan 10, 2025

Allow giving custom filters to the prompt decorator

def reverses: str) -> str:
    return s[::-1]

@prompt(filters=[reverse])
def reverse_prompt(text):
    '''{{ text | reverse }}'''

prompt = reverse_prompt("Hello")

print(prompt)
>>> "olleH"

return env.get_template(os.path.basename(path))


def prompt(fn: Callable) -> Prompt:
def prompt(fn: Optional[Callable] = None, **filters: Dict[str, Callable]) -> Callable:
Copy link
Member

Choose a reason for hiding this comment

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

filters should be a simple argument that has type Dict[str, Callable] and maps the filter names to the functions.

@@ -118,10 +125,14 @@ def _template_from_file(_, path: Path) -> jinja2.Template:
keep_trailing_newline=True,
undefined=jinja2.StrictUndefined,
)

for name, filter_fn in filters.items():
Copy link
Member

Choose a reason for hiding this comment

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

I generally don't mind a little repetition but I think we should refactor the code and add a create_environment class that can be used in both class methods. @yvan-sraka are there any reasons we shouldn't do that?

@derfred
Copy link
Author

derfred commented Jan 11, 2025

thanks for your review @rlouf. I've taken your comments into account. While doing so the following alternative approach came to me:

env = outlines.prompts.prompt_environment([reverse])

@env
def my_prompt(text):
  "reversed: {{ text | reverse }}"

what do you think?

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.

2 participants