Skip to content

feat(wren-ai-service): add custom instructions #1756

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 1 commit into from
Jun 19, 2025

Conversation

cyyeh
Copy link
Member

@cyyeh cyyeh commented Jun 19, 2025

Summary by CodeRabbit

  • New Features
    • Added support for an optional custom instruction field when generating charts and SQL answers. Users can now provide specific instructions to customize the style of AI-generated responses.
  • API Changes
    • Chart and SQL answer request formats now accept an optional custom instruction parameter.

@cyyeh cyyeh added module/ai-service ai-service related ci/ai-service ai-service related labels Jun 19, 2025
Copy link
Contributor

coderabbitai bot commented Jun 19, 2025

Walkthrough

Support for an optional custom_instruction parameter has been introduced throughout both the chart and SQL answer generation pipelines. This parameter is now accepted in API request models, propagated through service and pipeline layers, and integrated into prompt construction, allowing user-specified instructions to modify the style of generated responses.

Changes

File(s) Change Summary
.../chart_generation.py, .../sql_answer.py Updated pipeline and prompt function/method signatures to accept and propagate the custom_instruction parameter. System and user prompts now include this field.
.../services/chart.py, .../services/sql_answer.py Added optional custom_instruction field to request models; passed this parameter to pipeline run methods.

Suggested labels

wren-ai-service

Suggested reviewers

  • imAsterSun

Poem

A custom instruction hops along the way,
Through charts and answers, it now holds sway.
Pipelines listen, prompts obey,
Giving users a brand new say.
With every hop, the service grows—
A clever touch, as every bunny knows! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch chore/ai-service/custom-instructions

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this 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 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
Contributor

@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: 1

♻️ Duplicate comments (1)
wren-ai-service/src/web/v1/services/sql_answer.py (1)

21-21: Consider adding validation for custom instructions.

Similar to the chart service, the custom_instruction field accepts arbitrary user input without validation. This could potentially be exploited for prompt injection. Consider implementing the same validation approach across both services.

Apply similar validation as suggested for the chart service:

+ from pydantic import Field, validator
+
class SqlAnswerRequest(BaseRequest):
    query: str
    sql: str
    sql_data: Dict
-   custom_instruction: Optional[str] = None
+   custom_instruction: Optional[str] = Field(None, max_length=1000)
+
+   @validator('custom_instruction')
+   def validate_custom_instruction(cls, v):
+       if v is not None and len(v.strip()) == 0:
+           return None
+       return v
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6e84b6a and 9943c62.

📒 Files selected for processing (4)
  • wren-ai-service/src/pipelines/generation/chart_generation.py (6 hunks)
  • wren-ai-service/src/pipelines/generation/sql_answer.py (5 hunks)
  • wren-ai-service/src/web/v1/services/chart.py (2 hunks)
  • wren-ai-service/src/web/v1/services/sql_answer.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: pytest
  • GitHub Check: pytest
  • GitHub Check: Analyze (go)
🔇 Additional comments (10)
wren-ai-service/src/web/v1/services/chart.py (1)

128-128: LGTM: Custom instruction properly passed to pipeline.

The parameter is correctly propagated to the chart generation pipeline.

wren-ai-service/src/web/v1/services/sql_answer.py (1)

103-103: LGTM: Custom instruction properly passed to pipeline.

The parameter is correctly propagated to the SQL answer pipeline.

wren-ai-service/src/pipelines/generation/chart_generation.py (4)

30-30: LGTM: Appropriate system prompt instruction for custom instructions.

The system prompt correctly instructs the model to strictly follow custom instructions to modify response style.


50-50: LGTM: User prompt template properly includes custom instruction field.

The template correctly includes the custom_instruction field for prompt generation.


70-70: LGTM: Function signature and parameter passing implemented correctly.

The prompt function signature is properly updated to accept custom_instruction and passes it to the prompt builder.

Also applies to: 82-82


158-158: LGTM: Pipeline run method properly handles custom instruction parameter.

The run method correctly accepts the optional custom_instruction parameter and defaults it to an empty string, preventing None values in the prompt context.

Also applies to: 169-169

wren-ai-service/src/pipelines/generation/sql_answer.py (4)

32-32: LGTM: Consistent system prompt instruction for custom instructions.

The system prompt correctly mirrors the chart generation approach, instructing the model to strictly follow custom instructions.


45-45: LGTM: User prompt template consistent with chart generation.

The template correctly includes the custom_instruction field, maintaining consistency across pipelines.


58-58: LGTM: Function signature and parameter passing implemented correctly.

The prompt function signature is properly updated and parameter passing is consistent with the chart generation pipeline.

Also applies to: 66-66


148-148: LGTM: Pipeline run method properly handles custom instruction parameter.

The run method implementation is consistent with the chart generation pipeline, correctly handling the optional parameter with appropriate defaulting.

Also applies to: 159-159

@@ -18,6 +18,7 @@ class ChartRequest(BaseRequest):
sql: str
data: Optional[Dict[str, Any]] = None
remove_data_from_chart_schema: Optional[bool] = True
custom_instruction: Optional[str] = None
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding validation for custom instructions.

The custom_instruction field accepts arbitrary user input without validation, which could potentially be used for prompt injection attacks. Consider adding length limits, content validation, or sanitization to prevent misuse.

+ from pydantic import Field, validator
+
class ChartRequest(BaseRequest):
    query: str
    sql: str
    data: Optional[Dict[str, Any]] = None
    remove_data_from_chart_schema: Optional[bool] = True
-   custom_instruction: Optional[str] = None
+   custom_instruction: Optional[str] = Field(None, max_length=1000)
+
+   @validator('custom_instruction')
+   def validate_custom_instruction(cls, v):
+       if v is not None and len(v.strip()) == 0:
+           return None
+       return v
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
custom_instruction: Optional[str] = None
from pydantic import Field, validator
class ChartRequest(BaseRequest):
query: str
sql: str
data: Optional[Dict[str, Any]] = None
remove_data_from_chart_schema: Optional[bool] = True
custom_instruction: Optional[str] = Field(None, max_length=1000)
@validator('custom_instruction')
def validate_custom_instruction(cls, v):
if v is not None and len(v.strip()) == 0:
return None
return v
🤖 Prompt for AI Agents
In wren-ai-service/src/web/v1/services/chart.py at line 21, the
custom_instruction field currently accepts any user input without validation,
posing a security risk. Add validation logic to enforce length limits and
sanitize or filter the content to prevent prompt injection attacks. Implement
checks that reject or clean inputs containing suspicious or disallowed patterns
before processing or storing the custom_instruction value.

@cyyeh cyyeh requested a review from yichieh-lu June 19, 2025 01:34
@cyyeh cyyeh merged commit dcdc555 into main Jun 19, 2025
15 checks passed
@cyyeh cyyeh deleted the chore/ai-service/custom-instructions branch June 19, 2025 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci/ai-service ai-service related module/ai-service ai-service related wren-ai-service
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants