ReMe/reme/components/as_llm/__init__.py
jinliyl 4c54c2b650
Some checks failed
CI / Python packages / Build and verify distributions (push) Waiting to run
CI / Python quality / GitHub Actions (push) Waiting to run
CI / Python quality / Pre-commit (push) Waiting to run
CI / Python tests / Unit Tests - py3.11 (push) Waiting to run
CI / Python tests / Unit Tests - py3.12 (push) Waiting to run
CI / Python tests / Unit Tests - py3.13 (push) Waiting to run
CI / Python tests / Unit Tests - py3.14 (push) Waiting to run
CI / Windows / CLI smoke - py3.11 (push) Waiting to run
Deploy / Documentation / Build documentation (push) Waiting to run
Deploy / Documentation / deploy (push) Blocked by required conditions
CI and Release / Docker / Build and test / amd64 (push) Waiting to run
CI and Release / Docker / Build and test / arm64 (push) Waiting to run
CI and Release / Docker / Publish multi-platform tags (push) Blocked by required conditions
Security / CodeQL / Analyze javascript-typescript (push) Waiting to run
Security / CodeQL / Analyze python (push) Waiting to run
CI / ReMe Studio / Studio checks (push) Has been cancelled
feat(docker): add container deployment and multi-platform release workflow (#582)
* feat(docker): add container deployment and multi-platform release workflow

* fix(llm): initialize providers on model access and preserve runtime injection

* test(llm): keep rejected model updates independent of credentials
2026-10-03 23:54:58 +08:00

128 lines
3.2 KiB
Python

"""LLM model wrappers for AgentScope."""
from agentscope.credential import (
AnthropicCredential,
CredentialBase,
DashScopeCredential,
DeepSeekCredential,
GeminiCredential,
MoonshotCredential,
OllamaCredential,
OpenAICredential,
XAICredential,
)
from agentscope.model import ChatModelBase
from ..base_component import BaseComponent
from ..component_registry import R
from ...enumeration import ComponentEnum
class BaseAsLLM(BaseComponent):
"""Base wrapper for AgentScope chat models.
Subclasses set ``credential_cls``. Providers are constructed on first use,
allowing local file and search jobs to run without model credentials.
"""
component_type = ComponentEnum.AS_LLM
credential_cls: type[CredentialBase]
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._model: ChatModelBase | None = None
async def _start(self) -> None:
"""Keep service startup independent of provider credentials."""
@property
def model(self) -> ChatModelBase:
"""Initialize on first access, including direct Step dependency resolution."""
self.initialize_model()
assert self._model is not None
return self._model
@model.setter
def model(self, value: ChatModelBase | None) -> None:
"""Preserve explicit model injection used by standalone consumers."""
self._model = value
def initialize_model(self) -> None:
"""Construct the configured provider once, without making a remote request."""
if self._model is not None:
return
kwargs = dict(self.kwargs)
credential = self.credential_cls(**kwargs.pop("credential", {}))
model_cls = credential.get_chat_model_class()
params_dict = kwargs.pop("parameters", None)
parameters = model_cls.Parameters(**params_dict) if params_dict else None
self._model = model_cls(credential=credential, parameters=parameters, **kwargs)
@R.register("openai")
class OpenAIAsLLM(BaseAsLLM):
"""OpenAI chat model wrapper."""
credential_cls = OpenAICredential
@R.register("anthropic")
class AnthropicAsLLM(BaseAsLLM):
"""Anthropic chat model wrapper."""
credential_cls = AnthropicCredential
@R.register("dashscope")
class DashScopeAsLLM(BaseAsLLM):
"""DashScope chat model wrapper."""
credential_cls = DashScopeCredential
@R.register("deepseek")
class DeepSeekAsLLM(BaseAsLLM):
"""DeepSeek chat model wrapper."""
credential_cls = DeepSeekCredential
@R.register("gemini")
class GeminiAsLLM(BaseAsLLM):
"""Gemini chat model wrapper."""
credential_cls = GeminiCredential
@R.register("moonshot")
class MoonshotAsLLM(BaseAsLLM):
"""Moonshot chat model wrapper."""
credential_cls = MoonshotCredential
@R.register("ollama")
class OllamaAsLLM(BaseAsLLM):
"""Ollama chat model wrapper."""
credential_cls = OllamaCredential
@R.register("xai")
class XAIAsLLM(BaseAsLLM):
"""xAI chat model wrapper."""
credential_cls = XAICredential
__all__ = [
"BaseAsLLM",
"OpenAIAsLLM",
"AnthropicAsLLM",
"DashScopeAsLLM",
"DeepSeekAsLLM",
"GeminiAsLLM",
"MoonshotAsLLM",
"OllamaAsLLM",
"XAIAsLLM",
]