Skip to content

Feat(unified router) - Merged temporal classification and query prompt into a single prompt for faster query results #374

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 6 commits into from
Apr 7, 2025

Conversation

oindrila-b
Copy link
Contributor

@oindrila-b oindrila-b commented Apr 7, 2025

Description

The PR contains the following changes :

  • Added .env.default that was removed previously.
  • Modified the searchQueryPrompt to incorporate prompt from temporalEventClassification, resulting into searchQueryPrompt becoming a unified router for query, thereby removing the need to separately classify the query as an event or meeting.

Testing

Tested locally using eval.ts

Additional Notes

None so far

Summary by CodeRabbit

  • New Features

    • Enhanced query processing now better interprets time-related requests by distinguishing between upcoming and past events. This improvement helps provide more accurate responses to ambiguous inquiries.
    • Introduced a new environment configuration file with essential application settings and encryption keys.
  • Chores

    • Updated default configuration settings now include recommended placeholder values for key integrations and security measures, ensuring smoother deployment and improved system reliability.

Copy link

coderabbitai bot commented Apr 7, 2025

Walkthrough

This pull request adds a new environment configuration file and updates several modules to incorporate a temporal direction mechanism. The new .env.default file defines key environment variables for database, host, encryption, and model settings. In addition, modifications in the AI evaluation and prompt modules introduce a temporalDirection property to better handle ambiguous user inputs, while the chat API updates type definitions and parameter handling accordingly. These changes streamline how temporal context is processed throughout the application.

Changes

File(s) Change Summary
server/.env.default Introduces a new configuration file with environment variables: DATABASE_HOST, VESPA_HOST, HOST, ENCRYPTION_KEY, JWT_SECRET, SERVICE_ACCOUNT_ENCRYPTION_KEY, and EMBEDDING_MODEL.
server/ai/eval.ts Adds a new property temporalDirection initialized to null in the parsed object; expands condition checks for parsed.answer to include empty strings. Simplifies the creation of the classification object by using temporalDirection directly.
server/ai/prompts.ts Updates the searchQueryPrompt function to determine and return a temporalDirection ("next", "prev", or null) based on user queries related to events; modifies the output JSON structure to include the new field.
server/api/chat.ts Simplifies type definitions by changing the classification parameter from TemporalClassifier & { cost: number } to TemporalClassifier in multiple functions. Initializes costArr as an empty array and adds temporalDirection initialized to null in the parsed variable.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant P as Prompt (searchQueryPrompt)
    participant E as Evaluator (endToEndFlow)
    participant C as Chat API

    U->>P: Submit query
    P->>P: Evaluate query for temporal direction
    P->>U: Return JSON { answer, queryRewrite, temporalDirection }
    
    U->>E: Send processed request
    E->>E: Initialize parsed object (includes temporalDirection: null)
    E->>E: Check for ambiguous answer (null or empty string)
    E->>C: Forward classification using parsed.temporalDirection
    C->>C: Process message with updated types
    C->>U: Return final response
Loading

Poem

I'm a bunny in the code field, hopping along,
With temporal trails that now make our flow strong.
Encryption keys and queries dance in delight,
Each update a carrot for a future so bright.
Happy hops to the dev team—coding feels just right! 🐇✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cda9916 and 5f635cd.

📒 Files selected for processing (1)
  • server/ai/eval.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • server/ai/eval.ts

🪧 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @oindrila-b, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request focuses on unifying the query routing mechanism by incorporating temporal event classification directly into the searchQueryPrompt. This eliminates the need for a separate classification step, potentially leading to faster query results. The changes include adding a missing .env.default file and modifying the searchQueryPrompt to include logic for determining temporal direction (past or future) within the prompt itself. The eval.ts and chat.ts files are updated to reflect these changes, removing the explicit call to temporalEventClassification and adjusting the data structures accordingly.

Highlights

  • Unified Query Router: The searchQueryPrompt now incorporates temporal event classification, streamlining the query processing pipeline.
  • Temporal Direction Detection: The prompt is enhanced to identify whether a query refers to a past or future event, indicated by the temporalDirection field in the JSON output.
  • Code Simplification: The separate temporalEventClassification call is removed from eval.ts and chat.ts, simplifying the code and potentially improving performance.

Changelog

