Repository navigation
feat: Extend ChatPromptBuilder with insert tag
#11486
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c3dfeb2
Extend ChatPromptBuilder
sjrl 7b5b7c4
updates
sjrl a0b2183
add support for subscripting
sjrl e142e6c
Add explicit tests for using messages placeholder multiple times
sjrl 742b76c
mypy fix
sjrl e98c8cf
change to insert tag
sjrl b8e2474
add more tests
sjrl 8abda6a
improve example in reno
sjrl 3b244b3
PR feedback
sjrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
releasenotes/notes/add-messages-jinja-tag-483cca5bda6884f2.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| features: | ||
| - | | ||
| Added a new ``{% insert %}`` tag to the Jinja2 string templates used by ``ChatPromptBuilder``. | ||
| The tag is a placeholder that evaluates an expression to a ``ChatMessage`` or a list of ``ChatMessage`` | ||
| objects and expands it into the prompt, so messages provided at runtime can be interleaved with literal | ||
| ``{% message %}`` blocks. For example, you can wrap the runtime messages with a system message above | ||
| and a templated user message below, then pass the messages (and any template variables) at run time: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from haystack.components.builders import ChatPromptBuilder | ||
| from haystack.dataclasses import ChatMessage | ||
|
|
||
| template = """ | ||
| {% message role="system" %}You are a helpful assistant.{% endmessage %} | ||
| {% insert messages %} | ||
| {% message role="user" %}{{ query }}{% endmessage %} | ||
|
sjrl marked this conversation as resolved.
Resolved
|
||
| """ | ||
|
|
||
| builder = ChatPromptBuilder(template=template) | ||
| result = builder.run( | ||
| messages=[ChatMessage.from_user("Hi"), ChatMessage.from_assistant("Hello!")], | ||
| query="What's the weather?", | ||
| ) | ||
| # result["prompt"] -> [system, user "Hi", assistant "Hello!", user "What's the weather?"] | ||
|
|
||
| All content types (tool calls, tool call results, images, reasoning, ``name`` and ``meta``) round trip | ||
| without loss. A missing or empty value expands to nothing. | ||
|
|
||
| The expression can be a plain variable (``{% insert messages %}``), a slice or index | ||
| (``{% insert messages[-1:] %}``, ``{% insert messages[-1] %}``), or a combination of variables | ||
| (``{% insert previous + current %}``). Multiple ``{% insert %}`` tags can be used in a single template, | ||
| so the runtime messages can be split, reordered, or repeated across different positions. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.