LLM Configuration and Selection#
RAGFlow's LLM configuration operates at two levels: a static binding stored on each chat assistant (dialog), and an optional per-request runtime override applied during inference. Both paths converge on resolve_model_config, the central resolution function.
Chat Assistant LLM Binding (llm_id)#
Every Dialog record carries two LLM reference fields :
| Field | Type | Purpose |
|---|---|---|
llm_id | CharField(128) | Human-readable composite model name, e.g. gpt-4o@default@OpenAI |
tenant_llm_id | CharField(32) | UUID FK into TenantModel.id (post-v0.27.0 three-tier hierarchy) |
Generation parameters are stored alongside the model reference in llm_setting , which defaults to {"temperature": 0.1, "top_p": 0.3, "frequency_penalty": 0.7, "presence_penalty": 0.4, "max_tokens": 512}.
On create (POST /chats), if llm_id is not provided it falls back to tenant.tenant_llm_id β the tenant's system-wide default chat model .
Model Reference Formats#
llm_id accepts two reference formats, resolved by resolve_model_config:
- UUID β matches a
TenantModel.id; resolved directly viaget_model_config_by_id. - Composite name β parsed right-to-left by
split_model_nameas{model_name}@{instance_name}@{provider_name}. The right-anchored split preserves model names that themselves contain@(e.g. quantization suffixes liketext-embedding-v1@q8_0@lmstudio@LM-Studio). If only one@separator is present,instance_namedefaults to"default".
resolve_model_config tries UUID first; on LookupError it falls back to composite-name resolution .
Runtime Per-Request LLM Override#
Both the native chat API (POST /chat/completions) and the OpenAI-compatible endpoint (POST /openai/<chat_id>/chat/completions) support overriding the dialog's bound model at request time.
Native API β pass llm_id in the request body :
- If
llm_idis present,get_api_keyis called to verify the tenant has a valid credential for that model. On failure, the request is rejected with"Cannot use specified model {chat_model_id}.". dia.llm_idis replaced in-memory for the duration of the request; the stored dialog record is not mutated.- If
llm_idis absent and the dialog has no model bound, the tenant's defaultllm_idis used as a final fallback .
OpenAI-compatible API β the model field in the request body drives selection :
- If
modelis the literal string"model"(the placeholder), the dialog's storedllm_idis used unchanged. - Any other value is validated via
_validate_llm_id(which callsresolve_model_config) and then API-key-checked withget_api_key. On success,dia.llm_idis overridden for the request.
Validation Functions#
| Function | Location | What it checks |
|---|---|---|
_validate_llm_id | chat_api.py | Calls resolve_model_config; returns an error string or None. Handles model_type as string or list; defaults to "chat" if not "vision". |
_validate_llm_id | openai_api.py | Sync variant; same resolve logic, model_type always coerced to "chat" or "vision". |
_validate_rerank_id | chat_api.py | Skips validation for built-in rerank models (BAAI/bge-reranker-v2-m3, maidalun1020/bce-reranker-base_v1); otherwise calls resolve_model_config with model_type="rerank". |
get_api_key | joint_services/tenant_model_service.py | Verifies provider β instance chain; returns the stored api_key string or raises LookupError. Used at request time, not during dialog create/update. |
Key Source Files#
| File | Role |
|---|---|
api/apps/restful_apis/chat_api.py | Native chat/completion endpoints; _validate_llm_id, _validate_rerank_id, runtime override logic |
api/apps/restful_apis/openai_api.py | OpenAI-compatible endpoint; _validate_llm_id (sync), model placeholder logic |
api/db/joint_services/tenant_model_service.py | resolve_model_config, get_api_key, split_model_name β core resolution layer |
api/db/db_models.py | Dialog model schema: llm_id, tenant_llm_id, llm_setting fields |
web/src/components/model-tree-select.tsx | UI picker; ModelTypeMap defines which model types llm_id accepts (chat, vision) |
Related Articles#
- Model Provider Architecture β explains the three-tier
Provider β Instance β Modelhierarchy (TenantModelProvider,TenantModelInstance,TenantModel) and themodel_typebit flags that underpinresolve_model_config. - Model Selection UI β covers
ModelTreeSelect,SystemSetting, and tenant default model management.