Skip to content

fix: siliconCloud rerank error #2774

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 1 commit into from
Apr 2, 2025
Merged

fix: siliconCloud rerank error #2774

merged 1 commit into from
Apr 2, 2025

Conversation

shaohuzhang1
Copy link
Contributor

fix: siliconCloud rerank error --bug=1054256 --user=王孝刚 【模型】添加硅基流动的重排序模型失败 https://www.tapd.cn/57709429/s/1679612

--bug=1054256 --user=王孝刚 【模型】添加硅基流动的重排序模型失败 https://www.tapd.cn/57709429/s/1679612
Copy link

f2c-ci-robot bot commented Apr 2, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

f2c-ci-robot bot commented Apr 2, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@wxg0103 wxg0103 merged commit 44c1d35 into main Apr 2, 2025
4 checks passed
@wxg0103 wxg0103 deleted the pr@main@fix_1054256 branch April 2, 2025 07:17
self.model = model
self.api_key = api_key
self.top_n = top_n

def compress_documents(self, documents: Sequence[Document], query: str, callbacks: Optional[Callbacks] = None) -> \
Sequence[Document]:
if not documents:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code you provided has a few areas that need addressing:

  1. Code Duplication: The __init__ method is defined twice with different parameters (one uses model, the other uses api_key).

  2. Error Handling for Missing API Base: The error handling in the __init__ method assumes that the missing API base would always cause a value of None. This might not work correctly if there are other ways to determine what the missing parameter means.

  3. Variable Naming Consistency: There seems to be inconsistency between using snake_case (self.model) and camelCase (self.Model in another part) for variable names. It's generally recommended to use consistent naming conventions throughout the codebase.

  4. Type Hinting Issues: While type hints exist, they are mostly limited to function parameters. The types used could be more specific based on expected usage.

Here’s an optimized version of the code with some improvements:

from typing import Any, Dict, List, Optional, Sequence

class YourClass:
    MAX_TOP_N = 99  # Example maximum value for top_n

    def __new__(cls, model_type=None, model_name='default_model',
                 model_credential: Dict[str, object] = {}, **kwargs):
        return cls.new_instance(
            model_type=model_type, model_name=model_name,
            model_credential=model_credential, **kwargs
        )

    @classmethod
    def new_instance(cls, model_type, model_name, model_credential: Dict[str, object], **kwargs) -> 'YourClass':
        top_n = kwargs.setdefault('top_n', cls.MAX_TOP_N)

        instance = super().__new__(cls)
        instance.init(model_type, model_name, model_credential, top_n=top_n)
        return instance

    def init(self, api_base: Optional[str] = None, model: Optional[str] = None, top_n: int = 3,
             api_key: Optional[str] = None):
        if not api_base:
            raise ValueError("Please provide server URL")
        
        if not model:
            raise ValueError("Please provide the model name")

        if not api_key:
            raise ValueError("Please provide the API key")

        self.api_base = api_base
        self.model = model
        self.api_key = api_key
        self.top_n = min(top_n, self.MAX_TOP_N)

    def compress_documents(self, documents: Sequence[Document], query: str, callbacks: Optional['Callbacks'] = None) -> Sequence[Document]:
        """Compresses a list of documents."""
        if not documents:
            return []

        # Placeholder for document compression logic
        compressed_docs = []  # Replace this with actual implementation
        for doc in documents:
            # Simulate compression process
            compressed_docs.append(doc.compress(query))
        return compressed_docs[:]

Key Changes:

  • New Instance Method: Refactored the initialization into a separate classmethod called new_instance.
  • Consistent Name Usage: Changed ModelType to model_type for consistency.
  • Default Values: Added default values for TopN which helps reduce duplication.
  • Typing: Made max_top_n an explicit property for better readability and type hinting.

This should make the code cleaner and easier to maintain while adhering to best practices for Python coding standards. Let me know if you have any further questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants