Skip to content

fix: .env not loaded on poetry run generate #348

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 5 commits into from
Oct 8, 2024
Merged

Conversation

marcusschiesser
Copy link
Collaborator

@marcusschiesser marcusschiesser commented Oct 8, 2024

Calling poetry run generate did not use the variables in .env file (as __init__.py was imported before calling load_dotenv)

Summary by CodeRabbit

  • Bug Fixes

    • Resolved an issue with the .env file not being loaded during command execution, ensuring environment variables are recognized.
    • Implemented a fix to programmatically ensure the index for LlamaCloud, enhancing indexing reliability.
  • Refactor

    • Updated import paths for the get_chat_engine function across multiple files to reflect a new module structure, while maintaining existing functionality and control flow.
    • Enhanced configuration management by centralizing environment variable handling within a new method.
  • Documentation

    • Clarified the structure and accessibility of the get_chat_engine function in the codebase.

Copy link

changeset-bot bot commented Oct 8, 2024

🦋 Changeset detected

Latest commit: 2e46ac4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
create-llama Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

coderabbitai bot commented Oct 8, 2024

Walkthrough

The changes in this pull request ensure that the .env file is loaded during the execution of the command poetry run generate, allowing environment variables to be recognized. The import path for the get_chat_engine function has been updated in two chat-related modules, indicating a restructuring of module organization. Additionally, the import statement for get_chat_engine has been removed from the __init__.py file of the engine module. Updates to the validation mechanism in the LlamaCloudConfig class have also been made.

Changes

File Path Change Summary
.changeset/hungry-chairs-tie.md Introduced a patch to ensure the .env file is loaded during poetry run generate, allowing environment variables to be recognized.
templates/components/multiagent/python/app/api/routers/chat.py Updated import path for get_chat_engine from app.engine to app.engine.engine.
templates/types/streaming/fastapi/app/api/routers/chat.py Updated import path for get_chat_engine from app.engine to app.engine.engine.
templates/types/streaming/fastapi/app/engine/init.py Removed import statement for get_chat_engine, making it unavailable for import from the package's namespace.
templates/components/vectordbs/python/llamacloud/generate.py Reorganized import statements; moved load_dotenv() and added logging configuration.
templates/components/vectordbs/python/llamacloud/index.py Updated validation mechanism in LlamaCloudConfig using @field_validator, added from_env method, and modified get_client to utilize this method.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ChatRouter
    participant ChatEngine

    User->>ChatRouter: Send chat request
    ChatRouter->>ChatEngine: Call get_chat_engine()
    ChatEngine-->>ChatRouter: Return chat engine instance
    ChatRouter-->>User: Send response
Loading

🐰 "In the garden, changes bloom,
The .env now finds its room.
With paths restructured, we delight,
Chat engines ready, shining bright!
A hop, a skip, to code anew,
Together we create, just me and you!" 🌼

Possibly related PRs


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?

❤️ 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.
    • 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 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.

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

@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 and nitpick comments (1)
.changeset/hungry-chairs-tie.md (1)

1-5: LGTM! Consider enhancing the change description.

The changeset file is correctly formatted and accurately reflects the nature of the change as a patch. The description succinctly captures the essence of the fix.

For improved clarity, consider expanding the change description slightly:

-Fix .env not loaded on poetry run generate
+Fix: Ensure .env is loaded when running 'poetry run generate'

This minor adjustment provides a bit more context about the fix without being overly verbose.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7652b2b and 245c0f8.

📒 Files selected for processing (4)
  • .changeset/hungry-chairs-tie.md (1 hunks)
  • templates/components/multiagent/python/app/api/routers/chat.py (1 hunks)
  • templates/types/streaming/fastapi/app/api/routers/chat.py (1 hunks)
  • templates/types/streaming/fastapi/app/engine/init.py (0 hunks)
💤 Files with no reviewable changes (1)
  • templates/types/streaming/fastapi/app/engine/init.py
✅ Files skipped from review due to trivial changes (1)
  • templates/types/streaming/fastapi/app/api/routers/chat.py
🧰 Additional context used
📓 Path-based instructions (1)
templates/components/multiagent/python/app/api/routers/chat.py (1)

