-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: Add DynamicPromptBuilder to Haystack 2.x #6328
Conversation
Hey @vblagoje , I added the component to API reference and updated some docstrings :) |
DynamicPromptBuilder in the pipeline, these variable names can be different. For example, if your component | ||
connected to the DynamicPromptBuilder has an output named `documents`, the `expected_runtime_variables` should | ||
contain `documents` as one of its values. The values associated with variables from the pipeline runtime are |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part sounds really strange to me. It seems to imply that the name of variables in the template must be identical to the output name of the senders components, canals
is not supposed to work like that. 🤔
For example, if your component connected to the DynamicPromptBuilder has an output named
documents
, theexpected_runtime_variables
should containdocuments
as one of its values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, hmm, how would you phrase it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this maybe.
For example, if
expected_runtime_variables
containsdocuments
your instance of DynamicPromptBuilder will expect an input calleddocuments
.
I would probably remove this part too, the name of input really doesn't depend on the connected components.
Depending on the components connected to the DynamicPromptBuilder in the pipeline, these variable names can be different.
test/preview/components/builders/test_dynamic_prompt_builder.py
Outdated
Show resolved
Hide resolved
test/preview/components/builders/test_dynamic_prompt_builder.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's something funky going on I think.
I tested the component with this snippet but it raises a ValueError
at line 245.
I would have expected this to work though. 🤔
from haystack.preview.components.builders.dynamic_prompt_builder import DynamicPromptBuilder
from haystack.preview import component, Pipeline
from typing import List
from haystack.preview.dataclasses import Document
@component
class DocumentProducer:
@component.output_types(documents=List[Document])
def run(self, doc_input: str):
return {"documents": [Document(content=doc_input)]}
pipe = Pipeline()
producer = DocumentProducer()
builder = DynamicPromptBuilder(expected_runtime_variables=["d1"], chat_mode=False)
pipe.add_component("p1", producer)
pipe.add_component("builder", builder)
pipe.connect("p1.documents", "builder.d1")
pipe.run(data={"p1": {"doc_input": "hey hey"}, "builder": {"prompt_source": {"This is what I received: {{d1}}"}}})
@silvanocerza You have one extra dictionary for prompt_source that is unnecessary and got the ValueError complaining about the type received. Just remove that one level of the dictionary, and it works. |
Right, it works now. 🤦♀️ |
Co-authored-by: Silvano Cerza <[email protected]>
|
||
""" | ||
|
||
def __init__(self, expected_runtime_variables: Optional[List[str]] = None, chat_mode: Optional[bool] = True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would change expected_runtime_variables
to just runtime_variables
. It seems a bit redundant to prefix it with expected_
, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with both, IIRC correctly @masci and I talked about expected_variables
or expected_runtime_variables
I didn't write it down tbh.
Co-authored-by: Silvano Cerza <[email protected]>
@silvanocerza a small favor 🙏 Have another look now and coordinate with Massi if needed regarding that variable name. Also please adjust the wording in the pydoc regarding runtime variables. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made the changes we talked about, we can merge when CI is green. 👍
Pull Request Test Coverage Report for Build 6968334557Warning: This coverage report may be inaccurate.We've detected an issue with your CI configuration that might affect the accuracy of this pull request's coverage report.
💛 - Coveralls |
Why:
This pull request introduces the
DynamicPromptBuilder
component. This new prompt builder enables dynamic and parametrized prompting, significantly enhancing the flexibility and functionality of prompt generation within the haystack pipeline.What:
The following key changes have been made in this PR:
DynamicPromptBuilder
class indynamic_prompt_builder.py
. This class leverages Jinja2 templating to dynamically generate prompts based on either a list ofChatMessage
instances or a string template. It can operate in chat or non-chat mode and supports injecting variables resolved at runtime.DynamicPromptBuilder
in the module's__all__
list, making it accessible as part of the builders module.test_dynamic_prompt_builder.py
to ensure the correct functioning of this new component under various scenarios.How can it be used:
Developers can utilize
DynamicPromptBuilder
in the haystack pipeline to create rich prompting scenarios where input variables are injected from the pipeline runtime and the user's pipeline run invocation. For some common use case scenarios for both chat and non-chat scenarios, see this colabHow did you test it:
Testing was performed using unit tests located in
test/preview/components/builders/test_dynamic_prompt_builder.py
. These tests cover various scenarios, including initialization of the builder, processing simple templates, handling chat messages, and validation of template variables. Manual tests were completed with the colab above.Notes for the reviewer: