导入整个目录
(self, dir_path: str, workflow_id: str | None = None)
| 327 | return document.id |
| 328 | |
| 329 | async def ingest_directory(self, dir_path: str, workflow_id: str | None = None) -> list[str]: |
| 330 | """导入整个目录""" |
| 331 | logger.info(f"Ingesting directory: {dir_path}") |
| 332 | |
| 333 | # 支持的文件扩展名 |
| 334 | supported_extensions = { |
| 335 | ".pdf", |
| 336 | ".doc", |
| 337 | ".docx", |
| 338 | ".md", |
| 339 | ".markdown", |
| 340 | ".html", |
| 341 | ".htm", |
| 342 | ".txt", |
| 343 | } |
| 344 | |
| 345 | # 扫描目录 |
| 346 | file_paths = [] |
| 347 | for root, _, files in os.walk(dir_path): |
| 348 | for file in files: |
| 349 | file_path = os.path.join(root, file) |
| 350 | if Path(file_path).suffix.lower() in supported_extensions: |
| 351 | file_paths.append(file_path) |
| 352 | |
| 353 | logger.info(f"Found {len(file_paths)} supported files") |
| 354 | |
| 355 | # 批量导入 |
| 356 | doc_ids = [] |
| 357 | for file_path in file_paths: |
| 358 | try: |
| 359 | doc_id = await self.ingest_file(file_path, workflow_id) |
| 360 | doc_ids.append(doc_id) |
| 361 | except Exception as e: |
| 362 | logger.error(f"Failed to ingest {file_path}: {str(e)}") |
| 363 | continue |
| 364 | |
| 365 | logger.info(f"Successfully ingested {len(doc_ids)} files from {dir_path}") |
| 366 | return doc_ids |
| 367 | |
| 368 | async def create_knowledge_base( |
| 369 | self, |
no test coverage detected