Initial commit: 国土空间规划课程智能体 v1.0
单容器 Docker 架构的国土空间规划课程智能问答系统,集成 FastAPI 后端与 Next.js 前端。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
LangChain 1.0 文档加载器封装
|
||||
"""
|
||||
from typing import List, Optional
|
||||
from pathlib import Path
|
||||
from langchain_community.document_loaders import (
|
||||
PyPDFLoader,
|
||||
Docx2txtLoader,
|
||||
TextLoader,
|
||||
UnstructuredMarkdownLoader
|
||||
)
|
||||
from langchain_core.documents import Document
|
||||
|
||||
class DocumentLoaderFactory:
|
||||
"""文档加载器工厂"""
|
||||
|
||||
@staticmethod
|
||||
def get_loader(file_path: str, file_type: str):
|
||||
"""根据文件类型获取对应的加载器"""
|
||||
loaders = {
|
||||
".pdf": PyPDFLoader,
|
||||
".docx": Docx2txtLoader,
|
||||
".txt": TextLoader,
|
||||
".md": UnstructuredMarkdownLoader,
|
||||
}
|
||||
|
||||
loader_class = loaders.get(file_type)
|
||||
if not loader_class:
|
||||
raise ValueError(f"Unsupported file type: {file_type}")
|
||||
|
||||
return loader_class(file_path)
|
||||
|
||||
@staticmethod
|
||||
def load_document(file_path: str, file_type: str, metadata: Optional[dict] = None) -> List[Document]:
|
||||
"""加载文档并添加元数据"""
|
||||
loader = DocumentLoaderFactory.get_loader(file_path, file_type)
|
||||
documents = loader.load()
|
||||
|
||||
if metadata:
|
||||
for doc in documents:
|
||||
doc.metadata.update(metadata)
|
||||
|
||||
return documents
|
||||
Reference in New Issue
Block a user