Skip to content

reface the interface of online models#1074

Merged
wzh1994 merged 3 commits intoLazyAGI:mainfrom
wzh1994:wzh/online
Mar 31, 2026
Merged

reface the interface of online models#1074
wzh1994 merged 3 commits intoLazyAGI:mainfrom
wzh1994:wzh/online

Conversation

@wzh1994
Copy link
Copy Markdown
Contributor

@wzh1994 wzh1994 commented Mar 31, 2026

📌 PR 内容 / PR Description

统一并规范 OnlineChatModuleOnlineEmbeddingModuleOnlineMultiModalModuleOnlineModuleAutoModel 五个在线模型入口的参数接口,抽象公共参数解析逻辑,消除历史命名不一致问题,并增强冲突检测与错误提示。

Unifies the parameter interface across all five online model entry points (OnlineChatModule, OnlineEmbeddingModule, OnlineMultiModalModule, OnlineModule, AutoModel), extracts a shared parameter resolver, eliminates historical naming inconsistencies, and strengthens conflict detection with clear error messages.


✅ 变更类型 / Type of Change

  • 重构 / Refactor (no functionality change, code structure optimized)
  • 修复 Bug / Bug fix (non-breaking change that fixes an issue)

🔍 核心变更 / Key Changes

1. 新增公共参数解析函数 resolve_online_paramsbase/utils.py

抽象出统一的参数解析器,供所有模块的 __new____init__forward 共用:

  • 别名归一化:自动将历史字段(embed_model_namemodel_nameembed_namebase_urlembed_url)映射到规范名(modelurl
  • 冲突检测:同时传入规范名与历史别名时,抛出明确的 ValueError
  • source-as-model 自动交换:若第一个位置参数是合法的 source 名(如 'openai'),自动与 source 互换;若 source 也是合法 source 名则报错;若 source 是模型名则直接交换
  • 灵活别名传参model_aliases / url_aliases 支持传 str(单别名)或 tuple(多别名)

2. 统一构造函数参数名

模块 变更前 变更后
OnlineEmbeddingModule 第一参数 sourceembed_model_nameembed_url 第一参数 modelurl(旧名保持 kw 兼容)
OnlineChatModule base_url urlbase_url 保持 kw 兼容)
OnlineMultiModalModule base_url urlbase_url 保持 kw 兼容)

3. 统一 forward 参数名

所有 forward 方法统一支持 urlmodel 作为显式参数,历史字段(base_urlembed_urlmodel_nameembed_model_nameembed_name)通过 resolve_online_params 自动兼容,同时检测冲突。

4. 修复错误类型与校验方式

  • OnlineChatModule / OnlineEmbeddingModuleskip_auth 无 url 时由 KeyError 改为 ValueError
  • OnlineMultiModalModule._resolve_type_name / _validate_parametersassert 改为 raise ValueError,错误信息更清晰
  • OnlineMultiModalBase.forward:去掉无意义的 try/except Exception 包装

5. 其他修复

  • dynamic_router._merge_dynamic_forward_kwargs:补充对 embed_urlembed_name 等历史别名的感知,避免重复注入
  • AutoModel:补充 model_name 别名支持,并增加 model-as-source 冲突检测
  • resolve_online_params 中使用 hasattr(__contains__) 而非 callable() 判断 source_registry 类型,修复 LazyDict 既是容器又是 Callable 导致被误当函数调用的 bug

⚡ 更新后的用法示例 / Usage After Update

# 构造:所有入口统一使用 model / source / url
OnlineChatModule('qwen-plus', source='qwen', url='http://...')
OnlineChatModule('openai')                     # model 是 source 名,自动交换
OnlineChatModule(base_url='http://...')        # 旧参数名仍可用(向前兼容)

OnlineEmbeddingModule('text-embedding-ada-002', source='openai')
OnlineEmbeddingModule('openai')                # 旧式位置传 source,自动交换
OnlineEmbeddingModule(embed_model_name='...')  # 旧参数名仍可用

# forward:统一使用 model / url 覆盖
module('input text', model='gpt-4', url='http://...')
module('input text', model_name='gpt-4')       # 旧参数名仍可用

# 冲突时报错清晰
OnlineChatModule('openai', source='qwen')
# ValueError: 'openai' is a recognised source name; pass it as source= and do not also set source='qwen'

OnlineEmbeddingModule(model='ada-002', embed_model_name='ada-003')
# ValueError: Conflicting parameters: `model` and `embed_model_name` are both provided. Use `model` only.

🔄 重构前 / 重构后对比 / Before & After

重构前 / Before:

# 接口不统一,各模块用不同参数名
OnlineChatModule(base_url='http://...')
OnlineEmbeddingModule(source='openai', embed_url='http://...', embed_model_name='ada-002')
OnlineMultiModalModule(base_url='http://...')

# forward 中别名处理分散在各自的方法体里
runtime_url = url or kw.pop('base_url', None)
runtime_model = model or kw.pop('model_name', None) or self._model_name

重构后 / After:

# 统一接口
OnlineChatModule(url='http://...')
OnlineEmbeddingModule(model='ada-002', source='openai', url='http://...')
OnlineMultiModalModule(url='http://...')

# forward 中统一调用 resolve_online_params
model, _, url, kw = resolve_online_params(model, None, url, kw,
                                          model_aliases='model_name', url_aliases='base_url')

⚠️ 注意事项 / Additional Notes

  • 完全向前兼容:base_urlembed_urlembed_model_namemodel_nameembed_name 等旧字段在构造和 forward 中均可继续使用
  • OnlineEmbeddingModule 第一个位置参数语义变化:旧版是 source,新版是 model;由于 source-as-model 自动交换机制,旧式 OnlineEmbeddingModule('openai') 行为不变

@wzh1994 wzh1994 requested a review from a team as a code owner March 31, 2026 07:51
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a centralized parameter resolution utility, resolve_online_params, to standardize the handling of model, source, and URL aliases across various online modules, including Chat, Embedding, and MultiModal. It also renames base_url to url for consistency and improves error reporting during type inference. The review feedback identifies a logic error in the parameter swapping mechanism within resolve_online_params that could interfere with dynamic routing. Additionally, it suggests including missing model aliases in the Chat and MultiModal modules to ensure consistent behavior across the library.

Comment on lines +54 to +57
if source is not None and _in(source):
raise ValueError(f'`{model!r}` and `{source!r}` are both recognised source names; '
f'provide exactly one via `source=`.')
source, model = model, source
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The logic for swapping model and source is incorrect when source is already provided but not present in the source_registry (e.g., when using source='dynamic'). Currently, if model is a recognized source name and source is set to something else (like 'dynamic'), the function will swap them because _in(source) will be false. This breaks dynamic routing by setting source to the model name and model to 'dynamic'. The swap should only occur if source is None. If both are provided and model is a recognized source, it should raise a ValueError to avoid ambiguity, consistent with the check added in automodel.py.

Suggested change
if source is not None and _in(source):
raise ValueError(f'`{model!r}` and `{source!r}` are both recognised source names; '
f'provide exactly one via `source=`.')
source, model = model, source
if source is not None:
raise ValueError(f'{model!r} is a recognised source name; pass it as source= and '
f'do not also set source={source!r}.')
source, model = model, None

Comment on lines +39 to +40
model, source, url, kwargs = resolve_online_params(
model, source, url, kwargs, url_aliases='base_url', source_registry=lazyllm.online.chat)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

For consistency with AutoModel and other online modules, OnlineChatModule should also resolve model_name and base_model aliases into the model parameter. Currently, these remain in kwargs, which might lead to unexpected behavior or duplicate parameters being passed to the underlying supplier.

        model, source, url, kwargs = resolve_online_params(
            model, source, url, kwargs, model_aliases=('model_name', 'base_model'),
            url_aliases='base_url', source_registry=lazyllm.online.chat)

Comment on lines +81 to +82
model, source, url, kwargs = resolve_online_params(
model, source, url, kwargs, url_aliases='base_url')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Similar to the OnlineChatModule, OnlineMultiModalModule is missing common model aliases like model_name and base_model. Additionally, there is a redundant call to resolve_online_params inside _validate_parameters (line 60). While the second call is needed to check the source_registry (which depends on the resolved type), the first call in new could be optimized or combined to avoid multiple passes over kwargs.

Suggested change
model, source, url, kwargs = resolve_online_params(
model, source, url, kwargs, url_aliases='base_url')
model, source, url, kwargs = resolve_online_params(
model, source, url, kwargs, model_aliases=('model_name', 'base_model'), url_aliases='base_url')

@wzh1994 wzh1994 requested a review from a team as a code owner March 31, 2026 08:34
@wzh1994 wzh1994 merged commit 5b30224 into LazyAGI:main Mar 31, 2026
19 of 20 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