从文件加载文档内容
(self, file_path: str)
| 101 | return hashlib.md5(content.encode()).hexdigest() |
| 102 | |
| 103 | async def _load_document_from_file(self, file_path: str) -> str | None: |
| 104 | """从文件加载文档内容""" |
| 105 | try: |
| 106 | file_ext = Path(file_path).suffix.lower() |
| 107 | |
| 108 | # 根据文件类型选择加载器 |
| 109 | if file_ext == ".pdf": |
| 110 | loader = PyPDFLoader(file_path) |
| 111 | elif file_ext in [".doc", ".docx"]: |
| 112 | loader = UnstructuredWordDocumentLoader(file_path) |
| 113 | elif file_ext in [".md", ".markdown"]: |
| 114 | loader = UnstructuredMarkdownLoader(file_path) |
| 115 | elif file_ext in [".html", ".htm"]: |
| 116 | loader = UnstructuredHTMLLoader(file_path) |
| 117 | elif file_ext in [".txt"]: |
| 118 | loader = TextLoader(file_path, encoding="utf-8") |
| 119 | else: |
| 120 | logger.warning(f"Unsupported file type: {file_ext}") |
| 121 | return None |
| 122 | |
| 123 | # 加载文档 |
| 124 | documents = loader.load() |
| 125 | if documents: |
| 126 | # 合并所有页面 |
| 127 | content = "\n\n".join([doc.page_content for doc in documents]) |
| 128 | return content |
| 129 | |
| 130 | return None |
| 131 | |
| 132 | except Exception as e: |
| 133 | logger.error(f"Failed to load document {file_path}: {str(e)}") |
| 134 | return None |
| 135 | |
| 136 | async def _load_document_from_url(self, url: str) -> str | None: |
| 137 | """从URL加载文档内容""" |
no test coverage detected