Skip to content

feat(wren-ai-service): add context window size handling in LLMProvider and related components #1693

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 4 commits into from
May 27, 2025

Conversation

yichieh-lu
Copy link
Contributor

@yichieh-lu yichieh-lu commented May 27, 2025

This PR integrates the context_window_size parameter from LLMProvider into the token limit checking logic. This ensures that prompt + context do not exceed the model’s actual maximum context length.

Summary by CodeRabbit

  • New Features

    • Made the context window size for language models configurable, allowing dynamic adjustment instead of a fixed token count limit.
    • Updated preprocessing, schema retrieval, and model configuration to use the dynamic context window size.
  • Bug Fixes

    • Ensured consistent handling of the context window size across the language model pipeline and preprocessing steps.

Copy link
Contributor

coderabbitai bot commented May 27, 2025

Walkthrough

A configurable context_window_size parameter was introduced and propagated throughout the codebase, replacing hardcoded token count limits. It was added to LLM provider classes, included in pipeline configurations, explicitly handled in model processing logic, and incorporated into preprocessing and schema retrieval functions to enable dynamic token limit control.

Changes

File(s) Change Summary
src/core/provider.py Added get_context_window_size method to LLMProvider abstract base class.
src/pipelines/retrieval/preprocess_sql_data.py Updated preprocess function signature and logic to use dynamic context_window_size; added config key in PreprocessSqlData constructor.
src/pipelines/retrieval/db_schema_retrieval.py Added context_window_size parameter to check_using_db_schemas_without_pruning function and DbSchemaRetrieval constructor config.
src/providers/init.py Modified llm_processor function to treat context_window_size as a distinct top-level model attribute with default 100000.
src/providers/llm/litellm.py Added context_window_size parameter with default 100000 to LitellmLLMProvider constructor and stored as instance attribute.
deployment/kustomizations/base/cm.yaml Added context_window_size: 1000000 to three LLM model entries in ConfigMap.
docker/config.example.yaml Added context_window_size: 1000000 to three LLM model entries in example configuration file.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant LLMProvider
    participant PreprocessSqlData
    participant preprocess

    User->>LLMProvider: get_context_window_size()
    LLMProvider-->>User: returns context_window_size

    User->>PreprocessSqlData: initialize with llm_provider
    PreprocessSqlData->>LLMProvider: get_context_window_size()
    PreprocessSqlData-->>PreprocessSqlData: set config["context_window_size"]

    PreprocessSqlData->>preprocess: call with context_window_size
    preprocess-->>PreprocessSqlData: process data with dynamic token limit
Loading

Suggested labels

wren-ai-service, deployment

Suggested reviewers

  • imAsterSun

Poem

In the warren of code, a window grew wide,
Context now stretches, no need to hide!
From provider to pipeline, the value flows through,
Dynamic and nimble, for tasks old and new.
With tokens unbounded, our queries take flight—
Hooray for the patch! The future looks bright.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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 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.

@cyyeh cyyeh self-requested a review May 27, 2025 11:22
@cyyeh cyyeh added module/ai-service ai-service related ci/ai-service ai-service related labels May 27, 2025
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

🔭 Outside diff range comments (1)
wren-ai-service/src/pipelines/retrieval/db_schema_retrieval.py (1)

298-300: 🛠️ Refactor suggestion

Update hardcoded log message to reflect dynamic threshold.

The log message still references the hardcoded value of "100,000" but the actual threshold is now dynamic. This could confuse users when debugging.

-        logger.info(
-            "db_schemas token count is greater than 100,000, so we will prune columns"
-        )
+        logger.info(
+            f"db_schemas token count is greater than {check_using_db_schemas_without_pruning['tokens']}, so we will prune columns"
+        )

However, this would require accessing the context window size here. A better approach might be to pass the threshold value or access it from the configs:

-        logger.info(
-            "db_schemas token count is greater than 100,000, so we will prune columns"
-        )
+        logger.info(
+            f"db_schemas token count ({check_using_db_schemas_without_pruning['tokens']}) is greater than context window size, so we will prune columns"
+        )
🧹 Nitpick comments (1)
wren-ai-service/src/pipelines/retrieval/db_schema_retrieval.py (1)

469-469: Add error handling for context window size retrieval.

The integration with llm_provider.get_context_window_size() looks good, but consider adding validation to ensure the returned value is a positive integer.

-            "context_window_size": llm_provider.get_context_window_size(),
+            "context_window_size": max(1, llm_provider.get_context_window_size() or 100000),

This ensures the context window size is always a positive value with a reasonable fallback.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5471e09 and 1b431c2.

📒 Files selected for processing (3)
  • deployment/kustomizations/base/cm.yaml (1 hunks)
  • docker/config.example.yaml (1 hunks)
  • wren-ai-service/src/pipelines/retrieval/db_schema_retrieval.py (3 hunks)
✅ Files skipped from review due to trivial changes (2)
  • docker/config.example.yaml
  • deployment/kustomizations/base/cm.yaml
🧰 Additional context used
🧬 Code Graph Analysis (1)
wren-ai-service/src/pipelines/retrieval/db_schema_retrieval.py (1)
wren-ai-service/src/core/provider.py (1)
  • get_context_window_size (17-18)
🔇 Additional comments (2)
wren-ai-service/src/pipelines/retrieval/db_schema_retrieval.py (2)

233-233: Good addition of configurable parameter.

Adding the context_window_size parameter makes the function more flexible and aligns with the PR objective of replacing hardcoded token limits.


273-273:

✅ Verification successful

Verify function callers are updated for the new parameter.

The replacement of the hardcoded 100,000 with the dynamic context_window_size parameter is correct. However, ensure all callers of check_using_db_schemas_without_pruning have been updated to provide this new required parameter.


🏁 Script executed:

#!/bin/bash
# Description: Verify all function calls to check_using_db_schemas_without_pruning include the new context_window_size parameter
# Expected: All calls should have the correct number of parameters

echo "Searching for function calls to check_using_db_schemas_without_pruning..."
rg -A 10 "check_using_db_schemas_without_pruning\(" --type py

echo "Searching for function definition..."
ast-grep --pattern 'def check_using_db_schemas_without_pruning($$$)'

Length of output: 7279


No callers require updating – no invocations found
A global search for check_using_db_schemas_without_pruning( returned only its own definition and no call sites in the repo. There are no callers that need the new context_window_size parameter.

Copy link
Member

@cyyeh cyyeh left a comment

Choose a reason for hiding this comment

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

lgtm, thanks for contribution

@cyyeh cyyeh merged commit 859cfe3 into Canner:main May 27, 2025
8 of 13 checks passed
@yichieh-lu yichieh-lu deleted the fix/issue-1684-token-size branch May 28, 2025 06:28
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants