haystack icon indicating copy to clipboard operation
haystack copied to clipboard

feat: extend PromptBuilder and deprecate DynamicPromptBuilder

Open tstadel opened this issue 9 months ago • 21 comments

Related Issues

Currently we cannot have both:

  • a default prompt template defined (PromptBuilder)
  • dynamically change prompt templates at runtime (DynamicPromptBuilder)

There are two options:

  • A we extend DynamicPromptBuilder and leave PromptBuilder as is
  • B we extend PromptBuilder and deprecate DynamicPromptBuilder

Edit 07.05.: We decided to go with B

This is Option B See https://github.com/deepset-ai/haystack/pull/7652 for Option A

Proposed Changes:

This extends PromptBuilder to change prompts at query time.

default_template = "This is the default prompt: \\n Query: {{query}}"
prompt_builder = PromptBuilder(template=default_template)

pipe = Pipeline()
pipe.add_component("prompt_builder", prompt_builder)

# using the default prompt
result = pipe.run(
    data={
        "prompt_builder": {
            "query": "Where does the speaker live?",
        },
    }
)
#  "This is the default prompt: \n Query: Where does the speaker live?"

# using the dynamic prompt
result = pipe.run(
    data={
        "prompt_builder": {
            "template": "This is the dynamic prompt:\\n Query: {{query}}",
            "query": "Where does the speaker live?",
        },
    }
)
#  "This is the dynamic prompt: \n Query: Where does the speaker live?"

How did you test it?

  • added tests

Notes for the reviewer

  • ~~There is a breaking change: required_variables param has been changed to optional_variables as most variables of templates are required anyways. We can undo that if necessary.~~
  • DynamicPromptBuilder is being deprecated
  • The Chat counterpart to PromptBuilder is implemented in https://github.com/deepset-ai/haystack/pull/7663

Checklist

tstadel avatar May 06 '24 15:05 tstadel