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

Update provider parameters, check for valid provider #2594

Merged
merged 2 commits into from
Jan 24, 2025

Conversation

hlohaus
Copy link
Collaborator

@hlohaus hlohaus commented Jan 24, 2025

Fix reading model list in GeminiPro
Fix check content-type in OpenaiAPI

Fix reading model list in GeminiPro
Fix  check content-type in OpenaiAPI
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Pull Request Review: Update provider parameters, check for valid provider

Author: H Lohaus

Summary

This pull request includes the following updates:

  • Fixes the reading of the model list in GeminiPro.
  • Fixes the content-type check in OpenaiAPI.

Review

  1. GeminiPro Changes:

    • Adding supports_system_message attribute is a positive enhancement for better functionality.
    • The change in get_models method now handles the api_base parameter correctly and improves error handling by returning fallback models on exceptions.
  2. OpenaiAPI Changes:

    • Improving content-type validation by using startswith to allow for more flexibility in content type checking.
    • The refactoring of the content type checks to simplify and consolidate the logic is beneficial for readability and maintainability.
  3. JS and API Improvements:

    • The update to the conversation parameter check adds more robustness to the code.
    • The fix in get_providers method ensures proper handling of the image attribute.
    • Removal of redundant web_search parameter in decorated_log improves clarity.
  4. Base Provider Changes:

    • Adding parameters such as safe, enhance, and private improves the functionality and usability of the provider.
    • Including stop and tools in BASIC_PARAMETERS ensures that these settings are available by default.

Suggestions

  • Ensure to update corresponding tests to reflect these changes, especially for the get_models method in GeminiPro and the content-type checks in OpenaiAPI.
  • Consider adding more detailed logging to capture the reasons for falling back to fallback_models in GeminiPro.

Thank you, H Lohaus, for your valuable contributions to the project. These improvements will significantly enhance the functionality and maintainability of the codebase.

Best regards,
g4f Copilot

g4f/Provider/needs_auth/GeminiPro.py Show resolved Hide resolved
@@ -50,7 +52,7 @@
cls.models.sort()

Choose a reason for hiding this comment

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

Sorting the models in place can have side effects if the list is used elsewhere. It may be better to return a sorted copy using sorted(cls.models).

g4f/Provider/needs_auth/OpenaiAPI.py Show resolved Hide resolved
@@ -108,7 +108,8 @@
if api_endpoint is None:
api_endpoint = f"{api_base.rstrip('/')}/chat/completions"
async with session.post(api_endpoint, json=data) as response:
if response.headers.get("content-type", None if stream else "application/json") == "application/json":
content_type = response.headers.get("content-type", "text/event-stream" if stream else "application/json")
if content_type.startswith("application/json"):
data = await response.json()
cls.raise_error(data)
await raise_for_status(response)

Choose a reason for hiding this comment

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

Ensure that raise_for_status is called after handling the JSON response and errors to avoid potential misinterpretation of response status.

g4f/Provider/needs_auth/OpenaiAPI.py Show resolved Hide resolved
g4f/gui/client/static/js/chat.v1.js Show resolved Hide resolved
g4f/gui/server/api.py Show resolved Hide resolved
@@ -62,7 +62,7 @@
"name": provider.__name__,
"label": provider.label if hasattr(provider, "label") else provider.__name__,
"parent": getattr(provider, "parent", None),
"image": getattr(provider, "image_models", None) is not None,
"image": bool(getattr(provider, "image_models", False)),
"vision": getattr(provider, "default_vision_model", None) is not None,

Choose a reason for hiding this comment

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

Similar to the previous line, you might want to use getattr(provider, "default_vision_model", None) directly instead of checking if it is not None. This can simplify the expression.

@TheFirstNoob
Copy link

@hlohaus Hi. A bit off topic, but I'll write it for your convenience. In order not to forget to update the link to the release version every time, you can use this one: https://github.com/xtekky/gpt4free/releases/latest
When a new version is released, it is usually always marked as the latest. This is a more universal solution.

Copy link

Pull Request Review by g4f Copilot

Thank you, @hlohaus, for your contribution to the project! This pull request contains several improvements across multiple files, tackling both functionality correctness and maintainability. Here's my detailed review:

Highlights from the PR

  1. GeminiPro Provider Updates:

    • Added the supports_system_message attribute to enhance capabilities.
    • Improved the get_models method by refactoring the API call and providing better error handling by returning fallback models directly when exceptions are encountered. This ensures robustness in case of failures.
  2. OpenaiAPI Provider Enhancements:

    • Improved the content-type validation logic by introducing a more flexible content_type.startswith check, making it compatible with additional nuances in the response's content-type header.
    • Enhanced error handling for unsupported content-types with a clearer error message.
  3. General Improvements to Core Modules:

    • Replaced the now-deprecated md5_hexdigest with the standard hashlib.md5, aligning with best practices and ensuring future compatibility.
    • Optimized and clarified boolean checks and fallback logic in various files (e.g., conversation parameter in JavaScript, get_providers in api.py).
  4. Parameter Additions in base_provider.py:

    • Added new parameters (safe, enhance, private, etc.), along with defaults such as stop and tools, to increase provider configurability.

Suggestions for Improvement

  • Docstrings and Comments:

    • While the code changes are clear, adding docstrings or comments in some places (like the new parameters in base_provider.py) would make the enhancements easier to understand for future contributors.
  • Testing Coverage:

    • Ensure the changes (especially in API error handling and new parameters) are covered with appropriate test cases to verify their correctness.
  • Changelog Update:

    • Include these changes in the project's changelog (if available) to keep the documentation up-to-date and inform all stakeholders about the new improvements.

Overall Feedback

This pull request makes great strides in improving the robustness of provider operations, code clarity, and configurability across several modules. The attention to detail in both API interaction and parameter handling demonstrates a strong focus on stability and usability.

Once the suggestions above are addressed, this should be good to merge!

Thanks again for your hard work on this substantial and impactful update!

Best regards,
g4f Copilot

@hlohaus
Copy link
Collaborator Author

hlohaus commented Jan 24, 2025

@hlohaus Hi. A bit off topic, but I'll write it for your convenience. In order not to forget to update the link to the release version every time, you can use this one: https://github.com/xtekky/gpt4free/releases/latest When a new version is released, it is usually always marked as the latest. This is a more universal solution.

@TheFirstNoob
I'm not currently creating a Windows package with every version update. 😉

@hlohaus hlohaus merged commit e634f82 into xtekky:main Jan 24, 2025
1 check 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.

2 participants