Skip to content

feat: add Jinja2 ChatMessage extension #297

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

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

Conversation

anakin87
Copy link
Member

@anakin87 anakin87 commented May 8, 2025

Related Issues

Proposed Changes:

  • introduce a Jinja2 ChatMessage extension
    1. The {% message %} tag is used to define a chat message.
    2. The message can contain text and other structured content parts.
    3. To include a structured content part in the message, the | for_template filter is used.
      The filter serializes the content part into a JSON string and wraps it in a <haystack_content_part> tag.
    4. The _build_chat_message_json method of the extension parses the message content parts,
      converts them into a ChatMessage object and serializes it to a JSON string.
    5. The obtained JSON string will be usable in the ChatPromptBuilder component, where templates are rendered to actual ChatMessage objects.

How did you test it?

CI; several tests for the extension

Notes for the reviewer

  • Don't get scared by the size of this PR (there are many tests)
  • (I'll keep working on the ChatPromptBuilder using this basis)

Checklist

@anakin87 anakin87 force-pushed the jinja-chatmessage-extension branch from 709e608 to 697e3e1 Compare May 8, 2025 12:49
@coveralls
Copy link

coveralls commented May 8, 2025

Pull Request Test Coverage Report for Build 14929353715

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+2.1%) to 70.654%

Files with Coverage Reduction New Missed Lines %
dataclasses/chat_message.py 2 98.31%
Totals Coverage Status
Change from base Build 14918586053: 2.1%
Covered Lines: 1134
Relevant Lines: 1605

💛 - Coveralls

@anakin87 anakin87 marked this pull request as ready for review May 8, 2025 13:10
@anakin87 anakin87 requested a review from a team as a code owner May 8, 2025 13:10
@anakin87 anakin87 requested review from julian-risch and sjrl and removed request for a team and julian-risch May 8, 2025 13:10
### How it works
1. The {% message %} tag is used to define a chat message.
2. The message can contain text and other structured content parts.
3. To include a structured content part in the message, the `| for_template` filter is used.
Copy link
Contributor

Choose a reason for hiding this comment

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

Also more of just a question. Why the name for_template? I'm not yet quite understanding what it does based on just the name.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I would like a short but descriptive name.
Other ideas: content_part/prepare_content_part/mark_content_part/format_content_part; something related to "including".

Do you have any other ideas?
(We can also involve some other team members if necessary)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I'm not entirely sure either. I'd be happy to have other team members give their input

@sjrl
Copy link
Contributor

sjrl commented May 9, 2025

@anakin87 Overall looks really good! I have some some comments mostly surrounding tests and error handling but the templating language itself looks very solid!

@anakin87 anakin87 requested a review from sjrl May 9, 2025 10:22
Copy link
Contributor

@sjrl sjrl left a comment

Choose a reason for hiding this comment

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

Everything looks good! I don't think the for_template naming needs to block this. Will leave it up to you

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.

3 participants