-
Notifications
You must be signed in to change notification settings - Fork 19
feat(templating): Add templating support for InstructionProvider #30
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
base: main
Are you sure you want to change the base?
Conversation
Add comprehensive support for popular Python templating engines (Jinja2, Mako, Mustache, Django) to ADK Community, enabling developers to use familiar templating syntax for dynamic agent instructions. ## What Changed Core Implementation: - Created BaseTemplateProvider abstract class with common functionality - Implemented 4 templating providers: * Jinja2InstructionProvider - Full-featured with custom filters/tests/globals * MakoInstructionProvider - Python-centric with inline expressions * MustacheInstructionProvider - Logic-less templating via chevron * DjangoInstructionProvider - Django template syntax with auto-config - All providers expose ADK session metadata (adk_session_id, adk_user_id, adk_app_name) - Added templating optional dependency group to pyproject.toml Testing: - Created 51 comprehensive unit tests across 4 test suites - Tests cover: basic rendering, nested access, control structures, filters, custom extensions, error handling, and import errors - All tests passing (79/79 total including existing tests) Examples: - Created 4 working sample agents demonstrating real-world use cases: * jinja2_devops_bot - Server monitoring with system metrics * mako_data_analyst - Sales analysis with Python expressions * mustache_customer_support - Customer support with logic-less templates * django_content_moderator - Content moderation with Django filters - Updated README.md with feature highlight, installation guide, and comparison table - Added comprehensive docstrings to all classes and methods Resolves: google#6
Summary of ChangesHello @Jainish-S, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the ADK Community framework by integrating robust templating capabilities for agent instructions. This allows developers to create highly dynamic and context-aware agent behaviors using popular Python templating engines, making agent instruction generation more flexible and powerful. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This is a fantastic contribution that adds comprehensive templating support to ADK Community. The implementation across four different templating engines (Jinja2, Mako, Mustache, Django) is well-structured, with a clean base class and individual provider implementations. The inclusion of extensive unit tests and sample agents for each provider is excellent and greatly improves the usability and reliability of this new feature.
I've found a few minor areas for improvement, mostly related to a redundant filter in a sample template, a suggestion to improve documentation for the Django provider's side-effects, and a correction to a test case for the Mustache provider. Overall, this is a high-quality pull request.
| TODAY'S MODERATION STATS | ||
| ======================== | ||
| Total Reviewed: {{ moderation_stats.total_reviewed_today }} | ||
| Approved: {{ moderation_stats.approved }} ({{ moderation_stats.approved|add:0|floatformat:0 }}) |
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.
The Django template filter add:0|floatformat:0 applied to moderation_stats.approved is redundant. Since moderation_stats.approved is already an integer, add:0 has no effect, and floatformat:0 will simply convert it to a string, which would happen anyway when rendering {{ moderation_stats.approved }}. This makes the template slightly more complex than necessary and can be simplified.
| Approved: {{ moderation_stats.approved }} ({{ moderation_stats.approved|add:0|floatformat:0 }}) | |
| Approved: {{ moderation_stats.approved }} ({{ moderation_stats.approved }}) |
| """Initialize the Django instruction provider. | ||
|
|
||
| Args: | ||
| template: The Django template string. | ||
| autoescape: If True, enables HTML autoescaping. Default is False | ||
| since instructions are typically plain text. | ||
| custom_filters: Optional dictionary of custom template filters. | ||
| custom_tags: Optional dictionary of custom template tags. | ||
|
|
||
| Raises: | ||
| ImportError: If Django is not installed. | ||
| """ |
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.
The provider configures global Django settings via conf.settings.configure() if they haven't been configured already. This is a side-effect that could potentially affect other parts of an application if it also uses Django. It would be beneficial to document this behavior in the __init__ docstring to make users aware of it.
"""Initialize the Django instruction provider.
Args:
template: The Django template string.
autoescape: If True, enables HTML autoescaping. Default is False
since instructions are typically plain text.
custom_filters: Optional dictionary of custom template filters.
custom_tags: Optional dictionary of custom template tags.
Raises:
ImportError: If Django is not installed.
Note:
If Django settings are not already configured, this provider will
configure a minimal set of settings for standalone use.
"""| template = """Environment: {{environment}} | ||
| {{#servers}} | ||
| Active Servers: | ||
| {{#servers}} | ||
| - [{{id}}] {{role}}: CPU {{cpu}}%{{#critical}} (CRITICAL!){{/critical}} | ||
| {{/servers}} | ||
| {{/servers}} | ||
| {{^servers}} | ||
| No servers active. | ||
| {{/servers}} | ||
| """ |
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.
The Mustache template in this test has flawed logic. The outer {{#servers}} section iterates over the servers list, setting the context to each server item for each iteration. The inner {{#servers}} section then incorrectly tries to find a servers key within the individual server item, which doesn't exist. Consequently, the inner block is never rendered, and the test assertions for srv-01, srv-02, and CRITICAL! should fail. The test is likely passing by mistake or due to an issue with the test runner setup.
The template should be corrected to first display a header if the list is not empty, and then iterate over the list to render the items.
| template = """Environment: {{environment}} | |
| {{#servers}} | |
| Active Servers: | |
| {{#servers}} | |
| - [{{id}}] {{role}}: CPU {{cpu}}%{{#critical}} (CRITICAL!){{/critical}} | |
| {{/servers}} | |
| {{/servers}} | |
| {{^servers}} | |
| No servers active. | |
| {{/servers}} | |
| """ | |
| template = """Environment: {{environment}} | |
| {{#servers}} | |
| Active Servers: | |
| {{/servers}} | |
| {{#servers}} | |
| - [{{id}}] {{role}}: CPU {{cpu}}%{{#critical}} (CRITICAL!){{/critical}} | |
| {{/servers}} | |
| {{^servers}} | |
| No servers active. | |
| {{/servers}} | |
| """ |
Add comprehensive support for popular Python templating engines (Jinja2, Mako, Mustache, Django) to ADK Community, enabling developers to use familiar templating syntax for dynamic agent instructions.
What Changed
Core Implementation:
Testing:
Examples:
Resolves: #6