Implementation:1Panel dev MaxKB IModelProvider
| Knowledge Sources | |
|---|---|
| Domains | Model_Provider, Design_Patterns |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
Abstract interface specification for model vendor provider classes, defining the contract for registering models, validating credentials, and instantiating model instances.
Description
The IModelProvider abstract class is the foundation of MaxKB's plugin-based model provider system. Each vendor (OpenAI, Anthropic, Gemini, Ollama, etc.) implements this interface to register its models, define credential validation, and provide model instantiation. The abstract methods are:
- get_model_info_manage() — returns a ModelInfoManage with all registered ModelInfo entries
- get_model_provide_info() — returns ModelProvideInfo with provider name and icon
Concrete implementations (e.g., OpenAIModelProvider) build a ModelInfoManage via the builder pattern, appending ModelInfo entries for each supported model type (LLM, EMBEDDING, STT, TTS, IMAGE, TTI, RERANKER, TTV, ITV).
Usage
Implement this interface when you need to:
- Add a new model vendor to MaxKB
- Understand the registration contract for model providers
- Register custom model types and credentials
Code Reference
Source Location
- Repository: MaxKB
- File: apps/models_provider/base_model_provider.py
Signature
class IModelProvider(ABC):
@abstractmethod
def get_model_info_manage(self) -> ModelInfoManage:
"""Return the ModelInfoManage with all registered models."""
@abstractmethod
def get_model_provide_info(self) -> ModelProvideInfo:
"""Return provider metadata (name, icon)."""
def get_model_type_list(self) -> list:
"""Return list of supported model types."""
def get_model_list(self, model_type: str) -> list:
"""Return list of models for a given type."""
def get_model_credential(self, model_type: str, model_name: str) -> BaseModelCredential:
"""Return the credential handler for a model."""
def get_model(self, model_type, model_name, model_credential: Dict, **model_kwargs) -> BaseModel:
"""Instantiate a model via ModelInfo.model_class.new_instance()."""
def is_valid_credential(self, model_type, model_name, model_credential, model_params,
raise_exception=False) -> bool:
"""Validate credentials for a model."""
def get_dialogue_number(self) -> int:
"""Return default dialogue history count (default: 3)."""
def down_model(self, model_type, model_name, model_credential) -> Iterator[DownModelChunk]:
"""Download model (optional, raises if unsupported)."""
Import
from models_provider.base_model_provider import IModelProvider, ModelInfo, ModelInfoManage, ModelProvideInfo
from models_provider.base_model_provider import MaxKBBaseModel, BaseModelCredential, ModelTypeConst
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model_type | str | Yes | Model type: LLM, EMBEDDING, STT, TTS, IMAGE, TTI, RERANKER, TTV, ITV |
| model_name | str | Yes | Model identifier (e.g., 'gpt-4', 'text-embedding-ada-002') |
| model_credential | Dict | Yes | Credential key-value pairs (api_key, api_base, etc.) |
| model_params | Dict | No | Runtime parameters |
| model_kwargs | kwargs | No | Additional params (model_id, streaming, use_local) |
Outputs
| Name | Type | Description |
|---|---|---|
| get_model() returns | BaseModel | LangChain-compatible model instance |
| get_model_info_manage() returns | ModelInfoManage | Registry of all vendor models |
Usage Examples
Implementing a Vendor Provider
from models_provider.base_model_provider import (
IModelProvider, ModelInfo, ModelInfoManage, ModelProvideInfo,
MaxKBBaseModel, BaseModelCredential, ModelTypeConst,
)
class MyCredential(BaseModelCredential):
def is_valid(self, model_type, model_name, model, model_params, provider, raise_exception=True):
if not model.get('api_key'):
raise Exception("API key is required")
return True
def encryption_dict(self, model_info):
return {'api_key': self.encryption(model_info.get('api_key', ''))}
class MyModel(MaxKBBaseModel):
@staticmethod
def new_instance(model_type, model_name, model_credential, **kwargs):
from langchain_openai import ChatOpenAI
return ChatOpenAI(model=model_name, api_key=model_credential.get('api_key'))
class MyModelProvider(IModelProvider):
def get_model_info_manage(self):
return ModelInfoManage.builder() \
.append_model_info(ModelInfo(
name='my-model',
desc='My custom model',
model_type=ModelTypeConst.LLM,
model_credential=MyCredential(),
model_class=MyModel,
)).build()
def get_model_provide_info(self):
return ModelProvideInfo(provider='my_vendor', name='My Vendor', icon='icon.png')