mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-29 04:08:12 +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:
@@ -214,6 +214,8 @@ def get_model_config_from_provider_instance(tenant_id, model_type: str|enum.Enum
|
||||
if model_obj:
|
||||
if model_obj.status == ActiveStatusEnum.INACTIVE.value:
|
||||
raise LookupError(f"Model {model_name} is disabled.")
|
||||
if model_obj.status == ActiveStatusEnum.UNSUPPORTED.value:
|
||||
raise LookupError(f"Model {model_name} cannot be used as {model_type_val} model.")
|
||||
|
||||
model_extra = json.loads(model_obj.extra) if model_obj.extra else {}
|
||||
model_config = {
|
||||
@@ -281,6 +283,7 @@ def get_model_type_by_name(tenant_id: str, model_name: str):
|
||||
if not instance_obj:
|
||||
raise LookupError(f"Instance {instance_name} not found for model {model_name}.")
|
||||
model_objs = TenantModelService.get_by_provider_id_and_instance_id_and_model_name(provider_obj.id, instance_obj.id, pure_model_name)
|
||||
types_in_json = []
|
||||
if not model_objs:
|
||||
extra_fields = json.loads(instance_obj.extra) if instance_obj.extra else {}
|
||||
region = extra_fields.get("region", "default")
|
||||
@@ -294,8 +297,8 @@ def get_model_type_by_name(tenant_id: str, model_name: str):
|
||||
llm_list = [llm for llm in fac_list[0]["llm"] if llm["llm_name"] == pure_model_name]
|
||||
if not llm_list:
|
||||
raise LookupError(f"Model {pure_model_name} not found for model {model_name}.")
|
||||
return _factory_model_types(llm_list[0])
|
||||
return [model_obj.model_type for model_obj in model_objs]
|
||||
types_in_json = _factory_model_types(llm_list[0])
|
||||
return list(set(types_in_json + [model_obj.model_type for model_obj in model_objs if model_obj.status != ActiveStatusEnum.UNSUPPORTED.value]) - {model_obj.model_type for model_obj in model_objs if model_obj.status == ActiveStatusEnum.UNSUPPORTED.value})
|
||||
|
||||
|
||||
def delete_models_by_instance_ids(instance_ids: list[str]):
|
||||
@@ -329,6 +332,7 @@ def get_models_by_tenant_and_provider_and_model_type(tenant_id: str, provider_na
|
||||
results = []
|
||||
for inst in instances:
|
||||
models = TenantModelService.get_by_provider_id_and_instance_id_and_model_type(provider_obj.id, inst.id, model_type)
|
||||
if models:
|
||||
results.extend(models)
|
||||
supported = [model for model in models if model.status != ActiveStatusEnum.UNSUPPORTED.value]
|
||||
if supported:
|
||||
results.extend(supported)
|
||||
return results
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
from common.constants import ActiveStatusEnum
|
||||
from api.db.db_models import DB, TenantModel
|
||||
from api.db.services.common_service import CommonService
|
||||
|
||||
@@ -59,6 +60,63 @@ class TenantModelService(CommonService):
|
||||
def batch_update_model_status(cls, model_ids, status):
|
||||
return cls.model.update(status=status).where(cls.model.id.in_(model_ids)).execute()
|
||||
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
def upsert_model_type(cls, provider_id: str, instance_id: str, model_name: str, operation: dict):
|
||||
model_type_records = cls.model.select().where(cls.model.provider_id == provider_id, cls.model.instance_id == instance_id, cls.model.model_name == model_name)
|
||||
if not model_type_records:
|
||||
for _type in operation.get("add", []):
|
||||
cls.insert(
|
||||
model_name=model_name,
|
||||
provider_id=provider_id,
|
||||
instance_id=instance_id,
|
||||
model_type=_type,
|
||||
extra="{}"
|
||||
)
|
||||
for _type in operation.get("delete", []):
|
||||
cls.insert(
|
||||
model_name=model_name,
|
||||
provider_id=provider_id,
|
||||
instance_id=instance_id,
|
||||
model_type=_type,
|
||||
status=ActiveStatusEnum.UNSUPPORTED,
|
||||
extra="{}"
|
||||
)
|
||||
return len(operation.get("add", [])) + len(operation.get("delete", []))
|
||||
model_record_example = [model_record for model_record in model_type_records if model_record.status != ActiveStatusEnum.UNSUPPORTED]
|
||||
extra_fields = model_record_example[0].extra if model_record_example else "{}"
|
||||
model_status = model_record_example[0].status if model_record_example else ActiveStatusEnum.ACTIVE.value
|
||||
type_record_map = {record.model_type: record for record in model_type_records}
|
||||
operated_cnt = 0
|
||||
for _type in operation.get("add", []):
|
||||
if type_record_map.get(_type):
|
||||
cls.update_by_id(type_record_map[_type].id, {"status": model_status})
|
||||
|
||||
else:
|
||||
cls.insert(
|
||||
model_name=model_name,
|
||||
provider_id=provider_id,
|
||||
instance_id=instance_id,
|
||||
model_type=_type,
|
||||
status=model_status,
|
||||
extra=extra_fields
|
||||
)
|
||||
operated_cnt += 1
|
||||
for _type in operation.get("delete", []):
|
||||
if type_record_map.get(_type):
|
||||
cls.update_by_id(type_record_map[_type].id, {"status": ActiveStatusEnum.UNSUPPORTED.value})
|
||||
else:
|
||||
cls.insert(
|
||||
model_name=model_name,
|
||||
provider_id=provider_id,
|
||||
instance_id=instance_id,
|
||||
model_type=_type,
|
||||
status=ActiveStatusEnum.UNSUPPORTED.value,
|
||||
extra=extra_fields
|
||||
)
|
||||
operated_cnt += 1
|
||||
return operated_cnt
|
||||
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
def delete_by_id(cls, model_id):
|
||||
|
||||
Reference in New Issue
Block a user