Skip to content

Make that vertexai and gemini translate to google for api keys file #323

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
Feb 26, 2025

Conversation

juandavidcruzgomez
Copy link
Contributor

@juandavidcruzgomez juandavidcruzgomez commented Feb 26, 2025

  • Map providers
  • Map providers
  • Add gemini to the mapping

Summary by CodeRabbit

  • New Features

    • Introduced a standardized mechanism to process and map API provider names, ensuring smoother automatic configuration adjustments.
  • Tests

    • Added unit tests to validate the new provider mapping functionality, enhancing overall integration stability.

Copy link

coderabbitai bot commented Feb 26, 2025

Walkthrough

This update introduces a static method map_provider in the StdLLMEngine class to standardize provider names using a regex-based dictionary lookup. The completion method now leverages this mapping function, including special handling for the "google" provider by setting an environment variable when applicable. Additionally, enhancements have been made to type annotations, and new fixtures are added in the test suite to facilitate provider mapping testing, along with a corresponding unit test.

Changes

File(s) Change Summary
edenai_apis/llmengine/llm_engine.py Added PROVIDER_MAPPING dictionary and a new static method map_provider in StdLLMEngine; updated completion to use this method with additional logic for "google".
edenai_apis/llmengine/tests/conftest.py Expanded type annotations and introduced the mapping_providers fixture to return structured provider mapping data.
edenai_apis/llmengine/tests/test_llm_engine.py Added a new unit test method test_map_provider in TestLLMEngine to validate the mapping logic, including the proper use of StdLLMEngine.map_provider.

Sequence Diagram(s)

sequenceDiagram
    participant U as User Request
    participant CE as Completion Method
    participant MP as map_provider
    participant GS as Google Credential Setup

    U->>CE: Call completion(provider)
    CE->>MP: Call map_provider(provider)
    MP-->>CE: Return mapped_provider
    alt mapped_provider == "google"
        CE->>GS: Retrieve API settings and set GOOGLE_APPLICATION_CREDENTIALS
    end
    CE-->>U: Return completion result
Loading

Poem

I'm a smart rabbit with hops so fleet,
Mapping providers quick, never missing a beat.
With regex magic and fixtures so neat,
CodeRabbit's changes make our code complete.
🐇💻 Hooray for improvements – a joyful treat!

Tip

CodeRabbit's docstrings feature is now available as part of our Pro Plan! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


📜 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 c4f8acf and 3ff6113.

