批量创建数据训练记录(复用单条插入逻辑)
(session: SessionDep, info_list: List[DataTrainingInfo], oid: int, trans: Trans)
| 260 | |
| 261 | |
| 262 | def batch_create_training(session: SessionDep, info_list: List[DataTrainingInfo], oid: int, trans: Trans): |
| 263 | """ |
| 264 | 批量创建数据训练记录(复用单条插入逻辑) |
| 265 | """ |
| 266 | if not info_list: |
| 267 | return { |
| 268 | 'success_count': 0, |
| 269 | 'failed_records': [], |
| 270 | 'duplicate_count': 0, |
| 271 | 'original_count': 0, |
| 272 | 'deduplicated_count': 0 |
| 273 | } |
| 274 | |
| 275 | failed_records = [] |
| 276 | success_count = 0 |
| 277 | inserted_ids = [] |
| 278 | |
| 279 | # 第一步:数据去重 |
| 280 | unique_records = {} |
| 281 | duplicate_records = [] |
| 282 | |
| 283 | for info in info_list: |
| 284 | # 创建唯一标识 |
| 285 | unique_key = ( |
| 286 | info.question.strip().lower() if info.question else "", |
| 287 | info.datasource_name.strip().lower() if info.datasource_name else "", |
| 288 | info.advanced_application_name.strip().lower() if info.advanced_application_name else "" |
| 289 | ) |
| 290 | |
| 291 | if unique_key in unique_records: |
| 292 | duplicate_records.append(info) |
| 293 | else: |
| 294 | unique_records[unique_key] = info |
| 295 | |
| 296 | # 将去重后的数据转换为列表 |
| 297 | deduplicated_list = list(unique_records.values()) |
| 298 | |
| 299 | # 预加载数据源和高级应用名称到ID的映射 |
| 300 | datasource_name_to_id = {} |
| 301 | datasource_stmt = select(CoreDatasource.id, CoreDatasource.name).where(CoreDatasource.oid == oid) |
| 302 | datasource_result = session.execute(datasource_stmt).all() |
| 303 | for ds in datasource_result: |
| 304 | datasource_name_to_id[ds.name.strip()] = ds.id |
| 305 | |
| 306 | assistant_name_to_id = {} |
| 307 | |
| 308 | assistant_stmt = select(AssistantModel.id, AssistantModel.name).where(and_(AssistantModel.type == 1, AssistantModel.oid == oid)) |
| 309 | assistant_result = session.execute(assistant_stmt).all() |
| 310 | for assistant in assistant_result: |
| 311 | assistant_name_to_id[assistant.name.strip()] = assistant.id |
| 312 | |
| 313 | # 验证和转换数据 |
| 314 | valid_records = [] |
| 315 | for info in deduplicated_list: |
| 316 | error_messages = [] |
| 317 | |
| 318 | # 基本验证 |
| 319 | if not info.question or not info.question.strip(): |
no test coverage detected