Skip to content

Commit 5b30224

Browse files
authored
reface the interface of online models (#1074)
1 parent 1fffac5 commit 5b30224

File tree

11 files changed

+136
-74
lines changed

11 files changed

+136
-74
lines changed

lazyllm/docs/module.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@
14581458
source (Optional[str]): 使用的服务提供方。为在线模块(``OnlineModule``)指定 ``qwen`` / ``glm`` / ``openai`` 等;若设为 ``local`` 则强制创建本地 TrainableModule。
14591459
type (Optional[str]): 模型类型。若未指定会尝试从 kwargs 中获取或由在线模块自动推断。
14601460
config (Union[str, bool]): 是否启用 ``auto_model_config_map`` 的覆盖逻辑,或者用户指定的 config 文件路径。默认为 True。
1461-
**kwargs: 兼容 `model` 的同义字段 `base_model` 和 `embed_model_name`,不接收其他用户传入的字段
1461+
**kwargs: 仅接受 `model` 的同义字段 `base_model`、`embed_model_name` 和 `model_name`,不接收其他用户自定义字段。其他模型参数(如 ``stream``、``type``、``url`` 等)应在配置文件(``auto_model_config_map``)中指定,由 ``config_id`` 引用后自动注入
14621462
''')
14631463

14641464
add_english_doc('AutoModel', '''\
@@ -1472,7 +1472,7 @@
14721472
source (Optional[str]): Provider for online modules (``qwen`` / ``glm`` / ``openai``). Set to ``local`` to force a local TrainableModule.
14731473
type (Optional[str]): Model type. If omitted, it will try to fetch from kwargs or be inferred by the online module.
14741474
config (Union[str, bool]): Whether to enable overrides from ``auto_model_config_map``, or a user-specified config file path. Defaults to True.
1475-
**kwargs: Accepts `base_model` and `embed_model_name` as synonyms for `model`; does not accept other user-provided fields.
1475+
**kwargs: Only the synonyms `base_model`, `embed_model_name` and `model_name` for `model` are accepted; no other user-supplied fields are allowed. Other model parameters (e.g. ``stream``, ``type``, ``url``) should be specified in the configuration file (``auto_model_config_map``) and referenced via ``config_id`` so they are injected automatically.
14761476
''')
14771477

14781478
add_chinese_doc('OnlineModule', '''\
@@ -1525,8 +1525,8 @@
15251525
15261526
Args:
15271527
model (str): 指定要访问的模型 (注意使用豆包时需用 Model ID 或 Endpoint ID,获取方式详见 [获取推理接入点](https://www.volcengine.com/docs/82379/1099522)。使用模型前,要先在豆包平台开通对应服务。),默认为 ``gpt-3.5-turbo(openai)`` / ``SenseChat-5(sensenova)`` / ``glm-4(glm)`` / ``moonshot-v1-8k(kimi)`` / ``qwen-plus(qwen)`` / ``mistral-7b-instruct-v0.2(doubao)`` / ``deepseek/deepseek-v3.2(ppio)``
1528-
source (str): 指定要创建的模块类型,可选为 ``openai`` / ``sensenova`` / ``glm`` / ``kimi`` / ``qwen`` / ``doubao`` / ``ppio`` / ``deepseek(暂时不支持访问)``
1529-
base_url (str): 指定要访问的平台的基础链接,默认是官方链接
1528+
source (str): 指定要创建的模块类型,可选为 ``openai`` / ``sensenova`` / ``glm`` / ``kimi`` / ``qwen`` / ``doubao`` / ``ppio`` / ``deepseek(暂时不支持访问)``。也可以直接将 source 名称作为 ``model`` 传入,系统会自动识别并交换两者。
1529+
url (str): 指定要访问的平台的基础链接,默认是官方链接。也可使用别名 ``base_url`` 传入。
15301530
system_prompt (str): 指定请求的system prompt,默认是官方给的system prompt
15311531
api_key (str): 可显式传入 API Key;当设置为 ``auto`` 或 ``dynamic`` 时,将在运行时从配置读取,支持动态切换 key
15321532
stream (bool): 是否流式请求和输出,默认为流式
@@ -1538,9 +1538,9 @@
15381538
Used to manage and create access modules for large model platforms currently available on the market. Currently, it supports openai, sensenova, glm, kimi, qwen, doubao, ppio and deepseek (since the platform does not allow recharges for the time being, access is not supported for the time being). For how to obtain the platform's API key, please visit [Getting Started](/#platform)
15391539
15401540
Args:
1541-
model (str): Specify the model to access (Note that you need to use Model ID or Endpoint ID when using Doubao. For details on how to obtain it, see [Getting the Inference Access Point](https://www.volcengine.com/docs/82379/1099522). Before using the model, you must first activate the corresponding service on the Doubao platform.), default is ``gpt-3.5-turbo(openai)`` / ``SenseChat-5(sensenova)`` / ``glm-4(glm)`` / ``moonshot-v1-8k(kimi)`` / ``qwen-plus(qwen)`` / ``mistral-7b-instruct-v0.2(doubao)`` / ``deepseek/deepseek-v3.2(ppio)`` .
1541+
model (str): Specify the model to access (Note that you need to use Model ID or Endpoint ID when using Doubao. For details on how to obtain it, see [Getting the Inference Access Point](https://www.volcengine.com/docs/82379/1099522). Before using the model, you must first activate the corresponding service on the Doubao platform.), default is ``gpt-3.5-turbo(openai)`` / ``SenseChat-5(sensenova)`` / ``glm-4(glm)`` / ``moonshot-v1-8k(kimi)`` / ``qwen-plus(qwen)`` / ``mistral-7b-instruct-v0.2(doubao)`` / ``deepseek/deepseek-v3.2(ppio)`` . A recognised source name can also be passed here; it will be automatically swapped into ``source``.
15421542
source (str): Specify the type of module to create. Options include ``openai`` / ``sensenova`` / ``glm`` / ``kimi`` / ``qwen`` / ``doubao`` / ``ppio`` / ``deepseek (not yet supported)`` .
1543-
base_url (str): Specify the base link of the platform to be accessed. The default is the official link.
1543+
url (str): Specify the base link of the platform to be accessed. The default is the official link. The alias ``base_url`` is also accepted.
15441544
system_prompt (str): Specify the requested system prompt. The default is the official system prompt.
15451545
api_key (str): You can pass an explicit API key. If set to ``auto`` or ``dynamic``, the key is resolved from config at runtime, enabling dynamic key switching.
15461546
stream (bool): Whether to request and output in streaming mode, default is streaming.
@@ -1677,22 +1677,28 @@
16771677
用来管理创建目前市面上的在线Embedding服务模块,目前支持openai、sensenova、glm、qwen、doubao
16781678
16791679
Args:
1680+
model (str): 指定要访问的模型 (注意使用豆包时需用 Model ID 或 Endpoint ID,获取方式详见 [获取推理接入点](https://www.volcengine.com/docs/82379/1099522)。使用模型前,要先在豆包平台开通对应服务。),默认为 ``text-embedding-ada-002(openai)`` / ``nova-embedding-stable(sensenova)`` / ``embedding-2(glm)`` / ``text-embedding-v1(qwen)`` / ``doubao-embedding-text-240715(doubao)``。也可使用别名 ``embed_model_name`` 或 ``model_name`` 传入。也可将 source 名称直接作为 ``model`` 传入,系统会自动识别并交换两者。
16801681
source (str): 指定要创建的模块类型,可选为 ``openai`` / ``sensenova`` / ``glm`` / ``qwen`` / ``doubao``
1681-
embed_url (str): 指定要访问的平台的基础链接,默认是官方链接
1682-
embed_mode_name (str): 指定要访问的模型 (注意使用豆包时需用 Model ID 或 Endpoint ID,获取方式详见 [获取推理接入点](https://www.volcengine.com/docs/82379/1099522)。使用模型前,要先在豆包平台开通对应服务。),默认为 ``text-embedding-ada-002(openai)`` / ``nova-embedding-stable(sensenova)`` / ``embedding-2(glm)`` / ``text-embedding-v1(qwen)`` / ``doubao-embedding-text-240715(doubao)``
1682+
url (str): 指定要访问的平台的基础链接,默认是官方链接。也可使用别名 ``embed_url`` 或 ``base_url`` 传入。
1683+
type (str): 模型服务类型,可选 ``embed`` / ``rerank``,默认根据模型名自动推断。
16831684
api_key (str): 可显式传入 API Key;当设置为 ``auto`` 或 ``dynamic`` 时,将在运行时从配置读取,支持动态切换 key
16841685
dynamic_auth (bool): 是否启用动态鉴权;为 True 时等价于 ``api_key='dynamic'``
1686+
return_trace (bool): 是否将结果记录在trace中,默认为False
1687+
batch_size (int): 批量请求时每批的大小,默认为32
16851688
''')
16861689

16871690
add_english_doc('OnlineEmbeddingModule', '''\
16881691
Used to manage and create online Embedding service modules currently on the market, currently supporting openai, sensenova, glm, qwen, doubao.
16891692
16901693
Args:
1694+
model (str): Specify the model to access (Note that you need to use Model ID or Endpoint ID when using Doubao. For details on how to obtain it, see [Getting the Inference Access Point](https://www.volcengine.com/docs/82379/1099522). Before using the model, you must first activate the corresponding service on the Doubao platform.), default is ``text-embedding-ada-002(openai)`` / ``nova-embedding-stable(sensenova)`` / ``embedding-2(glm)`` / ``text-embedding-v1(qwen)`` / ``doubao-embedding-text-240715(doubao)``. The aliases ``embed_model_name`` and ``model_name`` are also accepted. A recognised source name can be passed here too; it will be automatically swapped into ``source``.
16911695
source (str): Specify the type of module to create. Options are ``openai`` / ``sensenova`` / ``glm`` / ``qwen`` / ``doubao``.
1692-
embed_url (str): Specify the base link of the platform to be accessed. The default is the official link.
1693-
embed_mode_name (str): Specify the model to access (Note that you need to use Model ID or Endpoint ID when using Doubao. For details on how to obtain it, see [Getting the Inference Access Point](https://www.volcengine.com/docs/82379/1099522). Before using the model, you must first activate the corresponding service on the Doubao platform.), default is ``text-embedding-ada-002(openai)`` / ``nova-embedding-stable(sensenova)`` / ``embedding-2(glm)`` / ``text-embedding-v1(qwen)`` / ``doubao-embedding-text-240715(doubao)``
1696+
url (str): Specify the base link of the platform to be accessed. The default is the official link. The aliases ``embed_url`` and ``base_url`` are also accepted.
1697+
type (str): Service type, either ``embed`` or ``rerank``. Inferred from the model name when omitted.
16941698
api_key (str): You can pass an explicit API key. If set to ``auto`` or ``dynamic``, the key is resolved from config at runtime, enabling dynamic key switching.
16951699
dynamic_auth (bool): Whether to enable dynamic auth. When True, it is equivalent to ``api_key='dynamic'``.
1700+
return_trace (bool): Whether to record the results in trace. Defaults to False.
1701+
batch_size (int): Batch size for bulk requests. Defaults to 32.
16961702
''')
16971703

16981704
add_example('OnlineEmbeddingModule', '''\
@@ -1701,6 +1707,8 @@
17011707
>>> emb = m("hello world")
17021708
>>> print(f"emb: {emb}")
17031709
emb: [0.0010528564, 0.0063285828, 0.0049476624, -0.012008667, ..., -0.009124756, 0.0032043457, -0.051696777]
1710+
>>> m2 = lazyllm.OnlineEmbeddingModule("sensenova")
1711+
>>> emb2 = m2("hello world")
17041712
''')
17051713

17061714
add_chinese_doc('OnlineMultiModalModule', '''\
@@ -1710,7 +1718,7 @@
17101718
model (str): 指定要访问的模型名称。
17111719
source (str): 指定要创建的模块类型,如 ``qwen`` / ``glm`` / ``minimax`` / ``siliconflow`` / ``doubao`` 等。
17121720
type (str): 多模态任务类型,可选 ``stt`` / ``tts`` / ``text2image`` / ``image_editing``。
1713-
base_url (str): 指定要访问的平台基础链接,默认使用各平台官方链接。
1721+
url (str): 指定要访问的平台基础链接,默认使用各平台官方链接。也可使用别名 ``base_url`` 传入
17141722
api_key (str): 可显式传入 API Key;当设置为 ``auto`` 或 ``dynamic`` 时,将在运行时从配置读取,支持动态切换 key。
17151723
dynamic_auth (bool): 是否启用动态鉴权;为 True 时等价于 ``api_key='dynamic'``。
17161724
return_trace (bool): 是否将结果记录在 trace 中,默认为 False。
@@ -1723,7 +1731,7 @@
17231731
model (str): Model name to use.
17241732
source (str): Supplier to use, such as ``qwen`` / ``glm`` / ``minimax`` / ``siliconflow`` / ``doubao``.
17251733
type (str): Multimodal task type, one of ``stt`` / ``tts`` / ``text2image`` / ``image_editing``.
1726-
base_url (str): Base URL of the platform. Defaults to each supplier's official endpoint.
1734+
url (str): Base URL of the platform. Defaults to each supplier's official endpoint. The alias ``base_url`` is also accepted.
17271735
api_key (str): You can pass an explicit API key. If set to ``auto`` or ``dynamic``, the key is resolved from config at runtime, enabling dynamic key switching.
17281736
dynamic_auth (bool): Whether to enable dynamic auth. When True, it is equivalent to ``api_key='dynamic'``.
17291737
return_trace (bool): Whether to record the result in trace. Defaults to False.

lazyllm/module/llms/automodel.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ class AutoModel:
1111
def __new__(cls, model: Optional[str] = None, *, config_id: Optional[str] = None, source: Optional[str] = None, # noqa C901
1212
type: Optional[str] = None, config: Union[str, bool] = True, **kwargs: Any):
1313
# check and accomodate user params
14-
model = model or kwargs.pop('base_model', kwargs.pop('embed_model_name', None))
14+
model = model or kwargs.pop('base_model', kwargs.pop('embed_model_name', kwargs.pop('model_name', None)))
1515
if model in lazyllm.online.chat:
16+
if source is not None:
17+
raise ValueError(
18+
f'`{model!r}` is a recognised source name; pass it as `source=` and '
19+
f'do not also set `source={source!r}`.')
1620
source, model = model, None
1721

1822
if not model:

lazyllm/module/llms/online_module.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ def __new__(self, model: Optional[str] = None, source: Optional[str] = None, *,
3636
embed_kwargs = params.copy()
3737
embed_kwargs.pop('function', None)
3838
embed_kwargs.setdefault('type', 'rerank' if resolved_type == LLMType.RERANK else 'embed')
39-
return OnlineEmbeddingModule(source=source,
40-
embed_url=url,
41-
embed_model_name=model,
42-
**embed_kwargs)
39+
return OnlineEmbeddingModule(model=model, source=source, url=url, **embed_kwargs)
4340

4441
if resolved_type in list(self._MULTI_TYPE_TO_FUNCTION.keys()):
4542
multi_kwargs = params.copy()

lazyllm/module/llms/onlinemodule/base/onlineChatModuleBase.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from lazyllm.components.utils.file_operate import _delete_old_files, _image_to_base64
1515
from lazyllm.components.utils.downloader.model_downloader import LLMType
1616
from ....servermodule import LLMBase, StaticParams
17-
from .utils import LazyLLMOnlineBase
17+
from .utils import LazyLLMOnlineBase, resolve_online_params
1818

1919
class LazyLLMOnlineChatModuleBase(LazyLLMOnlineBase, LLMBase):
2020
TRAINABLE_MODEL_LIST = []
@@ -126,9 +126,10 @@ def forward(self, __input: Union[Dict, str] = None, *, llm_chat_history: List[Li
126126
# TODO(dengyuang): if current forward set stream_output = False but self._stream = True, will use stream = True
127127
stream_output = stream_output or self._stream
128128
__input, files = self._get_files(__input, lazyllm_files)
129-
runtime_base_url = url or kw.pop('base_url', None)
130-
runtime_url = self._get_chat_url(runtime_base_url) if runtime_base_url else self._chat_url
131-
runtime_model = model or kw.pop('model_name', None) or self._model_name
129+
model, _, url, kw = resolve_online_params(model, None, url, kw,
130+
model_aliases='model_name', url_aliases='base_url')
131+
runtime_url = self._get_chat_url(url) if url else self._chat_url
132+
runtime_model = model or self._model_name
132133

133134
params = {'input': __input, 'history': llm_chat_history, 'return_dict': True}
134135
if tools: params['tools'] = tools

lazyllm/module/llms/onlinemodule/base/onlineEmbeddingModuleBase.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from concurrent.futures import ThreadPoolExecutor, as_completed, wait
44
from lazyllm import LOG
55
from lazyllm.components.utils.downloader.model_downloader import LLMType
6-
from .utils import LazyLLMOnlineBase
6+
from .utils import LazyLLMOnlineBase, resolve_online_params
77
from lazyllm.components.utils.downloader import ModelManager
88

99

@@ -41,8 +41,12 @@ def batch_size(self, value: int):
4141

4242
def forward(self, input: Union[List, str], url: str = None, model: str = None, **kwargs
4343
) -> Union[List[float], List[List[float]]]:
44-
runtime_url = url or kwargs.pop('base_url', kwargs.pop('embed_url', None)) or self._embed_url
45-
runtime_model = model or kwargs.pop('model_name', kwargs.pop('embed_model_name', None)) or self._embed_model_name
44+
model, _, url, kwargs = resolve_online_params(
45+
model, None, url, kwargs,
46+
model_aliases=('model_name', 'embed_model_name', 'embed_name'),
47+
url_aliases=('base_url', 'embed_url'))
48+
runtime_url = url or self._embed_url
49+
runtime_model = model or self._embed_model_name
4650

4751
if runtime_model is not None:
4852
kwargs['model'] = runtime_model

lazyllm/module/llms/onlinemodule/base/onlineMultiModalBase.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import List, Dict, Union, Optional
22
import lazyllm
33
from ....servermodule import LLMBase
4-
from .utils import LazyLLMOnlineBase
4+
from .utils import LazyLLMOnlineBase, resolve_online_params
55
import base64
66
from pathlib import Path
77
import requests
@@ -29,23 +29,18 @@ def type(self):
2929
return 'MultiModal'
3030

3131
def _forward(self, input: Union[Dict, str] = None, files: List[str] = None, **kwargs):
32-
'''Forward method to be implemented by subclasses'''
3332
raise NotImplementedError(f'Subclass {self.__class__.__name__} must implement this method')
3433

3534
def forward(self, input: Union[Dict, str] = None, *, lazyllm_files=None,
3635
url: str = None, model: str = None, **kwargs):
37-
'''Main forward method with file handling'''
38-
try:
39-
input, files = self._get_files(input, lazyllm_files or kwargs.pop('files', None))
40-
runtime_url = url or kwargs.pop('base_url', None) or self._base_url
41-
runtime_model = model or kwargs.pop('model_name', None) or self._model_name
42-
call_params = {'input': input, **kwargs}
43-
if files: call_params['files'] = files
44-
return self._forward(**call_params, model=runtime_model, url=runtime_url)
45-
46-
except Exception as e:
47-
lazyllm.LOG.error(f'Error in {self.__class__.__name__}.forward: {str(e)}')
48-
raise
36+
input, files = self._get_files(input, lazyllm_files or kwargs.pop('files', None))
37+
model, _, url, kwargs = resolve_online_params(model, None, url, kwargs,
38+
model_aliases='model_name', url_aliases='base_url')
39+
runtime_url = url or self._base_url
40+
runtime_model = model or self._model_name
41+
call_params = {'input': input, **kwargs}
42+
if files: call_params['files'] = files
43+
return self._forward(**call_params, model=runtime_model, url=runtime_url)
4944

5045
def __repr__(self):
5146
return lazyllm.make_repr('Module', 'OnlineMultiModalModule',

lazyllm/module/llms/onlinemodule/base/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ def select_source_with_default_key(available_models, explicit_source: Optional[s
3131
f'You can set one of those environment: {excepted}')
3232

3333

34+
def resolve_online_params(
35+
model: Optional[str], source: Optional[str], url: Optional[str], extra: dict,
36+
*, model_aliases: Union[str, tuple] = (), url_aliases: Union[str, tuple] = (), source_registry=None,
37+
) -> tuple:
38+
if isinstance(model_aliases, str): model_aliases = (model_aliases,)
39+
if isinstance(url_aliases, str): url_aliases = (url_aliases,)
40+
remaining = dict(extra)
41+
for alias in model_aliases:
42+
if (val := remaining.pop(alias, None)) is not None:
43+
if model is not None:
44+
raise ValueError(f'Conflicting parameters: `model` and `{alias}` are both provided. Use `model` only.')
45+
model = val
46+
for alias in url_aliases:
47+
if (val := remaining.pop(alias, None)) is not None:
48+
if url is not None:
49+
raise ValueError(f'Conflicting parameters: `url` and `{alias}` are both provided. Use `url` only.')
50+
url = val
51+
if source_registry is not None and model is not None:
52+
_in = (lambda x: x in source_registry) if hasattr(source_registry, '__contains__') else source_registry
53+
if _in(model):
54+
if source is not None and (_in(source) or source == 'dynamic'):
55+
raise ValueError(f'`{model!r}` is a recognised source name; '
56+
f'do not also provide `source={source!r}`.')
57+
source, model = model, source
58+
return model, source, url, remaining
59+
60+
3461
def check_and_add_config(key, description, cfg=config):
3562
if key.lower() not in config.get_all_configs():
3663
cfg.add(key, str, '', f'{key.upper()}', description=description)

0 commit comments

Comments
 (0)