Click here to see the changelog
  • server/.env.default
    • Added the .env.default file with default environment variables.
  • server/ai/eval.ts
    • Modified the parsed object to include a direction property.
    • Removed the explicit call to temporalEventClassification and associated cost calculation.
    • Modified the condition to check for empty parsed answer
  • server/ai/prompts.ts
    • Modified searchQueryPrompt to include logic for temporal event classification, adding instructions to determine temporalDirection and include it in the JSON output.
  • server/api/chat.ts
    • Modified generatePointQueryTimeExpansion and UnderstandMessageAndAnswer functions to accept TemporalClassifier without the cost property.
    • Removed the costArr initialization with classification.cost in generatePointQueryTimeExpansion.
    • Modified MessageApi and MessageRetryApi to remove the explicit call to temporalEventClassification and instead rely on the temporalDirection from the parsed prompt output.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A prompt made unified,
No longer split and stratified,
One step decreased,
For queries released,
Results come, amplified.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a unified router for queries by incorporating the prompt from temporalEventClassification into searchQueryPrompt. This aims to improve query result speed by eliminating the need for separate classification. The changes include adding a .env.default file and modifying the eval.ts and prompts.ts files. Overall, the changes seem well-structured and aligned with the goal of faster query results.

Summary of Findings

  • Missing Cost Calculation: The temporalEventClassification cost is no longer being tracked or used in the generatePointQueryTimeExpansion function, which could lead to inaccurate cost reporting. It's important to ensure that all relevant costs are accounted for to provide a complete picture of resource usage.
  • Incomplete Temporal Classification: The temporalEventClassification function is removed from endToEndFlow in eval.ts, but the classification object is still being assigned a direction based on parsed.temporalDirection. This could lead to incorrect classification if the temporalDirection is not properly set in the unified prompt.
  • Default Encryption Keys: The .env.default file includes default encryption keys. While this is convenient for local development, it poses a significant security risk if these keys are used in a production environment. It's crucial to ensure that these default keys are replaced with unique, randomly generated keys in any deployed instance.

Merge Readiness

The pull request introduces significant changes to the query routing logic. While the changes aim to improve performance, there are some potential issues that need to be addressed before merging. Specifically, the missing cost calculation and incomplete temporal classification should be investigated and resolved. Additionally, the default encryption keys in .env.default pose a security risk and should be addressed before merging. I am unable to approve this pull request, and recommend that others review and approve this code before merging.

Comment on lines +4 to +6
ENCRYPTION_KEY= "ldRWi1hLL8zMT2yxAXwjjTJUYs1ikROLqRxFrs0qpvU=" # DEFAULT_VALUE, KINDLY GENERATE YOUR OWN
JWT_SECRET= "ldRWi1hLL8zMT2yxAXwjjTJUYs1ikROLqRxFrs0qpvU=" # DEFAULT_VALUE, KINDLY GENERATE YOUR OWN
SERVICE_ACCOUNT_ENCRYPTION_KEY= "ldRWi1hLL8zMT2yxAXwjjTJUYs1ikROLqRxFrs0qpvU=" # DEFAULT_VALUE, KINDLY GENERATE YOUR OWN
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

These are default encryption keys. While this is convenient for local development, it poses a significant security risk if these keys are used in a production environment. It's crucial to ensure that these default keys are replaced with unique, randomly generated keys in any deployed instance. Consider adding a warning in the README about this.

