Implementation:1Panel dev MaxKB get model
| Knowledge Sources | |
|---|---|
| Domains | Model_Provider |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
Concrete utility functions for runtime model instantiation and provider lookup from database records, provided by MaxKB.
Description
The get_model and get_provider functions in models_provider/tools.py are the runtime entry points for model instantiation. get_model takes a Django Model database record (with provider, model_type, model_name, and RSA-encrypted credential), decrypts the credential, resolves the provider via ModelProvideConstants enum, and calls IModelProvider.get_model() to instantiate a LangChain-compatible model. get_model_instance_by_model_workspace_id adds workspace authorization and parameter resolution.
Usage
Import these functions when you need to:
- Instantiate a model from a database record at runtime
- Resolve a provider instance by its string identifier
- Get a model with workspace-level authorization
Code Reference
Source Location
- Repository: MaxKB
- File: apps/models_provider/tools.py
Signature
def get_model(model: Model, **kwargs) -> BaseModel:
"""Instantiate a model from a Django Model DB record."""
def get_provider(provider: str) -> IModelProvider:
"""Resolve a provider instance by its string key."""
def get_model_instance_by_model_workspace_id(
model_id: str,
workspace_id: str,
**kwargs,
) -> BaseModel:
"""Get a model instance with workspace authorization and default params."""
def get_model_by_id(_id: str, workspace_id: str) -> Model:
"""Look up a Model DB record by ID with workspace authorization."""
def get_model_default_params(model: Model) -> Dict:
"""Extract default parameter values from model's params form."""
Import
from models_provider.tools import get_model, get_provider, get_model_instance_by_model_workspace_id
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | Model | Yes (for get_model) | Django Model DB record with provider, model_type, model_name, credential |
| provider | str | Yes (for get_provider) | Provider string key (e.g., 'model_openai_provider') |
| model_id | str | Yes (for get_model_instance) | Model UUID |
| workspace_id | str | Yes | Workspace UUID for authorization |
| kwargs | dict | No | Additional params: streaming, model_id, use_local |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | BaseModel | LangChain-compatible model instance (ChatModel, Embeddings, etc.) |
Usage Examples
Instantiating a Model from DB Record
from models_provider.tools import get_model, get_model_instance_by_model_workspace_id
from models_provider.models import Model
from django.db.models import QuerySet
# Get the model DB record
model_record = QuerySet(Model).filter(id="model-uuid").first()
# Instantiate the model (credential is RSA-decrypted internally)
chat_model = get_model(model_record, streaming=True)
# chat_model is now a LangChain BaseChatModel instance
With Workspace Authorization
# Get model with workspace-level authorization and default params
embedding_model = get_model_instance_by_model_workspace_id(
model_id="model-uuid",
workspace_id="ws-uuid",
)
# embedding_model is ready for .embed_query() / .embed_documents()