Pattern templates/**: For files under the templates folder, do not report 'Missing Dependencies Detected' errors.

🔇 Additional comments (1)
templates/components/multiagent/python/app/api/routers/chat.py (1)

8-8: LGTM! Verify the new import path.

The updated import statement reflects a restructuring of the module organization, which is good for code organization. The get_chat_engine function is now imported from a more specific location.

To ensure this change is consistent across the codebase, run the following script:

✅ Verification successful

Verified the import path update.

All instances of get_chat_engine now correctly import from app.engine.engine. No old import statements remain, ensuring consistent module organization.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the new import path for get_chat_engine

# Test: Search for any remaining old import statements
rg --type python 'from app.engine import get_chat_engine'

# Test: Confirm the new import statement is used consistently
rg --type python 'from app.engine.engine import get_chat_engine'

Length of output: 188


Script:

#!/bin/bash
# Description: Verify the new import path for get_chat_engine using file pattern matching

# Test: Search for any remaining old import statements using glob pattern
rg 'from app\.engine import get_chat_engine' --glob '*.py'

# Test: Confirm the new import statement is used consistently using glob pattern
rg 'from app\.engine\.engine import get_chat_engine' --glob '*.py'

Length of output: 342

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.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 83dacf1 and a48525c.

📒 Files selected for processing (1)
  • templates/components/vectordbs/python/llamacloud/index.py (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
templates/components/vectordbs/python/llamacloud/index.py (1)

Pattern templates/**: For files under the templates folder, do not report 'Missing Dependencies Detected' errors.

🔇 Additional comments (4)
templates/components/vectordbs/python/llamacloud/index.py (4)

10-10: Import of field_validator from Pydantic is appropriate

The addition of field_validator to the imports correctly aligns with the use of Pydantic's new validation mechanism in version 2.x.


51-60: Addition of from_env class method enhances configuration

The from_env class method provides a clear and centralized approach to initialize LlamaCloudConfig from environment variables, improving the configuration management and readability of the code.


64-67: Using default_factory with from_env for dynamic defaults

The use of default_factory=LlamaCloudConfig.from_env in the IndexConfig ensures that the llama_cloud_pipeline_config is initialized with environment variables by default. This approach enhances flexibility and aligns with Pydantic's best practices.


91-91: Update get_client to utilize from_env method

The get_client function now correctly uses LlamaCloudConfig.from_env() to load configuration from environment variables, ensuring consistency across the application.

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

🧹 Outside diff range and nitpick comments (1)
templates/components/vectordbs/python/llamacloud/generate.py (1)

23-58: New ensure_index function adds crucial setup logic.

The ensure_index function is a valuable addition:

  • It checks for existing pipelines and creates a new one if necessary, ensuring proper index setup.
  • The use of environment variables for API keys and model names is a good security practice.

However, consider the following suggestion for future flexibility:

The check for OpenAIEmbedding might be too restrictive if other embedding models are to be supported in the future. Consider making this check configurable or allowing for multiple embedding model types. For example:

supported_embedding_models = [OpenAIEmbedding]  # Add other models as needed
if not any(isinstance(Settings.embed_model, model) for model in supported_embedding_models):
    raise ValueError(
        "Creating a new pipeline with the current embedding model is not supported."
    )

This approach would make it easier to add support for additional embedding models in the future without changing the core logic.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a48525c and 2e46ac4.

📒 Files selected for processing (2)
  • .changeset/chilled-zebras-taste.md (1 hunks)
  • templates/components/vectordbs/python/llamacloud/generate.py (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .changeset/chilled-zebras-taste.md
🧰 Additional context used
📓 Path-based instructions (1)
templates/components/vectordbs/python/llamacloud/generate.py (1)

Pattern templates/**: For files under the templates folder, do not report 'Missing Dependencies Detected' errors.

🔇 Additional comments (4)
templates/components/vectordbs/python/llamacloud/generate.py (4)

2-14: Improved environment variable loading and import organization.

The changes in this section are well-structured and address the core issue of the PR:

  1. The load_dotenv() function is now called immediately after its import, ensuring that environment variables are loaded before any other imports that might depend on them.
  2. New imports (PipelineType, init_settings, Settings) have been added to support the new functionality.
  3. The get_index import has been moved below the load_dotenv() call, which is crucial for ensuring that any environment variables used in get_index are properly loaded.

These changes effectively resolve the issue of .env not being loaded on poetry run generate.


Line range hint 15-20: Appropriate logging configuration added.

The addition of logging configuration is a good practice:

  • Setting the log level to INFO will provide useful information during execution without being overly verbose.
  • Creating a logger instance allows for consistent logging throughout the file.

This change will improve the ability to debug and monitor the application's behavior.


59-65: Improved generate_datasource function with proper initialization.

The changes to the generate_datasource function enhance its reliability:

  1. The addition of init_settings() at the beginning ensures that all necessary settings are initialized before proceeding with the data source generation.
  2. The call to ensure_index(index) guarantees that the index is properly set up before use, which is crucial for the correct functioning of the LlamaCloud pipeline.

These modifications address the core issue of the PR by ensuring that the environment is correctly set up before processing the data source.


Line range hint 1-93: Summary: Effective resolution of .env loading issue and improved LlamaCloud integration.

This PR successfully addresses the issue of .env not being loaded on poetry run generate and enhances the LlamaCloud integration:

  1. Environment variables are now properly loaded before any dependent imports.
  2. The new ensure_index function guarantees proper setup of the LlamaCloud pipeline.
  3. The generate_datasource function now initializes settings and ensures index setup before processing.

These changes significantly improve the reliability and functionality of the application when working with LlamaCloud. The code is well-structured and follows good practices for environment variable handling and error checking.

Great job on addressing the core issue and enhancing the overall robustness of the code!

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