@@ -728,7 +728,7 @@ async function* generatePointQueryTimeExpansion(
const maxIterations = 10
const weekInMs = 12 * 24 * 60 * 60 * 1000
const direction = classification.direction as string
let costArr: number[] = [classification.cost]
let costArr: number[] = []
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The temporalEventClassification cost is no longer being tracked or used. Ensure that all relevant costs are accounted for to provide a complete picture of resource usage. Consider adding the cost from the unified prompt to costArr.

Suggested change
let costArr: number[] = []
let costArr: number[] = []; // Initialize as empty array

Comment on lines +889 to +891
const classification: TemporalClassifier = {
direction: parsed.temporalDirection,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The classification object is being assigned a direction based on parsed.temporalDirection. Ensure that parsed.temporalDirection is reliably set by the unified prompt, otherwise the classification might be incorrect. Consider adding a fallback mechanism or validation to ensure the direction is properly determined.

Suggested change
const classification: TemporalClassifier = {
direction: parsed.temporalDirection,
}
const classification: TemporalClassifier = {
direction: parsed.temporalDirection ?? null, // Add a null fallback
}

Comment on lines +889 to +891
const classification: TemporalClassifier = {
direction: parsed.temporalDirection,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding a check to ensure that parsed is not null or undefined before accessing parsed.temporalDirection to avoid potential runtime errors.

Suggested change
const classification: TemporalClassifier = {
direction: parsed.temporalDirection,
}
const classification: TemporalClassifier = {
direction: parsed?.temporalDirection ?? null, // Add a null fallback
}

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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 72e0bc0 and cda9916.

📒 Files selected for processing (4)
  • server/.env.default (1 hunks)
  • server/ai/eval.ts (2 hunks)
  • server/ai/prompts.ts (1 hunks)
  • server/api/chat.ts (7 hunks)
🧰 Additional context used
🧬 Code Definitions (2)
server/ai/eval.ts (1)
server/ai/types.ts (1)
  • TemporalClassifier (59-61)
server/api/chat.ts (1)
server/ai/types.ts (1)
  • TemporalClassifier (59-61)
🔇 Additional comments (9)
server/.env.default (1)

1-7:

❓ Verification inconclusive

New environment configuration file looks good.

This new .env.default file provides helpful default configuration values for database connections, hosts, and necessary encryption keys. The embedding model is also properly specified.

I recommend verifying that the default encryption key is appropriately flagged as a placeholder. While the comment indicates users should generate their own keys, consider checking if this value has sufficient entropy as a placeholder:


🏁 Script executed:

#!/bin/bash
# Check if the encryption key has sufficient entropy (at least 32 bytes base64)
DEFAULT_KEY="ldRWi1hLL8zMT2yxAXwjjTJUYs1ikROLqRxFrs0qpvU="
DECODED_LENGTH=$(echo $DEFAULT_KEY | base64 -d | wc -c)
echo "Decoded key length: $DECODED_LENGTH bytes"

if [ $DECODED_LENGTH -ge 32 ]; then
  echo "✅ Default key has sufficient entropy for a placeholder"
else
  echo "⚠️ Default key has insufficient entropy for a placeholder"
fi

Length of output: 458


Attention Required: Verify the Entropy Check Mechanism for the Placeholder Key

The new .env.default file provides appropriate default values and clearly flags the encryption keys as placeholders. However, when verifying that the default encryption key has sufficient entropy as a placeholder, the entropy check failed because the base64 command was not found in the current environment. Please ensure one of the following:

  • The base64 utility is available so that the entropy check can correctly validate that the provided key decodes to at least 32 bytes.
  • Alternatively, manually verify in an environment with base64 support that the key ldRWi1hLL8zMT2yxAXwjjTJUYs1ikROLqRxFrs0qpvU= meets the required entropy.

Once the entropy verification process is confirmed in the proper environment, the placeholder key usage can be considered acceptable.

server/ai/eval.ts (2)

859-859: Updated parsed object to include direction field.

This change adds the direction field to the parsed object, aligning with the unified router approach mentioned in the PR objectives.


883-884: Improved handling of empty responses.

Good enhancement to handle both null and empty string responses, making the code more robust against different types of empty responses.

server/ai/prompts.ts (2)

700-714: Integrated temporal classification into search query prompt.

This is a key implementation of the PR's objective - merging the temporal classification logic directly into the search query prompt. The code clearly defines criteria for determining temporal direction based on the query context.


717-719: Updated JSON output structure to include temporal direction.

The output schema now includes the temporalDirection field, which supports the unified router approach. The documentation for this field is also appropriately added.

Also applies to: 722-723

server/api/chat.ts (4)

718-718: Simplified the classification parameter type.

The classification parameter has been simplified by removing the cost property, making it more focused on its primary purpose of temporal classification.


731-731: Initialized costArr as an empty array.

The cost array initialization has been modified to start empty instead of including the classification cost. This aligns with the simplified classification parameter type.


1128-1128: Added temporalDirection to parsed object in MessageApi.

The parsed object now includes temporalDirection to support the unified router approach. The classification object is also correctly instantiated using this property.

Also applies to: 1219-1219


1530-1530: Added temporalDirection to parsed object in MessageRetryApi.

Similar to the MessageApi, the MessageRetryApi now includes temporalDirection in its parsed object and properly uses it for the classification.

Also applies to: 1619-1619

Comment on lines +889 to +891
const classification: TemporalClassifier = {
direction: parsed.temporalDirection,
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fixed field name inconsistency.

There's a field name inconsistency here. The parsed object has a direction field (line 859), but you're using temporalDirection to set the classification object's direction.

Apply this fix to ensure consistency:

const classification: TemporalClassifier = {
-  direction: parsed.temporalDirection,
+  direction: parsed.direction,
}
📝 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
const classification: TemporalClassifier = {
direction: parsed.temporalDirection,
}
const classification: TemporalClassifier = {
direction: parsed.direction,
}

@zereraz zereraz merged commit 2f85493 into main Apr 7, 2025
4 checks passed
@zereraz zereraz deleted the feat/unified-router branch April 7, 2025 20:05
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.

2 participants