Skip to content
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

feat(constants): Add utility functions for config access #1631

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Jacques-Murray
Copy link

This PR adds utility functions to better encapsulate configuration access in constants.py:

  • Add get_env_vars() to access provider environment variables
  • Add get_providers() to retrieve supported providers list
  • Add get_models() to access provider model configurations

These functions improve code maintainability by:

  • Providing a clean interface to access configuration data
  • Adding type hints and documentation for better code clarity
  • Enabling filtered access to specific provider configurations

- Add get_env_vars() to access provider environment variables
- Add get_providers() to retrieve supported providers list
- Add get_models() to access provider model configurations

These functions improve code maintainability by encapsulating configuration access.
@joaomdmoura
Copy link
Collaborator

joaomdmoura commented Nov 20, 2024

This is a review from a crew. I'm testing a crew for PRs, so if it sounds weird that is the reason, you are one of the first ones to see this. :) I'll try to personally look into it as well

Specific Code Improvements

  1. Add Type Hints: Introducing Python type hints will improve code clarity and maintainability. For instance:

    from typing import Dict, List, Optional
    
    def get_env_vars(provider: Optional[str] = None) -> Dict[str, List[str]]:
  2. Enhance Input Validation: Ensure that the provider parameter is validated to prevent erroneous inputs. Recommended changes:

    def get_env_vars(provider: Optional[str] = None) -> Dict[str, List[str]]:
        if provider is not None and not isinstance(provider, str):
            raise TypeError("Provider must be a string or None")
        if provider and provider not in ENV_VARS:
            raise ValueError(f"Unknown provider: {provider}")
        return ENV_VARS[provider] if provider else ENV_VARS
  3. Normalize Return Types: The get_models() function should return a consistent type regardless of the input:

    def get_models(provider: Optional[str] = None) -> Dict[str, List[str]]:
        if provider and provider in MODELS:
            return {provider: MODELS[provider]}
        return MODELS
  4. Organize Code Structure: Consider grouping related constants with the utility functions to enhance readability:

    # Provider-related constants and utility functions here
  5. Add Unit Tests: It is essential to cover the new functions with unit tests to ensure they function as expected. Example tests might include:

    def test_get_env_vars():
        assert get_env_vars() is not None
        assert isinstance(get_env_vars("openai"), list)
        with pytest.raises(ValueError):
            get_env_vars("invalid_provider")

Historical Context from Related PRs

This PR builds upon prior discussions in related pull requests, particularly the following:

These pull requests collectively indicate a strong trend towards improving user experience and security in CLI interactions, emphasizing the necessity of sound configuration management.

Implications for Related Files

Adding these utility functions enhances the encapsulation of configuration management, reducing direct access to raw data structures. It will allow changes in the underlying configuration constants without impacting other parts of the code, provided that the interface remains consistent. This approach not only improves maintainability but also encourages collaborative development by fostering a better understanding of data flow.

In conclusion, while the current implementation is a solid foundation, implementing the suggested improvements will align the code with best practices and ensure a more robust, maintainable system moving forward.

- Add type hints for Dict, List, and Optional types
- Enhance get_env_vars with better error handling for invalid inputs
- Add unit tests for get_env_vars function
- Fix get_models return type consistency
@bhancockio
Copy link
Collaborator

Hey @Jacques-Murray! I like where you are going with this.

However, where in the code should we call these updated functions?

We try not to add code to the library that doesn't get called by anything.

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.

4 participants