Repository navigation
chore(wren-ai-service): add separate reasoning pipeline - #1212
Conversation
WalkthroughThis pull request introduces a new Changes
Sequence DiagramsequenceDiagram
participant User
participant AskService
participant SQLGenerationReasoning
participant SQLGeneration
User->>AskService: Submit query
AskService->>SQLGenerationReasoning: Generate reasoning plan
SQLGenerationReasoning-->>AskService: Return reasoning plan
AskService->>SQLGeneration: Pass query with reasoning plan
SQLGeneration->>SQLGeneration: Generate SQL using reasoning
SQLGeneration-->>AskService: Return generated SQL
AskService-->>User: Present results
Possibly related PRs
Suggested reviewers
Poem
✨ Finishing Touches
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 5
🧹 Nitpick comments (4)
wren-ai-service/src/pipelines/generation/sql_generation.py (1)
49-51: Handle possible None or empty 'sql_generation_reasoning' in prompt.If
sql_generation_reasoningisNoneor an empty string, the prompt template will include an empty "REASONING PLAN" section, which might confuse the model. Consider adding a conditional block in the template to include the "REASONING PLAN" section only whensql_generation_reasoningis provided.Modify the prompt template as follows:
### QUESTION ### User's Question: {{ query }} Current Time: {{ current_time }} +{% if sql_generation_reasoning %} ### REASONING PLAN ### {{ sql_generation_reasoning }} +{% endif %} Let's think step by step.Update the
promptfunction to ensuresql_generation_reasoningis always a string:def prompt( query: str, documents: List[str], sql_generation_reasoning: str, prompt_builder: PromptBuilder, configuration: Configuration | None = None, sql_samples: List[Dict] | None = None, has_calculated_field: bool = False, has_metric: bool = False, ) -> dict: + if not sql_generation_reasoning: + sql_generation_reasoning = "" return prompt_builder.run( query=query, documents=documents, sql_generation_reasoning=sql_generation_reasoning, instructions=construct_instructions( configuration, has_calculated_field, has_metric, sql_samples, ), sql_samples=sql_samples, current_time=configuration.show_current_time(), )Also applies to: 71-71
wren-ai-service/src/pipelines/generation/followup_sql_generation.py (1)
61-63: Handle possible None or empty 'sql_generation_reasoning' in prompt.If
sql_generation_reasoningisNoneor an empty string, the prompt template will include an empty "REASONING PLAN" section. Consider adding a conditional block in the template to include the "REASONING PLAN" section only whensql_generation_reasoningis provided to prevent confusion.Modify the prompt template:
### QUESTION ### User's Follow-up Question: {{ query }} Current Time: {{ current_time }} +{% if sql_generation_reasoning %} ### REASONING PLAN ### {{ sql_generation_reasoning }} +{% endif %} Let's think step by step.Update the
promptfunction to handlesql_generation_reasoningbeingNone:def prompt( query: str, documents: List[str], sql_generation_reasoning: str, history: AskHistory, configuration: Configuration, prompt_builder: PromptBuilder, sql_samples: List[Dict] | None = None, has_calculated_field: bool = False, has_metric: bool = False, ) -> dict: + if not sql_generation_reasoning: + sql_generation_reasoning = "" previous_query_summaries = [step.summary for step in history.steps if step.summary] return prompt_builder.run( query=query, documents=documents, sql_generation_reasoning=sql_generation_reasoning, history=history, previous_query_summaries=previous_query_summaries, instructions=construct_instructions( configuration, has_calculated_field, has_metric, sql_samples, ), current_time=configuration.show_current_time(), sql_samples=sql_samples, )Also applies to: 86-86
deployment/kustomizations/base/cm.yaml (1)
181-183: Consider adding documentation for the new reasoning pipeline.The
sql_generation_reasoningpipeline has been consistently added across all configurations, but its purpose, integration points, and usage patterns should be documented. Consider:
- Adding comments in the configuration files explaining the pipeline's role
- Updating relevant documentation to describe how this pipeline enhances SQL generation
- Including examples of how it integrates with existing pipelines
This will help future maintainers understand the reasoning pipeline's purpose and proper usage.
wren-ai-service/src/globals.py (1)
111-113: Consider separating configurations for different use cases.Both
ask_serviceandquestion_recommendationservices reuse the samepipe_components["sql_generation_reasoning"]configuration. While this promotes consistency, please verify if:
- The reasoning requirements are truly identical for both use cases
- Future changes might require different configurations
If the requirements might diverge in the future, consider using separate configurations like:
ask_service_sql_generation_reasoningquestion_recommendation_sql_generation_reasoningThis approach is already used for other components (e.g.,
question_recommendation_sql_generation).Also applies to: 227-229
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (13)
deployment/kustomizations/base/cm.yaml(1 hunks)docker/config.example.yaml(1 hunks)wren-ai-service/src/globals.py(2 hunks)wren-ai-service/src/pipelines/generation/__init__.py(2 hunks)wren-ai-service/src/pipelines/generation/followup_sql_generation.py(5 hunks)wren-ai-service/src/pipelines/generation/sql_generation.py(5 hunks)wren-ai-service/src/pipelines/generation/sql_generation_reasoning.py(1 hunks)wren-ai-service/src/pipelines/generation/utils/sql.py(1 hunks)wren-ai-service/src/web/v1/services/ask.py(3 hunks)wren-ai-service/src/web/v1/services/question_recommendation.py(3 hunks)wren-ai-service/tools/config/.env.dev.example(1 hunks)wren-ai-service/tools/config/config.example.yaml(1 hunks)wren-ai-service/tools/config/config.full.yaml(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- wren-ai-service/src/pipelines/generation/init.py
⏰ Context from checks skipped due to timeout of 90000ms (4)
- GitHub Check: pytest
- GitHub Check: pytest
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: Analyze (go)
🔇 Additional comments (13)
wren-ai-service/src/web/v1/services/question_recommendation.py (2)
70-70: LGTM! Optional configuration parameter with proper default.The addition of the optional configuration parameter with a default value maintains backward compatibility.
97-98: Verify sql_generation_reasoning parameter handling.The sql_generation_reasoning parameter is passed to the SQL generation pipeline, but we should ensure it handles None values gracefully.
Run this script to verify the parameter handling in the SQL generation pipeline:
✅ Verification successful
Parameter handling for sql_generation_reasoning is properly implemented
The parameter is safely handled through a dedicated pipeline that generates it before use, and it's properly integrated into the template system that handles null values gracefully. The code structure ensures the parameter is always provided through well-defined interfaces.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for sql_generation parameter handling in the codebase ast-grep --pattern 'def run($args) { $$$ sql_generation_reasoning = $expr $$$ }'Length of output: 88
Script:
#!/bin/bash # Search for sql_generation_reasoning usage with Python syntax ast-grep --pattern 'def $func($params): $$$ sql_generation_reasoning$$$' # Backup search using ripgrep rg -n "sql_generation_reasoning" -B 2 -A 2Length of output: 13814
wren-ai-service/src/globals.py (4)
111-113: LGTM! Consistent pipeline integration.The sql_generation_reasoning pipeline is correctly added to the ask_service with proper component configuration.
227-229: LGTM! Consistent pipeline reuse.The same sql_generation_reasoning pipeline configuration is reused in question_recommendation service, promoting code reuse.
227-229: Document the impact on question recommendation quality.While adding reasoning capabilities is beneficial, please ensure:
- The reasoning step enhances rather than impedes the question recommendation process
- The performance impact is acceptable for the question recommendation use case
- The documentation is updated to explain how the reasoning step contributes to question recommendations
✅ Verification successful
SQL Generation Reasoning integration verified as correct and beneficial
The component is properly integrated into the question recommendation pipeline, following the same pattern as the ask service. It enhances the recommendation process by adding reasoning capabilities without introducing performance overhead or architectural inconsistencies.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Check for documentation and performance considerations # Search for documentation about the reasoning step's role in question recommendations echo "Checking for documentation:" rg -l "sql.*generation.*reasoning" --type md # Search for any performance tests or benchmarks echo -e "\nChecking for performance tests:" rg -l "performance|benchmark" --type python | grep -i "test" # Search for the QuestionRecommendation class implementation to understand integration echo -e "\nChecking QuestionRecommendation implementation:" ast-grep --pattern 'class QuestionRecommendation { $$$ def process($$$) { $$$ } $$$ }'Length of output: 494
Script:
#!/bin/bash # Search for Python files related to question recommendations echo "Searching for question recommendation files:" fd -e py | grep -i "question.*recommend" # Search for Python test files echo -e "\nSearching for test files:" fd -e py | grep -i "test" # Try alternative pattern for QuestionRecommendation class echo -e "\nSearching for QuestionRecommendation class:" rg "class.*Question.*Recommend" -A 10 # Search for SQL generation reasoning mentions in Python files echo -e "\nSearching for SQL generation reasoning usage:" rg "sql_generation_reasoning" -A 5Length of output: 20173
111-113: Verify integration with surrounding pipeline steps in ask_service.The placement of
sql_generation_reasoningbetweensql_generationandsql_correctionis logical. However, please ensure:
- The output format from
sql_generationmatches the input requirements ofsql_generation_reasoning- The output from
sql_generation_reasoningis compatible withsql_correction✅ Verification successful
Integration between pipeline steps is properly designed and compatible
The verification confirms that:
- SQLGenerationReasoning's output (reasoning_plan) is correctly consumed by SQLGeneration's prompt template
- SQLGeneration's output format is compatible with SQLCorrection's input requirements
- The pipeline order (reasoning → generation → correction) is logically sound with matching interfaces
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the interface compatibility between pipeline steps # Search for the SQLGeneration class implementation to check its output format echo "Checking SQLGeneration output format:" ast-grep --pattern 'class SQLGeneration { $$$ def process($$$) { $$$ } $$$ }' # Search for the SQLGenerationReasoning class implementation to check its input/output format echo -e "\nChecking SQLGenerationReasoning input/output format:" ast-grep --pattern 'class SQLGenerationReasoning { $$$ def process($$$) { $$$ } $$$ }' # Search for the SQLCorrection class implementation to check its input format echo -e "\nChecking SQLCorrection input format:" ast-grep --pattern 'class SQLCorrection { $$$ def process($$$) { $$$ } $$$ }'Length of output: 579
Script:
#!/bin/bash # Find Python files containing these classes echo "Looking for relevant Python files:" fd -e py echo -e "\nSearching for SQLGeneration class:" rg -A 10 "class SQLGeneration" echo -e "\nSearching for SQLGenerationReasoning class:" rg -A 10 "class SQLGenerationReasoning" echo -e "\nSearching for SQLCorrection class:" rg -A 10 "class SQLCorrection"Length of output: 13113
Script:
#!/bin/bash # Check process methods and their interfaces echo "Checking SQLGeneration process method:" rg -A 20 "def process" wren-ai-service/src/pipelines/generation/sql_generation.py echo -e "\nChecking SQLGenerationReasoning process method:" rg -A 20 "def process" wren-ai-service/src/pipelines/generation/sql_generation_reasoning.py echo -e "\nChecking SQLCorrection process method:" rg -A 20 "def process" wren-ai-service/src/pipelines/generation/sql_correction.pyLength of output: 552
Script:
#!/bin/bash echo "=== SQLGeneration implementation ===" cat wren-ai-service/src/pipelines/generation/sql_generation.py echo -e "\n=== SQLGenerationReasoning implementation ===" cat wren-ai-service/src/pipelines/generation/sql_generation_reasoning.py echo -e "\n=== SQLCorrection implementation ===" cat wren-ai-service/src/pipelines/generation/sql_correction.py echo -e "\n=== BasicPipeline implementation ===" fd -t f "basic.py|pipeline.py" src/pipelines | xargs catLength of output: 12726
wren-ai-service/src/web/v1/services/ask.py (1)
304-304: LGTM! Consistent parameter passing.The sql_generation_reasoning is correctly passed to both followup and regular SQL generation pipelines.
Also applies to: 318-318
wren-ai-service/src/pipelines/generation/utils/sql.py (1)
336-336: LGTM! Clear prompt update for reasoning-based approach.The system prompt is updated to explicitly mention using the reasoning plan for SQL generation.
wren-ai-service/tools/config/.env.dev.example (1)
11-13: LGTM! Well-structured environment variable addition.The
LANGFUSE_PROJECT_IDvariable is properly grouped with other Langfuse-related variables and follows the example env file format.docker/config.example.yaml (1)
133-134: Verify if engine dependency is intentionally omitted.The
sql_generation_reasoningpipeline uses the same model as other SQL-related pipelines but doesn't specify an engine dependency. Please confirm if this is intentional, as most other SQL-related pipelines (e.g.,sql_generation,sql_correction) includeengine: wren_ui.wren-ai-service/tools/config/config.example.yaml (1)
147-149: Configuration is consistent with docker/config.example.yaml.The same observation about engine dependency applies here.
wren-ai-service/tools/config/config.full.yaml (1)
164-165: Configuration is consistent with other config files.The same observation about engine dependency applies here.
deployment/kustomizations/base/cm.yaml (1)
181-183: LGTM! Configuration is consistently applied across all deployment contexts.The
sql_generation_reasoningpipeline configuration is properly integrated into the Kubernetes deployment and maintains consistency with other configuration files.
| sql_generation_reasoning_system_prompt = """ | ||
| ### TASK ### | ||
| You are a helpful data analyst that great at thinking deeply and reasoning about the user's question and the database schema and give a step by step reasoning plan in order to answer user's question. | ||
|
|
||
| ### INSTRUCTIONS ### | ||
| 1. Think deeply and reason about the user's question and the database schema. | ||
| 2. Give a step by step reasoning plan in order to answer user's question. | ||
| 3. The reasoning plan should be in the language same as the language user provided in the input. | ||
| 4. Make sure considering current time provided in the input. | ||
| """ | ||
|
|
There was a problem hiding this comment.
🛠️ Refactor suggestion
Correct grammatical errors in the system prompt.
The sql_generation_reasoning_system_prompt contains several grammatical errors. Please consider the following corrections:
- Line 21: Change "You are a helpful data analyst that great at thinking deeply and reasoning..." to "You are a helpful data analyst who is great at thinking deeply and reasoning..."
- Line 26: Change "Make sure considering current time provided in the input." to "Make sure to consider the current time provided in the input."
Apply this diff to correct the grammatical errors:
sql_generation_reasoning_system_prompt = """
### TASK ###
-You are a helpful data analyst that great at thinking deeply and reasoning about the user's question and the database schema and give a step by step reasoning plan in order to answer user's question.
+You are a helpful data analyst who is great at thinking deeply and reasoning about the user's question and the database schema, and you provide a step-by-step reasoning plan in order to answer the user's question.
### INSTRUCTIONS ###
1. Think deeply and reason about the user's question and the database schema.
2. Give a step-by-step reasoning plan in order to answer the user's question.
3. The reasoning plan should be in the same language as the language provided by the user in the input.
-4. Make sure considering current time provided in the input.
+4. Make sure to consider the current time provided in the input.
"""📝 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.
| sql_generation_reasoning_system_prompt = """ | |
| ### TASK ### | |
| You are a helpful data analyst that great at thinking deeply and reasoning about the user's question and the database schema and give a step by step reasoning plan in order to answer user's question. | |
| ### INSTRUCTIONS ### | |
| 1. Think deeply and reason about the user's question and the database schema. | |
| 2. Give a step by step reasoning plan in order to answer user's question. | |
| 3. The reasoning plan should be in the language same as the language user provided in the input. | |
| 4. Make sure considering current time provided in the input. | |
| """ | |
| sql_generation_reasoning_system_prompt = """ | |
| ### TASK ### | |
| You are a helpful data analyst who is great at thinking deeply and reasoning about the user's question and the database schema, and you provide a step-by-step reasoning plan in order to answer the user's question. | |
| ### INSTRUCTIONS ### | |
| 1. Think deeply and reason about the user's question and the database schema. | |
| 2. Give a step-by-step reasoning plan in order to answer the user's question. | |
| 3. The reasoning plan should be in the same language as the language provided by the user in the input. | |
| 4. Make sure to consider the current time provided in the input. | |
| """ |
| configuration: Configuration | None = None, | ||
| ) -> dict: | ||
| return prompt_builder.run( | ||
| query=query, | ||
| documents=documents, | ||
| current_time=configuration.show_current_time(), | ||
| language=configuration.language, |
There was a problem hiding this comment.
Ensure 'configuration' is not None before accessing its attributes.
In the prompt function, configuration defaults to None, but its attributes are accessed without checking if it's None. This could lead to an AttributeError if configuration is not provided.
Apply this diff to set a default Configuration if configuration is None:
def prompt(
query: str,
documents: List[str],
prompt_builder: PromptBuilder,
configuration: Configuration | None = None,
) -> dict:
+ if configuration is None:
+ configuration = Configuration()
return prompt_builder.run(
query=query,
documents=documents,
current_time=configuration.show_current_time(),
language=configuration.language,
)📝 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.
| configuration: Configuration | None = None, | |
| ) -> dict: | |
| return prompt_builder.run( | |
| query=query, | |
| documents=documents, | |
| current_time=configuration.show_current_time(), | |
| language=configuration.language, | |
| configuration: Configuration | None = None, | |
| ) -> dict: | |
| if configuration is None: | |
| configuration = Configuration() | |
| return prompt_builder.run( | |
| query=query, | |
| documents=documents, | |
| current_time=configuration.show_current_time(), | |
| language=configuration.language, |
Summary by CodeRabbit
New Features
sql_generation_reasoningusing GPT-4o mini modelConfiguration Updates