mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-24 17:36:47 +08:00
Feat: model provider (#16028)
### What problem does this PR solve? Feat: - Allow upsert model_type for instance model Fix: - Allow create instance with duplicate api_key ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@@ -630,6 +630,66 @@ def list_instance_models(tenant_id: str = None, provider_name: str = None, insta
|
||||
return get_error_data_result(message="Internal server error")
|
||||
|
||||
|
||||
@manager.route("/providers/<provider_name>/instances/<instance_name>/models", methods=["PUT"]) # noqa: F821
|
||||
@login_required
|
||||
@add_tenant_id_to_kwargs
|
||||
async def update_instance_models(tenant_id: str, provider_name: str, instance_name: str):
|
||||
"""
|
||||
Batch update model_type for models in instance.
|
||||
---
|
||||
tags:
|
||||
- Providers
|
||||
security:
|
||||
- ApiKeyAuth: []
|
||||
parameters:
|
||||
- in: path
|
||||
name: provider_name
|
||||
type: string
|
||||
required: true
|
||||
description: Provider name.
|
||||
- in: path
|
||||
name: instance_name
|
||||
type: string
|
||||
required: true
|
||||
description: Instance name.
|
||||
- in: header
|
||||
name: Authorization
|
||||
type: string
|
||||
required: true
|
||||
description: Bearer token for authentication.
|
||||
- in: body
|
||||
name: body
|
||||
description: Model details.
|
||||
required: true
|
||||
schema:
|
||||
type: object
|
||||
required:
|
||||
- model_name
|
||||
- model_type
|
||||
properties:
|
||||
model_name:
|
||||
type: list of string
|
||||
description: Model name.
|
||||
model_type:
|
||||
type: list of string
|
||||
description: Model type.
|
||||
"""
|
||||
data = await request.get_json()
|
||||
if not data or "model_name" not in data or "model_type" not in data:
|
||||
return get_error_argument_result(message="model_name and model_type are required")
|
||||
model_name = data["model_name"]
|
||||
model_type = data["model_type"]
|
||||
try:
|
||||
success, msg = provider_api_service.update_instance_models(tenant_id, provider_name, instance_name, model_name, model_type)
|
||||
if success:
|
||||
return get_result(message=msg)
|
||||
else:
|
||||
return get_error_data_result(message=msg)
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
return get_error_data_result(message="Internal server error")
|
||||
|
||||
|
||||
@manager.route("/providers/<provider_name>/instances/<instance_name>/models", methods=["POST"]) # noqa: F821
|
||||
@login_required
|
||||
@add_tenant_id_to_kwargs
|
||||
|
||||
@@ -127,7 +127,7 @@ def _get_model_info(tenant_id: str, default_model: str, model_type: str):
|
||||
model_entity = TenantModelService.get_by_provider_id_and_instance_id_and_model_type_and_model_name(
|
||||
provider_obj.id, instance_obj.id, model_type, model_name
|
||||
)
|
||||
enable = model_entity is None or model_entity.status != ActiveStatusEnum.INACTIVE.value
|
||||
enable = model_entity is None or model_entity.status == ActiveStatusEnum.ACTIVE.value
|
||||
|
||||
if not enable:
|
||||
return None
|
||||
@@ -214,7 +214,7 @@ def _check_model_available(tenant_id: str, provider_name: str, instance_name: st
|
||||
provider_obj.id, instance_obj.id, model_type, model_name
|
||||
)
|
||||
if model_entity:
|
||||
if model_entity.status == "inactive":
|
||||
if model_entity.status != ActiveStatusEnum.ACTIVE.value:
|
||||
return False, f"Model '{model_name}' isn't available"
|
||||
return True, None
|
||||
|
||||
@@ -364,7 +364,8 @@ def list_tenant_added_models(tenant_id: str, model_type_filter: str=None):
|
||||
manual_modified_models = model_record_map.get(model_record_key, [])
|
||||
active_model_types = [manual_model.model_type for manual_model in manual_modified_models if manual_model.status == ActiveStatusEnum.ACTIVE.value]
|
||||
inactive_model_types = [manual_model.model_type for manual_model in manual_modified_models if manual_model.status == ActiveStatusEnum.INACTIVE.value]
|
||||
model_types = list(set(factory_model_types + active_model_types) - set(inactive_model_types))
|
||||
unsupport_model_types = [manual_model.model_type for manual_model in manual_modified_models if manual_model.status == ActiveStatusEnum.UNSUPPORTED.value]
|
||||
model_types = list(set(factory_model_types + active_model_types) - set(inactive_model_types) - set(unsupport_model_types))
|
||||
if not model_types:
|
||||
continue
|
||||
|
||||
|
||||
@@ -313,9 +313,6 @@ async def create_provider_instance(tenant_id: str, provider_name: str, instance_
|
||||
api_key_str = ""
|
||||
if api_key:
|
||||
api_key_str = api_key if isinstance(api_key, str) else json.dumps(api_key)
|
||||
same_key_instance = TenantModelInstanceService.get_by_provider_id_and_api_key(provider_obj.id, api_key_str)
|
||||
if same_key_instance:
|
||||
return False, f"Already exist instance: {same_key_instance.instance_name} with api_key {api_key}"
|
||||
success, msg = await verify_api_key(provider_name, api_key, base_url, region, model_info)
|
||||
if not success:
|
||||
return False, msg
|
||||
@@ -594,6 +591,57 @@ def drop_provider_instances(tenant_id: str, provider_name: str, instance_names:
|
||||
return True, None
|
||||
|
||||
|
||||
def _hybrid_get_instance_models(provider_name: str, instance_id: str):
|
||||
# List all models from the LLM dictionary for this provider
|
||||
factory_info = [f for f in FACTORY_LLM_INFOS if f["name"] == provider_name]
|
||||
if not factory_info:
|
||||
return False, f"Provider '{provider_name}' not found"
|
||||
|
||||
# Get model records for this instance from tenant_model table
|
||||
model_records = TenantModelService.get_models_by_instance_id(instance_id)
|
||||
# Build a map of model_name -> status, type
|
||||
model_info_map: dict = {}
|
||||
model_unsupported_type_map = {}
|
||||
for model_record in model_records:
|
||||
if model_record.status == ActiveStatusEnum.UNSUPPORTED:
|
||||
if model_unsupported_type_map.get(model_record.model_name):
|
||||
model_unsupported_type_map[model_record.model_name].append(model_record.model_type)
|
||||
else:
|
||||
model_unsupported_type_map[model_record.model_name] = [model_record.model_type]
|
||||
continue
|
||||
if model_info_map.get(model_record.model_name):
|
||||
model_info_map[model_record.model_name]["model_type"].append(model_record.model_type)
|
||||
else:
|
||||
model_info_map[model_record.model_name] = {
|
||||
"status": model_record.status,
|
||||
"model_type": [model_record.model_type],
|
||||
"extra": model_record.extra
|
||||
}
|
||||
|
||||
llms = factory_info[0].get("llm", [])
|
||||
models = []
|
||||
for llm in llms:
|
||||
models.append({
|
||||
"name": llm["llm_name"],
|
||||
"model_type": list(
|
||||
set(_factory_model_types(llm) + model_info_map.get(llm["llm_name"], {}).get("model_type", [])) - set(model_unsupported_type_map.get(llm["llm_name"], []))
|
||||
),
|
||||
"max_tokens": llm.get("max_tokens"),
|
||||
"status": model_info_map.get(llm["llm_name"], {}).get("status", "active"),
|
||||
})
|
||||
factory_models = [m["name"] for m in models]
|
||||
for model_name, model_info_dict in model_info_map.items():
|
||||
if model_name not in factory_models:
|
||||
extra_fields = json.loads(model_info_dict["extra"]) if model_info_dict["extra"] else {}
|
||||
models.append({
|
||||
"name": model_name,
|
||||
"model_type": set(model_info_dict["model_type"]) - set(model_unsupported_type_map.get(model_name, [])),
|
||||
"max_tokens": extra_fields.get("max_tokens", 8192),
|
||||
"status": model_info_dict["status"],
|
||||
})
|
||||
return True, models
|
||||
|
||||
|
||||
def list_instance_models(tenant_id: str, provider_name: str, instance_name: str, supported_only: bool = False):
|
||||
"""
|
||||
List models for a provider instance.
|
||||
@@ -628,47 +676,40 @@ def list_instance_models(tenant_id: str, provider_name: str, instance_name: str,
|
||||
if not instance_obj:
|
||||
return False, f"No instance found for provider '{provider_name}' and instance '{instance_name}'"
|
||||
|
||||
# Get model records for this instance from tenant_model table
|
||||
model_records = TenantModelService.get_models_by_instance_id(instance_obj.id)
|
||||
# Build a map of model_name -> status, type
|
||||
model_info_map: dict = {}
|
||||
for model_record in model_records:
|
||||
if model_info_map.get(model_record.model_name):
|
||||
model_info_map[model_record.model_name]["model_type"].append(model_record.model_type)
|
||||
else:
|
||||
model_info_map[model_record.model_name] = {
|
||||
"status": model_record.status,
|
||||
"model_type": [model_record.model_type],
|
||||
"extra": model_record.extra
|
||||
return _hybrid_get_instance_models(provider_name, instance_obj.id)
|
||||
|
||||
|
||||
def update_instance_models(tenant_id: str, provider_name: str, instance_name: str, model_names: list, model_types: list):
|
||||
if not model_names or not model_types:
|
||||
return False, "model_name and model_type are required"
|
||||
|
||||
provider_obj = TenantModelProviderService.get_by_tenant_id_and_provider_name(tenant_id, provider_name)
|
||||
if not provider_obj:
|
||||
return False, f"No provider found for provider '{provider_name}'"
|
||||
instance_obj = TenantModelInstanceService.get_by_provider_id_and_instance_name(provider_obj.id, instance_name)
|
||||
if not instance_obj:
|
||||
return False, f"No instance found for provider '{provider_name}' and instance '{instance_name}'"
|
||||
|
||||
found, models = _hybrid_get_instance_models(provider_name, instance_obj.id)
|
||||
if not found:
|
||||
return False, models
|
||||
|
||||
model_info_map = {model["name"]: model for model in models}
|
||||
not_exist_models = set(model_names) - set(model_info_map.keys())
|
||||
if not_exist_models:
|
||||
return False, f"Models {not_exist_models} not found for provider '{provider_name}' and instance '{instance_name}'"
|
||||
for model_name in model_names:
|
||||
model_info = model_info_map.get(model_name, {})
|
||||
TenantModelService.upsert_model_type(
|
||||
provider_obj.id,
|
||||
instance_obj.id,
|
||||
model_name,
|
||||
{
|
||||
"add": list(set(model_types) - set(model_info["model_type"])),
|
||||
"delete": list(set(model_info["model_type"]) - set(model_types))
|
||||
}
|
||||
|
||||
# List all models from the LLM dictionary for this provider
|
||||
factory_info = [f for f in FACTORY_LLM_INFOS if f["name"] == provider_name]
|
||||
if not factory_info:
|
||||
return False, f"Provider '{provider_name}' not found"
|
||||
|
||||
llms = factory_info[0].get("llm", [])
|
||||
models = []
|
||||
for llm in llms:
|
||||
models.append({
|
||||
"name": llm["llm_name"],
|
||||
"model_type": list(
|
||||
dict.fromkeys(_factory_model_types(llm) + model_info_map.get(llm["llm_name"], {}).get("model_type", []))
|
||||
),
|
||||
"max_tokens": llm.get("max_tokens"),
|
||||
"status": model_info_map.get(llm["llm_name"], {}).get("status", "active"),
|
||||
})
|
||||
factory_models = [m["name"] for m in models]
|
||||
for model_name, model_info_dict in model_info_map.items():
|
||||
if model_name not in factory_models:
|
||||
extra_fields = json.loads(model_info_dict["extra"]) if model_info_dict["extra"] else {}
|
||||
models.append({
|
||||
"name": model_name,
|
||||
"model_type": model_info_dict["model_type"],
|
||||
"max_tokens": extra_fields.get("max_tokens", 8192),
|
||||
"status": model_info_dict["status"],
|
||||
})
|
||||
return True, models
|
||||
)
|
||||
return True, "success"
|
||||
|
||||
|
||||
def add_model_to_instance(tenant_id: str, provider_name: str, instance_name: str, model_name: str, model_type: str|list[str], max_tokens: int=8192, extra: dict=None):
|
||||
@@ -742,7 +783,7 @@ def update_model_status(tenant_id: str, provider_name: str, instance_name: str,
|
||||
|
||||
if model_obj_list:
|
||||
# Model record exists — update its status
|
||||
TenantModelService.batch_update_model_status([m.id for m in model_obj_list], status)
|
||||
TenantModelService.batch_update_model_status([m.id for m in model_obj_list if m.status != ActiveStatusEnum.UNSUPPORTED.value], status)
|
||||
else:
|
||||
# Model record does not exist
|
||||
if status == ActiveStatusEnum.ACTIVE.value:
|
||||
|
||||
Reference in New Issue
Block a user