-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Conversation
--bug=1054256 --user=王孝刚 【模型】添加硅基流动的重排序模型失败 https://www.tapd.cn/57709429/s/1679612
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. |
[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 |
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: |
There was a problem hiding this comment.
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:
-
Code Duplication: The
__init__
method is defined twice with different parameters (one usesmodel
, the other usesapi_key
). -
Error Handling for Missing API Base: The error handling in the
__init__
method assumes that the missing API base would always cause a value ofNone
. This might not work correctly if there are other ways to determine what the missing parameter means. -
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. -
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
tomodel_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!
fix: siliconCloud rerank error --bug=1054256 --user=王孝刚 【模型】添加硅基流动的重排序模型失败 https://www.tapd.cn/57709429/s/1679612