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

core: Allow nested prompt templates #28024

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dmenini
Copy link
Contributor

@dmenini dmenini commented Nov 11, 2024

This PR allows to use nested prompt templates.

  • partial now checks if the partial variable is a PromptTemplate and it will propagate partial variables to it
  • format will first format the nested prompts and the root prompt

In my application, we allow users to write part of prompts (i.e. specific instructions), which may contain input variables. After this change, it will be possible for us to instantiate those instructions as PromptTemplate, and pass them to the main prompts as partial variables.

N.B. I know that there is PipelinePromptTemplate that does something similar, but it doesn't support partial prompts as of now, and imo it should not be needed if the PromptTemplate itself can handle nested prompts.

Thanks for your feedback!

@hwchase17 @baskaryan

Copy link

vercel bot commented Nov 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Nov 11, 2024 3:07pm

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 11, 2024
}

# Filter template variables based on
# partial_variables and nested_partial_vars
values["input_variables"] = [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only point I found a bit counterintuitive. Imo if the user passes input_variables to the PromptTemplate we should not overwrite them with values from the template. This complicates the logic as I had to make sure that the nested partial variables are correctly excluded, to not break backward compatibility.

@dmenini dmenini force-pushed the core/inject-partial-to-partial-variables branch from ee87817 to 6905b5d Compare November 11, 2024 15:06
@efriis efriis requested a review from baskaryan December 4, 2024 02:59
Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

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

Thanks for this. The changes look largely OK (e.g., non-breaking) but IMO it's unclear that the added complexity here is worth it. This is ultimately somewhat niche functionality and we don't have any documentation of what this buys over doing the formatting yourself, e.g.,

template = "This is a {foo} test."
template_nested = "{bar}"

new_prompt = PromptTemplate(
    template=template.format(foo=template_nested)
).partial(bar="bar")

)
prompt_dict["partial_variables"] = {**self.partial_variables, **kwargs}
Copy link
Collaborator

Choose a reason for hiding this comment

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

are we losing self.partial_variables, here?

@ccurme ccurme marked this pull request as draft December 20, 2024 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Status: In review
Development

Successfully merging this pull request may close these issues.

3 participants