Skip to content

Conversation

OmarAhmed-A
Copy link
Contributor

@OmarAhmed-A OmarAhmed-A commented Aug 19, 2025

  • Current Time parameter added to all SQL generation templates
  • Consistent temporal context across all SQL pipelines
  • Uses configuration.show_current_time() for proper formatting
  • Improves handling of temporal queries ("today", "this month", etc.)

Extends existing current_time pattern to intent classification,
sql_generation, sql_correction, followup_sql_generation, and sql_regeneration.

Fixes #1898

Summary by CodeRabbit

  • New Features
    • Prompts now include the current time across SQL generation, regeneration, correction, and intent classification to provide time-aware context for queries.
    • Current time is injected at runtime via configuration; no user action required.
    • Prompt templates updated for consistent time awareness in system and user prompts, improving relevance and accuracy for time-sensitive tasks.

Copy link
Contributor

coderabbitai bot commented Aug 19, 2025

Walkthrough

Adds configuration-driven current_time propagation into multiple generation pipeline prompts and prompt_builder.run calls. Several prompt() signatures now accept configuration: Configuration = Configuration(). Templates include "Current Time: {{ current_time }}". One file contains a duplicated Configuration import.

Changes

Cohort / File(s) Summary
Time-aware SQL & follow-up generation
wren-ai-service/src/pipelines/generation/sql_generation.py, wren-ai-service/src/pipelines/generation/followup_sql_generation.py, wren-ai-service/src/pipelines/generation/sql_regeneration.py, wren-ai-service/src/pipelines/generation/sql_correction.py
Add Configuration import; extend prompt(...) signatures to accept configuration: Configuration = Configuration() (where applicable); pass current_time=configuration.show_current_time() into prompt_builder.run; add Current Time: {{ current_time }} to user prompt templates.
Intent classification time exposure
wren-ai-service/src/pipelines/generation/intent_classification.py
Add Current Time: {{ current_time }} to templates and pass current_time=configuration.show_current_time() into prompt_builder.run; no public API signature change.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Caller as Pipeline Caller
  participant PromptFn as prompt()
  participant Config as Configuration
  participant Builder as PromptBuilder

  Caller->>PromptFn: prompt(..., configuration=Configuration())
  activate PromptFn
  PromptFn->>Config: show_current_time()
  Config-->>PromptFn: "YYYY-MM-DD HH:MM:SS"
  PromptFn->>Builder: run(..., current_time=<value>)
  activate Builder
  Note over Builder: Templates include "Current Time: {{ current_time }}"
  Builder-->>PromptFn: Rendered prompts
  deactivate Builder
  PromptFn-->>Caller: Prompt payload
  deactivate PromptFn
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
Add today’s date to prompts/instructions so relative time queries are interpreted correctly (#1898)

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Duplicate Configuration import (wren-ai-service/src/pipelines/generation/sql_regeneration.py) Redundant import added; not required by the objective and should be removed.

Possibly related PRs

Suggested labels

module/ai-service, wren-ai-service

Suggested reviewers

  • cyyeh
  • yichieh-lu
  • imAsterSun

Poem

I checked the clocks with twitchy ears,
Now prompts know dates without the fears.
A hop, a stamp—time in every query,
No more “today?” confused or weary.
This rabbit nudges timestamps bright. ⏰🐇

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 22c903e and 6de5d2d.

📒 Files selected for processing (4)
  • wren-ai-service/src/pipelines/generation/followup_sql_generation.py (4 hunks)
  • wren-ai-service/src/pipelines/generation/sql_correction.py (3 hunks)
  • wren-ai-service/src/pipelines/generation/sql_generation.py (4 hunks)
  • wren-ai-service/src/pipelines/generation/sql_regeneration.py (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • wren-ai-service/src/pipelines/generation/sql_correction.py
  • wren-ai-service/src/pipelines/generation/sql_regeneration.py
  • wren-ai-service/src/pipelines/generation/sql_generation.py
  • wren-ai-service/src/pipelines/generation/followup_sql_generation.py
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

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

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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: 0

🧹 Nitpick comments (3)
wren-ai-service/src/pipelines/generation/utils/sql.py (3)

3-3: Use a timezone-aware datetime import

Relying on naive datetimes can mis-anchor “today” at runtime depending on server locale/timezone. Import timezone to anchor to UTC (or a configured tz).

-from datetime import datetime
+from datetime import datetime, timezone

488-500: Anchor “today” to a specific timezone and add ISO-8601 for locale safety

This achieves the PR goal while avoiding ambiguity:

  • Use UTC to avoid server-local timezone drift.
  • Include ISO-8601 alongside the human-readable form (strftime’s month name is locale-dependent).
  • Filter out None/empty user instructions to avoid polluting the instruction list.
-    
-    # Always add global date instruction
-    today = datetime.now().strftime("%B %d, %Y")  # e.g., "August 18, 2025"
-    global_instruction = f"Today's date is {today}. When users ask about 'today', 'current', 'latest', or use relative time expressions like 'this month', 'this year', use this date as the reference point."
-    _instructions.append(global_instruction)
-    
-    # Add user-provided instructions
-    if instructions:
-        _instructions += [
-            instruction.get("instruction") for instruction in instructions
-        ]
+    # Always add global date instruction (UTC, include ISO for clarity)
+    today_date = datetime.now(timezone.utc).date()
+    human = today_date.strftime("%B %d, %Y")  # e.g., "August 18, 2025"
+    iso = today_date.isoformat()
+    global_instruction = (
+        f"Today's date is {human} (ISO: {iso}, timezone: UTC). "
+        "When users ask about 'today', 'current', 'latest', or use relative time expressions like 'this month' or 'this year', use this date as the reference point."
+    )
+    _instructions.append(global_instruction)
+
+    # Add user-provided instructions (filter out empty/None)
+    if instructions:
+        _instructions += [
+            ins for ins in (i.get("instruction") for i in instructions) if ins
+        ]

If a different timezone (e.g., project/user/data-source) is desired, we can parameterize this or read from config without breaking callers.


488-500: Make date instructions timezone-aware and validate instruction list

  • In wren-ai-service/src/pipelines/generation/utils/sql.py (around lines 488–500), replace the naive datetime.now() with an explicit timezone. For example:
    - today = datetime.now().strftime("%B %d, %Y")
    + from zoneinfo import ZoneInfo  # or use pytz
    + tz = ZoneInfo(configuration.timezone)  # e.g., "UTC" or your project/user locale
    + today = datetime.now(tz).strftime("%B %d, %Y")
  • Audit all callers of construct_instructions (e.g. in followup_sql_generation.py:105, sql_generation_reasoning.py:70, sql_generation.py:101, sql_regeneration.py:114, intent_classification.py:285, followup_sql_generation_reasoning.py:81) to confirm they always pass a list of dicts containing the "instruction" key.
    • If there’s any doubt, add a guard such as:
    cleaned = [
        inst["instruction"]
        for inst in (instructions or [])
        if isinstance(inst, dict) and "instruction" in inst
    ]
    _instructions += cleaned
  • Ensure your deployment config or user settings expose a canonical timezone (UTC or otherwise) so this change remains consistent across environments.
📜 Review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between eabc6e4 and 195e6a9.

📒 Files selected for processing (1)
  • wren-ai-service/src/pipelines/generation/utils/sql.py (2 hunks)
🔇 Additional comments (1)
wren-ai-service/src/pipelines/generation/utils/sql.py (1)

488-494: LGTM: Global date context added first, matching PR objectives

Placing the date instruction at the top ensures consistent temporal grounding across pipelines. This should improve handling of “today/this month/current year”.

- Current Time parameter added to all SQL generation templates
- Consistent temporal context across all SQL pipelines
- Uses configuration.show_current_time() for proper formatting
- Improves handling of temporal queries ("today", "this month", etc.)

Extends existing current_time pattern to intent classification,
sql_generation, sql_correction, followup_sql_generation, and sql_regeneration.
@OmarAhmed-A OmarAhmed-A force-pushed the feat/global-date-instructions branch from 195e6a9 to c1fa959 Compare August 19, 2025 07:00
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

🧹 Nitpick comments (5)
wren-ai-service/src/pipelines/generation/intent_classification.py (2)

290-291: Guard against None configuration before calling show_current_time().

configuration is annotated as optional in this module, but it’s dereferenced unconditionally here. If prompt() is ever invoked outside the pipeline with configuration=None, this will raise.

Minimal, localized hardening:

-        current_time=configuration.show_current_time(),
+        current_time=(configuration or Configuration()).show_current_time(),

Additionally, consider normalizing configuration at function entry (outside this hunk) to also protect language=configuration.language:

def prompt(..., configuration: Configuration | None = None) -> dict:
    configuration = configuration or Configuration()
    _prompt = prompt_builder.run(
        query=query,
        language=configuration.language,
        ...
        current_time=configuration.show_current_time(),
    )

And for consistency with other modules in this PR, you may also switch the signature default to configuration: Configuration = Configuration().


159-160: Date format currently doesn’t match PR requirement (example: “August 18, 2025”).

The PR calls for a clear, unambiguous date format; however, Configuration.show_current_time() returns YYYY-MM-DD Weekday HH:MM:SS. Consider switching to a month-name format and including explicit timezone for clarity.

Proposed update to src/web/v1/services/__init__.py (outside this file):

def show_current_time(self):
    tz = pytz.timezone(self.timezone.name)
    now = datetime.now(tz)
    # Example: "August 19, 2025 14:03:25 UTC (America/Los_Angeles)"
    return f"{now.strftime('%B %d, %Y %H:%M:%S')} {now.tzname()} ({self.timezone.name})"

If you prefer to only surface the date (as your example suggests), use:

return f"{now.strftime('%B %d, %Y')} ({self.timezone.name})"

Please confirm which variant you want and I can push a patch.

wren-ai-service/src/pipelines/generation/sql_correction.py (1)

58-59: Optional: align the displayed date format with the PR’s “August 18, 2025” guidance.

The current show_current_time() uses YYYY-MM-DD Weekday HH:MM:SS. If you want to avoid ambiguity, consider switching to a month-name format and include the timezone string.

Would you like me to open a follow-up PR to adjust show_current_time() accordingly?

wren-ai-service/src/pipelines/generation/sql_generation.py (1)

74-75: Optional: update date rendering to match the PR’s “August 18, 2025” style.

If you want to strictly follow the example, update show_current_time() to output a month-name date and explicit timezone string.

wren-ai-service/src/pipelines/generation/followup_sql_generation.py (1)

80-81: Optional: match date format to requirement.

Consider switching to a month-name date format with timezone to align with the PR description.

📜 Review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 195e6a9 and c1fa959.

📒 Files selected for processing (5)
  • wren-ai-service/src/pipelines/generation/followup_sql_generation.py (4 hunks)
  • wren-ai-service/src/pipelines/generation/intent_classification.py (2 hunks)
  • wren-ai-service/src/pipelines/generation/sql_correction.py (3 hunks)
  • wren-ai-service/src/pipelines/generation/sql_generation.py (4 hunks)
  • wren-ai-service/src/pipelines/generation/sql_regeneration.py (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (5)
wren-ai-service/src/pipelines/generation/sql_correction.py (2)
wren-ai-service/src/web/v1/services/__init__.py (2)
  • Configuration (26-41)
  • show_current_time (31-38)
wren-ai-service/src/pipelines/generation/sql_question.py (1)
  • run (115-128)
wren-ai-service/src/pipelines/generation/intent_classification.py (1)
wren-ai-service/src/web/v1/services/__init__.py (1)
  • show_current_time (31-38)
wren-ai-service/src/pipelines/generation/sql_regeneration.py (1)
wren-ai-service/src/web/v1/services/__init__.py (2)
  • Configuration (26-41)
  • show_current_time (31-38)
wren-ai-service/src/pipelines/generation/followup_sql_generation.py (1)
wren-ai-service/src/web/v1/services/__init__.py (2)
  • Configuration (26-41)
  • show_current_time (31-38)
wren-ai-service/src/pipelines/generation/sql_generation.py (2)
wren-ai-service/src/web/v1/services/__init__.py (2)
  • Configuration (26-41)
  • show_current_time (31-38)
wren-ai-service/src/providers/__init__.py (1)
  • Configuration (301-303)
🪛 Ruff (0.12.2)
wren-ai-service/src/pipelines/generation/sql_regeneration.py

26-26: Redefinition of unused Configuration from line 25

Remove definition: Configuration

(F811)

🔇 Additional comments (5)
wren-ai-service/src/pipelines/generation/intent_classification.py (1)

159-162: Good addition: injecting current time into the prompt improves temporal intent handling.

Including Current Time: {{ current_time }} directly in the user prompt aligns with the PR objective and will help the model interpret relative-time phrases.

wren-ai-service/src/pipelines/generation/sql_correction.py (1)

58-61: LGTM: current time wiring and configuration passthrough are correct.

  • Current Time: {{ current_time }} added to the template.
  • prompt() signature now accepts configuration with a sane default.
  • prompt_builder.run is fed current_time=configuration.show_current_time().

These changes are coherent and align with the PR objective.

Also applies to: 70-76

wren-ai-service/src/pipelines/generation/sql_regeneration.py (1)

93-96: LGTM: time-aware prompt and configuration plumbed through.

  • Added Current Time: {{ current_time }} to the template.
  • prompt() now accepts configuration: Configuration = Configuration().
  • prompt_builder.run correctly passes current_time.

This matches the pattern used across the other pipelines in this PR.

Also applies to: 112-113, 128-129

wren-ai-service/src/pipelines/generation/sql_generation.py (1)

25-26: LGTM: consistent current time integration.

  • Imported Configuration.
  • Added Current Time: {{ current_time }} to the template.
  • prompt() accepts configuration with a default and forwards current_time to the builder.

This is consistent with the broader changes and should improve handling of relative-time queries.

Also applies to: 74-76, 98-99, 114-115

wren-ai-service/src/pipelines/generation/followup_sql_generation.py (1)

26-27: LGTM: follow-up generation now time-aware.

  • Imported Configuration.
  • Injected Current Time: {{ current_time }} into the follow-up template.
  • prompt() accepts configuration and propagates current_time.

Matches the pattern across other generation pipelines in this PR.

Also applies to: 80-81, 102-103, 118-119

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
wren-ai-service/src/pipelines/generation/sql_regeneration.py (1)

139-139: Critical: regenerate_sql (and other pipelines) return a tuple, breaking their post_process steps
All of these functions are annotated to return a dict but currently do:

return await generator(prompt=prompt.get("prompt")), generator_name

This causes AttributeError in post_process when calling .get("replies") on a tuple.

Files requiring the same fix:

  • src/pipelines/generation/sql_regeneration.py:139
  • src/pipelines/generation/chart_generation.py:91
  • src/pipelines/generation/chart_adjustment.py:120
  • src/pipelines/generation/sql_correction.py:85
  • src/pipelines/generation/question_recommendation.py:49
  • src/pipelines/generation/intent_classification.py:298
  • src/pipelines/generation/sql_tables_extraction.py:71
  • src/pipelines/generation/semantics_description.py:75
  • src/pipelines/generation/relationship_recommendation.py:62
  • src/pipelines/generation/sql_question.py:67
  • src/pipelines/generation/sql_generation.py:126
  • src/pipelines/generation/sql_generation_reasoning.py:84

Apply this patch in each place to return only the LLM result:

-    return await generator(prompt=prompt.get("prompt")), generator_name
+    return await generator(prompt=prompt.get("prompt"))
♻️ Duplicate comments (1)
wren-ai-service/src/pipelines/generation/sql_regeneration.py (1)

25-26: Fix broken import: stray diff marker and duplicate/indented import cause SyntaxError (and F811).

Line 25 contains a leading "-" (leftover diff marker) and Line 26 is indented. Both trigger SyntaxError per static analysis, and the duplicate import trips Ruff F811. Keep a single, properly unindented import.

Apply this diff:

-from src.web.v1.services import Configuration
- from src.web.v1.services import Configuration
+from src.web.v1.services import Configuration
🧹 Nitpick comments (2)
wren-ai-service/src/pipelines/generation/sql_regeneration.py (2)

111-114: Avoid instantiating Configuration as a default argument (shared mutable default).

Using a Pydantic/BaseModel instance as a default can lead to shared state across invocations. Prefer None + instantiate inside.

Apply this diff:

-    configuration: Configuration = Configuration(),
+    configuration: Configuration | None = None,
 ) -> dict:
-    _prompt = prompt_builder.run(
+    if configuration is None:
+        configuration = Configuration()
+    _prompt = prompt_builder.run(

187-201: Optional: Thread configuration through the pipeline to allow non-default timezone.

Right now the prompt node has a configuration param, but SQLRegeneration.run() doesn’t accept/pass it, so callers can’t override the default UTC/timezone. If you want request- or project-scoped timezones, accept configuration: Configuration | None = None in run() and pass it into inputs.

Example:

# Signature
async def run(..., sql_functions: list[SqlFunction] | None = None, configuration: Configuration | None = None,):

# Inputs
inputs={
    ...
    "sql_functions": sql_functions,
    "configuration": configuration or Configuration(),
    **self._components,
    **self._configs,
}
📜 Review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c1fa959 and 4c28421.

📒 Files selected for processing (1)
  • wren-ai-service/src/pipelines/generation/sql_regeneration.py (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
wren-ai-service/src/pipelines/generation/sql_regeneration.py (1)
wren-ai-service/src/web/v1/services/__init__.py (2)
  • Configuration (26-41)
  • show_current_time (31-38)
🪛 Ruff (0.12.2)
wren-ai-service/src/pipelines/generation/sql_regeneration.py

25-25: SyntaxError: Expected an identifier, but found a keyword 'from' that cannot be used here


25-25: SyntaxError: Simple statements must be separated by newlines or semicolons


25-25: SyntaxError: Simple statements must be separated by newlines or semicolons


26-26: SyntaxError: Unexpected indentation

🔇 Additional comments (2)
wren-ai-service/src/pipelines/generation/sql_regeneration.py (2)

92-92: Good addition: explicit “Current Time” context in the user prompt.

This aligns with the PR objective to make relative time references reliable across SQL pipelines.


127-127: Correct propagation of formatted current time.

Passing current_time=configuration.show_current_time() ensures consistent, timezone-aware formatting.

@cyyeh
Copy link
Member

cyyeh commented Aug 22, 2025

@OmarAhmed-A there is conflict with the main branch and please make sure the pr is the latest to the main branch, thanks!

@cyyeh
Copy link
Member

cyyeh commented Aug 22, 2025

@OmarAhmed-A thanks for raising the pr! Since the update include several time related instructions in multiple pipelines, I suggest you try different kinds of questions that include time, see how it works and share with us. I could do this on my end here too, but not my high priority as of now. Welcome to discuss the testing results with me in discord. Thanks

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.

Wrenai doesn't know Todays date!

2 participants