diff --git a/docs/en/insight/features.mdx b/docs/en/insight/features.mdx index f68a2d81b..2f9ab8b6d 100644 --- a/docs/en/insight/features.mdx +++ b/docs/en/insight/features.mdx @@ -64,6 +64,7 @@ LangBot uses **LiteLLM as a unified model request backend**, so beyond the built | [DeepSeek](https://www.deepseek.com/) | ✅ | | | [Google Gemini](https://aistudio.google.com/prompts/new_chat) | ✅ | | | [xAI](https://x.ai/) | ✅ | | +| [NVIDIA NIM](https://build.nvidia.com/) | ✅ | | | [Moonshot](https://www.moonshot.cn/) | ✅ | | | [Zhipu AI](https://open.bigmodel.cn/) | ✅ | | | [Mistral](https://mistral.ai/) | ✅ | | diff --git a/docs/ja/insight/features.mdx b/docs/ja/insight/features.mdx index 0d74cc8fe..902baa4e2 100644 --- a/docs/ja/insight/features.mdx +++ b/docs/ja/insight/features.mdx @@ -64,6 +64,7 @@ LangBot は **LiteLLM を統一的なモデルリクエストバックエンド* | [DeepSeek](https://www.deepseek.com/) | ✅ | | | [Google Gemini](https://aistudio.google.com/prompts/new_chat) | ✅ | | | [xAI](https://x.ai/) | ✅ | | +| [NVIDIA NIM](https://build.nvidia.com/) | ✅ | | | [Moonshot](https://www.moonshot.cn/) | ✅ | | | [Zhipu AI](https://open.bigmodel.cn/) | ✅ | | | [Mistral](https://mistral.ai/) | ✅ | | diff --git a/docs/zh/insight/features.mdx b/docs/zh/insight/features.mdx index 7e48430d1..84d85f157 100644 --- a/docs/zh/insight/features.mdx +++ b/docs/zh/insight/features.mdx @@ -64,6 +64,7 @@ LangBot 以 **LiteLLM 作为统一的模型请求后端**,因此除下表内 | [DeepSeek](https://www.deepseek.com/) | ✅ | | | [Google Gemini](https://aistudio.google.com/prompts/new_chat) | ✅ | | | [xAI](https://x.ai/) | ✅ | | +| [NVIDIA NIM](https://build.nvidia.com/) | ✅ | | | [Moonshot](https://www.moonshot.cn/) | ✅ | | | [智谱AI](https://open.bigmodel.cn/) | ✅ | | | [Mistral](https://mistral.ai/) | ✅ | | diff --git a/src/langbot/pkg/provider/modelmgr/requesters/nvidia.svg b/src/langbot/pkg/provider/modelmgr/requesters/nvidia.svg new file mode 100644 index 000000000..95bf8f857 --- /dev/null +++ b/src/langbot/pkg/provider/modelmgr/requesters/nvidia.svg @@ -0,0 +1 @@ + diff --git a/src/langbot/pkg/provider/modelmgr/requesters/nvidiachatcmpl.yaml b/src/langbot/pkg/provider/modelmgr/requesters/nvidiachatcmpl.yaml new file mode 100644 index 000000000..37d8d1e2c --- /dev/null +++ b/src/langbot/pkg/provider/modelmgr/requesters/nvidiachatcmpl.yaml @@ -0,0 +1,29 @@ +apiVersion: v1 +kind: LLMAPIRequester +metadata: + name: nvidia-chat-completions + label: + en_US: NVIDIA NIM + zh_Hans: NVIDIA NIM + icon: nvidia.svg +spec: + litellm_provider: openai + config: + - name: base_url + label: + en_US: Base URL + zh_Hans: 基础 URL + type: string + required: true + default: https://integrate.api.nvidia.com/v1 + - name: timeout + label: + en_US: Timeout + zh_Hans: 超时时间 + type: integer + required: true + default: 120 + alias: "nvidia NVIDIA nim NIM nemotron Nemotron 英伟达 build.nvidia.com" + support_type: + - llm + provider_category: manufacturer diff --git a/tests/unit_tests/provider/requesters/test_nvidiachatcmpl.py b/tests/unit_tests/provider/requesters/test_nvidiachatcmpl.py new file mode 100644 index 000000000..a045e74e8 --- /dev/null +++ b/tests/unit_tests/provider/requesters/test_nvidiachatcmpl.py @@ -0,0 +1,102 @@ +"""Regression coverage for the NVIDIA NIM requester manifest.""" + +from __future__ import annotations + +import json +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import AsyncMock, Mock, patch + +import litellm +import pytest +import yaml + +from langbot.pkg.discover.engine import ComponentDiscoveryEngine +from langbot.pkg.provider.modelmgr.modelmgr import ModelManager +from langbot.pkg.provider.modelmgr.requesters import litellmchat +from langbot.pkg.provider.modelmgr.requesters.litellmchat import LiteLLMRequester +from langbot_plugin.api.entities.builtin.provider.message import Message +from tests.unit_tests.provider.conftest import TEST_EXECUTION_CONTEXT +from tests.unit_tests.provider.test_reasoning_control import _runtime_model + +MANIFEST_PATH = 'pkg/provider/modelmgr/requesters/nvidiachatcmpl.yaml' +NVIDIA_BASE_URL = 'https://integrate.api.nvidia.com/v1' +NVIDIA_MODEL = 'nvidia/nemotron-3.5-super-vl-120b-a12b' + + +def _load_component(): + engine = ComponentDiscoveryEngine(SimpleNamespace()) + return engine.load_component_manifest(MANIFEST_PATH) + + +def test_nvidia_manifest_is_llm_requester(): + component = _load_component() + + assert component is not None + assert component.kind == 'LLMAPIRequester' + assert component.metadata.name == 'nvidia-chat-completions' + assert component.metadata.icon == 'nvidia.svg' + assert component.spec['litellm_provider'] == 'openai' + assert component.spec['support_type'] == ['llm'] + assert component.execution is None + icon = Path(__file__).parents[4] / 'src/langbot' / component.icon_rel_path + assert icon.is_file() + + fields = {item['name']: item for item in component.spec['config']} + assert fields['base_url']['default'] == NVIDIA_BASE_URL + assert fields['base_url']['type'] == 'string' + assert fields['timeout']['default'] == 120 + assert 'model' not in fields + + +def test_nvidia_manifest_has_no_hard_coded_key(): + manifest = yaml.safe_load((Path(__file__).parents[4] / 'src/langbot' / MANIFEST_PATH).read_text(encoding='utf-8')) + assert 'api_key' not in json.dumps(manifest) + + +@pytest.mark.asyncio +async def test_nvidia_provider_routes_through_litellm(mock_app_for_modelmgr): + app = mock_app_for_modelmgr + app.discover = ComponentDiscoveryEngine(app) + app.discover.load_component_manifest(MANIFEST_PATH) + + model_mgr = ModelManager(app) + await model_mgr.initialize() + assert 'nvidia-chat-completions' not in model_mgr.requester_dict + assert model_mgr.get_available_requester_manifest_by_name('nvidia-chat-completions') is not None + + runtime_provider = await model_mgr.load_provider( + TEST_EXECUTION_CONTEXT, + { + 'uuid': 'nvidia-provider', + 'name': 'NVIDIA NIM', + 'requester': 'nvidia-chat-completions', + 'base_url': NVIDIA_BASE_URL, + 'api_keys': ['test'], + }, + ) + adapter = runtime_provider.requester + assert isinstance(adapter, LiteLLMRequester) + assert adapter.requester_cfg['custom_llm_provider'] == 'openai' + assert adapter.requester_cfg['base_url'] == NVIDIA_BASE_URL + + calls = [] + + async def fake_acompletion(**kwargs): + calls.append(kwargs) + return litellm.ModelResponse( + model=NVIDIA_MODEL, + choices=[{'index': 0, 'message': {'role': 'assistant', 'content': 'ok'}, 'finish_reason': 'stop'}], + usage={'prompt_tokens': 1, 'completion_tokens': 1, 'total_tokens': 2}, + ) + + model = _runtime_model(adapter, name=NVIDIA_MODEL, abilities=[]) + model.provider.token_mgr.get_token = Mock(return_value='test') + with patch.object(litellmchat, 'acompletion', new=AsyncMock(side_effect=fake_acompletion)): + result, _ = await adapter.invoke_llm(None, model, [Message(role='user', content='hello')]) + + assert result.content == 'ok' + assert calls[0]['model'] == f'openai/{NVIDIA_MODEL}' + assert calls[0]['api_base'] == NVIDIA_BASE_URL + assert calls[0]['api_key'] == 'test' + await adapter.aclose()