📒 Files selected for processing (1)
  • edenai_apis/llmengine/llm_engine.py (5 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
edenai_apis/llmengine/llm_engine.py

756-756: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (2)
edenai_apis/llmengine/llm_engine.py (2)

735-735: Update provider mapping to support pattern matching.

The mapping approach looks good, but the current implementation might be too restrictive for some provider names. Based on the PR objectives, we need to ensure both "vertexai" and "gemini" translate to "google" for API keys.

Consider updating the mapping dictionary to support pattern variations:

-    PROVIDER_MAPPING = {"vertex_ai": "google", "gemini": "google"}
+    PROVIDER_MAPPING = {"vertex_ai.*": "google", "gemini": "google"}

803-826: Improved provider mapping logic looks good.

The implementation appropriately handles different cases for provider mapping:

  1. Special handling for Google's VertexAI (setting environment variable)
  2. Special handling for Gemini (using genai_api_key)
  3. Default handling for other providers

This effectively achieves the PR objective of making both vertexai and gemini translate to google while maintaining their distinct API key requirements.

✨ Finishing Touches
  • 📝 Generate Docstrings

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

@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: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ce4bbc4 and c4f8acf.

📒 Files selected for processing (3)
  • edenai_apis/llmengine/llm_engine.py (4 hunks)
  • edenai_apis/llmengine/tests/conftest.py (4 hunks)
  • edenai_apis/llmengine/tests/test_llm_engine.py (2 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
edenai_apis/llmengine/llm_engine.py

755-755: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (5)
edenai_apis/llmengine/tests/test_llm_engine.py (1)

227-230: Test implementation looks good.

The unit test for the new map_provider method is well structured and uses the fixture data effectively to verify mapping behavior across different provider names.

edenai_apis/llmengine/tests/conftest.py (2)

1-1: Type annotations correctly updated.

The import statement is properly extended to include the required types for the new fixture.


170-196: Comprehensive test data for provider mapping.

The test data covers a wide range of provider mappings, including edge cases like empty strings and None values.

edenai_apis/llmengine/llm_engine.py (2)

65-65: Import addition looks good.

The re module is required for the regex matching in the new map_provider method.


802-817: The Google credentials handling looks good.

Setting the GOOGLE_APPLICATION_CREDENTIALS environment variable for Google provider is correct and aligned with the PR objectives to make vertexai and gemini translate to Google.

@@ -730,6 +731,8 @@ def image_generation(

class StdLLMEngine(LLMEngine):

PROVIDER_MAPPING = {"vertex_ai": "google", "gemini": "google"}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Provider mapping dictionary is inconsistent with test data.

The PROVIDER_MAPPING dictionary only includes "vertex_ai" and "gemini" keys, but the test fixture includes more specific patterns like "vertex_ai-text-models". This mismatch could lead to test failures.

Consider updating the mapping dictionary to support all patterns:

-    PROVIDER_MAPPING = {"vertex_ai": "google", "gemini": "google"}
+    PROVIDER_MAPPING = {"vertex_ai.*": "google", "gemini": "google"}
📝 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
PROVIDER_MAPPING = {"vertex_ai": "google", "gemini": "google"}
PROVIDER_MAPPING = {"vertex_ai.*": "google", "gemini": "google"}

Comment on lines +750 to +758
@staticmethod
def map_provider(provider_name: str) -> str:
if provider_name is None:
return None
# Try to regex match the keys of PROVIDER_MAPPING and provider_name. The first one ot match wins
for key in StdLLMEngine.PROVIDER_MAPPING.keys():
if re.match(key, provider_name, re.RegexFlag.IGNORECASE):
return StdLLMEngine.PROVIDER_MAPPING[key]
return provider_name
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix the regex pattern matching implementation.

The current implementation has two issues:

  1. It iterates through keys using .keys() which is unnecessary
  2. The regex pattern needs to match the full string, not just a part of it
    @staticmethod
    def map_provider(provider_name: str) -> str:
        if provider_name is None:
            return None
-        # Try to regex match the keys of PROVIDER_MAPPING and provider_name. The first one ot match wins
-        for key in StdLLMEngine.PROVIDER_MAPPING.keys():
-            if re.match(key, provider_name, re.RegexFlag.IGNORECASE):
+        # Try to regex match the keys of PROVIDER_MAPPING and provider_name. The first one to match wins
+        for key in StdLLMEngine.PROVIDER_MAPPING:
+            if re.fullmatch(f"{key}.*", provider_name, re.IGNORECASE):
                return StdLLMEngine.PROVIDER_MAPPING[key]
        return provider_name
📝 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
@staticmethod
def map_provider(provider_name: str) -> str:
if provider_name is None:
return None
# Try to regex match the keys of PROVIDER_MAPPING and provider_name. The first one ot match wins
for key in StdLLMEngine.PROVIDER_MAPPING.keys():
if re.match(key, provider_name, re.RegexFlag.IGNORECASE):
return StdLLMEngine.PROVIDER_MAPPING[key]
return provider_name
@staticmethod
def map_provider(provider_name: str) -> str:
if provider_name is None:
return None
# Try to regex match the keys of PROVIDER_MAPPING and provider_name. The first one to match wins
for key in StdLLMEngine.PROVIDER_MAPPING:
if re.fullmatch(f"{key}.*", provider_name, re.IGNORECASE):
return StdLLMEngine.PROVIDER_MAPPING[key]
return provider_name
🧰 Tools
🪛 Ruff (0.8.2)

755-755: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

@juandavidcruzgomez juandavidcruzgomez merged commit f505542 into master Feb 26, 2025
1 of 2 checks passed
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.

1 participant