Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

add_english_doc('AutoModel', '''\
Expand All @@ -1472,7 +1472,7 @@
source (Optional[str]): Provider for online modules (``qwen`` / ``glm`` / ``openai``). Set to ``local`` to force a local TrainableModule.
type (Optional[str]): Model type. If omitted, it will try to fetch from kwargs or be inferred by the online module.
config (Union[str, bool]): Whether to enable overrides from ``auto_model_config_map``, or a user-specified config file path. Defaults to True.
**kwargs: Accepts `base_model` and `embed_model_name` as synonyms for `model`; does not accept other user-provided fields.
**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.
''')

add_chinese_doc('OnlineModule', '''\
Expand Down Expand Up @@ -1525,8 +1525,8 @@

Args:
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)``
source (str): 指定要创建的模块类型,可选为 ``openai`` / ``sensenova`` / ``glm`` / ``kimi`` / ``qwen`` / ``doubao`` / ``ppio`` / ``deepseek(暂时不支持访问)``
base_url (str): 指定要访问的平台的基础链接,默认是官方链接
source (str): 指定要创建的模块类型,可选为 ``openai`` / ``sensenova`` / ``glm`` / ``kimi`` / ``qwen`` / ``doubao`` / ``ppio`` / ``deepseek(暂时不支持访问)``。也可以直接将 source 名称作为 ``model`` 传入,系统会自动识别并交换两者。
url (str): 指定要访问的平台的基础链接,默认是官方链接。也可使用别名 ``base_url`` 传入。
system_prompt (str): 指定请求的system prompt,默认是官方给的system prompt
api_key (str): 可显式传入 API Key;当设置为 ``auto`` 或 ``dynamic`` 时,将在运行时从配置读取,支持动态切换 key
stream (bool): 是否流式请求和输出,默认为流式
Expand All @@ -1538,9 +1538,9 @@
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)

Args:
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)`` .
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``.
source (str): Specify the type of module to create. Options include ``openai`` / ``sensenova`` / ``glm`` / ``kimi`` / ``qwen`` / ``doubao`` / ``ppio`` / ``deepseek (not yet supported)`` .
base_url (str): Specify the base link of the platform to be accessed. The default is the official link.
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.
system_prompt (str): Specify the requested system prompt. The default is the official system prompt.
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.
stream (bool): Whether to request and output in streaming mode, default is streaming.
Expand Down Expand Up @@ -1677,22 +1677,28 @@
用来管理创建目前市面上的在线Embedding服务模块,目前支持openai、sensenova、glm、qwen、doubao

Args:
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`` 传入,系统会自动识别并交换两者。
source (str): 指定要创建的模块类型,可选为 ``openai`` / ``sensenova`` / ``glm`` / ``qwen`` / ``doubao``
embed_url (str): 指定要访问的平台的基础链接,默认是官方链接
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)``
url (str): 指定要访问的平台的基础链接,默认是官方链接。也可使用别名 ``embed_url`` 或 ``base_url`` 传入。
type (str): 模型服务类型,可选 ``embed`` / ``rerank``,默认根据模型名自动推断。
api_key (str): 可显式传入 API Key;当设置为 ``auto`` 或 ``dynamic`` 时,将在运行时从配置读取,支持动态切换 key
dynamic_auth (bool): 是否启用动态鉴权;为 True 时等价于 ``api_key='dynamic'``
return_trace (bool): 是否将结果记录在trace中,默认为False
batch_size (int): 批量请求时每批的大小,默认为32
''')

add_english_doc('OnlineEmbeddingModule', '''\
Used to manage and create online Embedding service modules currently on the market, currently supporting openai, sensenova, glm, qwen, doubao.

Args:
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``.
source (str): Specify the type of module to create. Options are ``openai`` / ``sensenova`` / ``glm`` / ``qwen`` / ``doubao``.
embed_url (str): Specify the base link of the platform to be accessed. The default is the official link.
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)``
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.
type (str): Service type, either ``embed`` or ``rerank``. Inferred from the model name when omitted.
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.
dynamic_auth (bool): Whether to enable dynamic auth. When True, it is equivalent to ``api_key='dynamic'``.
return_trace (bool): Whether to record the results in trace. Defaults to False.
batch_size (int): Batch size for bulk requests. Defaults to 32.
''')

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

add_chinese_doc('OnlineMultiModalModule', '''\
Expand All @@ -1710,7 +1718,7 @@
model (str): 指定要访问的模型名称。
source (str): 指定要创建的模块类型,如 ``qwen`` / ``glm`` / ``minimax`` / ``siliconflow`` / ``doubao`` 等。
type (str): 多模态任务类型,可选 ``stt`` / ``tts`` / ``text2image`` / ``image_editing``。
base_url (str): 指定要访问的平台基础链接,默认使用各平台官方链接。
url (str): 指定要访问的平台基础链接,默认使用各平台官方链接。也可使用别名 ``base_url`` 传入
api_key (str): 可显式传入 API Key;当设置为 ``auto`` 或 ``dynamic`` 时,将在运行时从配置读取,支持动态切换 key。
dynamic_auth (bool): 是否启用动态鉴权;为 True 时等价于 ``api_key='dynamic'``。
return_trace (bool): 是否将结果记录在 trace 中,默认为 False。
Expand All @@ -1723,7 +1731,7 @@
model (str): Model name to use.
source (str): Supplier to use, such as ``qwen`` / ``glm`` / ``minimax`` / ``siliconflow`` / ``doubao``.
type (str): Multimodal task type, one of ``stt`` / ``tts`` / ``text2image`` / ``image_editing``.
base_url (str): Base URL of the platform. Defaults to each supplier's official endpoint.
url (str): Base URL of the platform. Defaults to each supplier's official endpoint. The alias ``base_url`` is also accepted.
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.
dynamic_auth (bool): Whether to enable dynamic auth. When True, it is equivalent to ``api_key='dynamic'``.
return_trace (bool): Whether to record the result in trace. Defaults to False.
Expand Down
6 changes: 5 additions & 1 deletion lazyllm/module/llms/automodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ class AutoModel:
def __new__(cls, model: Optional[str] = None, *, config_id: Optional[str] = None, source: Optional[str] = None, # noqa C901
type: Optional[str] = None, config: Union[str, bool] = True, **kwargs: Any):
# check and accomodate user params
model = model or kwargs.pop('base_model', kwargs.pop('embed_model_name', None))
model = model or kwargs.pop('base_model', kwargs.pop('embed_model_name', kwargs.pop('model_name', None)))
if model in lazyllm.online.chat:
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

if not model:
Expand Down
5 changes: 1 addition & 4 deletions lazyllm/module/llms/online_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ def __new__(self, model: Optional[str] = None, source: Optional[str] = None, *,
embed_kwargs = params.copy()
embed_kwargs.pop('function', None)
embed_kwargs.setdefault('type', 'rerank' if resolved_type == LLMType.RERANK else 'embed')
return OnlineEmbeddingModule(source=source,
embed_url=url,
embed_model_name=model,
**embed_kwargs)
return OnlineEmbeddingModule(model=model, source=source, url=url, **embed_kwargs)

if resolved_type in list(self._MULTI_TYPE_TO_FUNCTION.keys()):
multi_kwargs = params.copy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from lazyllm.components.utils.file_operate import _delete_old_files, _image_to_base64
from lazyllm.components.utils.downloader.model_downloader import LLMType
from ....servermodule import LLMBase, StaticParams
from .utils import LazyLLMOnlineBase
from .utils import LazyLLMOnlineBase, resolve_online_params

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

params = {'input': __input, 'history': llm_chat_history, 'return_dict': True}
if tools: params['tools'] = tools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from concurrent.futures import ThreadPoolExecutor, as_completed, wait
from lazyllm import LOG
from lazyllm.components.utils.downloader.model_downloader import LLMType
from .utils import LazyLLMOnlineBase
from .utils import LazyLLMOnlineBase, resolve_online_params
from lazyllm.components.utils.downloader import ModelManager


Expand Down Expand Up @@ -41,8 +41,12 @@ def batch_size(self, value: int):

def forward(self, input: Union[List, str], url: str = None, model: str = None, **kwargs
) -> Union[List[float], List[List[float]]]:
runtime_url = url or kwargs.pop('base_url', kwargs.pop('embed_url', None)) or self._embed_url
runtime_model = model or kwargs.pop('model_name', kwargs.pop('embed_model_name', None)) or self._embed_model_name
model, _, url, kwargs = resolve_online_params(
model, None, url, kwargs,
model_aliases=('model_name', 'embed_model_name', 'embed_name'),
url_aliases=('base_url', 'embed_url'))
runtime_url = url or self._embed_url
runtime_model = model or self._embed_model_name

if runtime_model is not None:
kwargs['model'] = runtime_model
Expand Down
23 changes: 9 additions & 14 deletions lazyllm/module/llms/onlinemodule/base/onlineMultiModalBase.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Dict, Union, Optional
import lazyllm
from ....servermodule import LLMBase
from .utils import LazyLLMOnlineBase
from .utils import LazyLLMOnlineBase, resolve_online_params
import base64
from pathlib import Path
import requests
Expand Down Expand Up @@ -29,23 +29,18 @@ def type(self):
return 'MultiModal'

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

def forward(self, input: Union[Dict, str] = None, *, lazyllm_files=None,
url: str = None, model: str = None, **kwargs):
'''Main forward method with file handling'''
try:
input, files = self._get_files(input, lazyllm_files or kwargs.pop('files', None))
runtime_url = url or kwargs.pop('base_url', None) or self._base_url
runtime_model = model or kwargs.pop('model_name', None) or self._model_name
call_params = {'input': input, **kwargs}
if files: call_params['files'] = files
return self._forward(**call_params, model=runtime_model, url=runtime_url)

except Exception as e:
lazyllm.LOG.error(f'Error in {self.__class__.__name__}.forward: {str(e)}')
raise
input, files = self._get_files(input, lazyllm_files or kwargs.pop('files', None))
model, _, url, kwargs = resolve_online_params(model, None, url, kwargs,
model_aliases='model_name', url_aliases='base_url')
runtime_url = url or self._base_url
runtime_model = model or self._model_name
call_params = {'input': input, **kwargs}
if files: call_params['files'] = files
return self._forward(**call_params, model=runtime_model, url=runtime_url)

def __repr__(self):
return lazyllm.make_repr('Module', 'OnlineMultiModalModule',
Expand Down
27 changes: 27 additions & 0 deletions lazyllm/module/llms/onlinemodule/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ def select_source_with_default_key(available_models, explicit_source: Optional[s
f'You can set one of those environment: {excepted}')


def resolve_online_params(
model: Optional[str], source: Optional[str], url: Optional[str], extra: dict,
*, model_aliases: Union[str, tuple] = (), url_aliases: Union[str, tuple] = (), source_registry=None,
) -> tuple:
if isinstance(model_aliases, str): model_aliases = (model_aliases,)
if isinstance(url_aliases, str): url_aliases = (url_aliases,)
remaining = dict(extra)
for alias in model_aliases:
if (val := remaining.pop(alias, None)) is not None:
if model is not None:
raise ValueError(f'Conflicting parameters: `model` and `{alias}` are both provided. Use `model` only.')
model = val
for alias in url_aliases:
if (val := remaining.pop(alias, None)) is not None:
if url is not None:
raise ValueError(f'Conflicting parameters: `url` and `{alias}` are both provided. Use `url` only.')
url = val
if source_registry is not None and model is not None:
_in = (lambda x: x in source_registry) if hasattr(source_registry, '__contains__') else source_registry
if _in(model):
if source is not None and (_in(source) or source == 'dynamic'):
raise ValueError(f'`{model!r}` is a recognised source name; '
f'do not also provide `source={source!r}`.')
source, model = model, source
return model, source, url, remaining


def check_and_add_config(key, description, cfg=config):
if key.lower() not in config.get_all_configs():
cfg.add(key, str, '', f'{key.upper()}', description=description)
Expand Down
Loading
Loading