Skip to content
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

Allow sentence metadata in responses #2108

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

Conversation

synesthesiam
Copy link
Contributor

@synesthesiam synesthesiam commented Mar 27, 2024

Sentence metadata was added to hassil in version 1.6.0:

language: en
intents:
  TestIntent:
    data:
      - sentences:
        - "..."
        metadata:
          key: value
        response: test_response

This PR exposes that metadata to response rendering as a metadata object:

language: en
responses:
  intents:
    TestIntent:
      test_response: "{{ metadata.key }}" 

This allows sentence authors to pass language-specific data to response templates, such as the non-inflected/base forms of nouns that are part of the matched sentence (device class, etc.).

Summary by CodeRabbit

  • New Features

    • Introduced an optional metadata field in the validation process for enhanced contextual information.
    • Updated response rendering to utilize intent metadata, enabling more dynamic responses.
  • Bug Fixes

    • Improved unit testing capabilities by integrating a mock object for the metadata key.

@synesthesiam
Copy link
Contributor Author

synesthesiam commented Mar 27, 2024

@witold-gren Can you try this out and let me know if it works for you? I still need to modify Home Assistant core for this to work in Assist, but I want to make sure it's correct here first.

@witold-gren
Copy link
Contributor

I can confirm that now it works properly on intents scripts (this repository). Thanks for this improvement @synesthesiam

Copy link
Contributor

@tetele tetele left a comment

Choose a reason for hiding this comment

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

Appologies for the delay. Simple, but very useful contribution, this <3

@witold-gren
Copy link
Contributor

@synesthesiam any update? 😀

Copy link

coderabbitai bot commented Oct 16, 2024

📝 Walkthrough

Walkthrough

The changes introduce a new optional field, metadata, to the SENTENCE_SCHEMA in script/intentfest/validate.py, allowing for additional contextual information during validation. The run function is updated to include a MagicMock object for metadata to facilitate unit testing. Additionally, the render_response function in shared/__init__.py is modified to accept the metadata parameter, enhancing the rendering process by incorporating intent metadata into the context for Jinja2 templates.

Changes

File Path Change Summary
script/intentfest/validate.py Added optional metadata field to SENTENCE_SCHEMA and updated run function to include MagicMock for metadata.
shared/init.py Modified render_response function to accept a new metadata parameter for enhanced template rendering.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Validator
    participant Renderer
    participant Template

    User->>Validator: Submit sentence for validation
    Validator->>Validator: Validate sentence with metadata
    Validator->>Renderer: Call render_response with metadata
    Renderer->>Template: Render template with context including metadata
    Template-->>Renderer: Return rendered response
    Renderer-->>User: Send rendered response
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
shared/__init__.py (1)

294-294: LGTM! Consider adding type hints for result.intent_metadata.

The addition of metadata to the context dictionary enhances the flexibility of response rendering by allowing intent metadata to be used in templates. This change aligns well with the PR objectives.

To improve type safety, consider adding a type hint for result.intent_metadata in the RecognizeResult class. This would provide better clarity on the expected structure of the metadata.

script/intentfest/validate.py (2)

170-170: LGTM! Consider adding a comment for clarity.

The addition of the metadata field to SENTENCE_SCHEMA is well-implemented and aligns with the PR objectives. It's correctly defined as optional and uses a flexible validation approach.

Consider adding a brief comment explaining the purpose and expected structure of the metadata field for future maintainers. For example:

# metadata: Optional dictionary for language-specific data (e.g., non-inflected forms of nouns)
vol.Optional("metadata"): {str: match_anything},

601-601: LGTM! Consider enhancing mock metadata for robust testing.

The addition of metadata as a MagicMock() object in the template rendering context is a good approach for flexible testing. It allows templates to use metadata without requiring specific metadata to be defined, maintaining backward compatibility.

For more robust testing, consider creating a mock metadata object with a predefined structure that represents expected metadata fields. This could help catch issues where templates rely on specific metadata structure. For example:

"metadata": MagicMock(spec_set={"base_form": str, "gender": str}),

This approach would still allow for flexible testing while providing some structure to catch potential issues in templates that rely on specific metadata fields.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8a33a02 and 61c19cf.

📒 Files selected for processing (2)
  • script/intentfest/validate.py (3 hunks)
  • shared/init.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
shared/__init__.py (6)

Pattern */**(html|markdown|md): - For instructional content in documentation, use a direct and authoritative tone. Avoid expressions of politeness such as 'may' or 'please', and ensure the goal of the instruction is fronted.

  • Apply the Microsoft Style Guide to ensure documentation maintains clarity and conciseness.
  • In step-by-step instructions, front the location phrase in the instructional sentence.
  • In step-by-step instructions, front the 'goal' in the instructional sentence.
  • In step-by-step instructions, if in doubt what to front, front the 'goal' before the location phrase in the instructional sentence.
  • do not hyphenate terms like 'top-right' or 'bottom-left' with 'corner'

Pattern */**(html|markdown|md): - Use bold to mark UI strings.

  • If "" are used to mark UI strings, replace them by bold.

Pattern */**(html|markdown|md): - Be brief in your replies and don't add fluff like "thank you for..." and "Please let me know if"


Pattern */**(html|markdown|md): - Use sentence-style capitalization also in headings.


Pattern */**(html|markdown|md): do not comment on HTML used for icons


Pattern */**(html|markdown|md): Avoid flagging inline HTML for embedding videos in future reviews for this repository.

🔇 Additional comments (2)
script/intentfest/validate.py (2)

11-11: LGTM! Appropriate import for mock object.

The import of MagicMock from unittest.mock is correctly placed and necessary for the changes in the template rendering context. This is a standard approach for creating mock objects in Python testing scenarios.


Line range hint 1-604: Overall assessment: Well-implemented changes with minor suggestions for improvement.

The changes in this file successfully introduce support for sentence metadata in the validation and testing process, aligning well with the PR objectives. The implementation maintains backward compatibility and allows for flexible testing. The suggestions provided aim to improve code clarity and the robustness of testing.

Key points:

  1. The metadata field is correctly added to the SENTENCE_SCHEMA.
  2. The MagicMock import and usage for metadata in the template rendering context is appropriate.
  3. Consider adding comments for clarity and enhancing the mock metadata structure for more robust testing.

These changes provide a solid foundation for incorporating sentence metadata in the Home Assistant intents framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants