feat: 多文件处理

This commit is contained in:
李如威 2025-08-08 15:33:35 +08:00
parent af0ac9605b
commit 97498e2fd7
5 changed files with 38 additions and 12 deletions

View File

@ -37,9 +37,11 @@ class MultiFormatRAG(BaseRAG):
contexts = [] contexts = []
for doc in docs: for doc in docs:
source = doc.metadata.get("source_file", "未知来源") source = doc.metadata.get("source_file", "未知来源")
content = doc.page_content.strip()
if source not in sources: if source not in sources:
sources.append(source) sources.append(source)
contexts.append(doc.page_content.strip()) contexts.append(content)
context = "\n\n".join(contexts) context = "\n\n".join(contexts)
sources_str = "".join(sources) sources_str = "".join(sources)
@ -56,8 +58,8 @@ async def test_multiple_formats():
rag = MultiFormatRAG( rag = MultiFormatRAG(
vector_store_name="multiformat_kb", vector_store_name="multiformat_kb",
retriever_top_k=3, retriever_top_k=3,
storage_directory="./test_files", storage_directory="../test_files", # 相对于examples目录
status_db_path="./status.db", status_db_path="../status.db", # 相对于examples目录
) )
# 测试文件列表 # 测试文件列表
@ -92,7 +94,7 @@ async def test_multiple_formats():
format_type = file_info["format"] format_type = file_info["format"]
description = file_info["description"] description = file_info["description"]
file_path = Path("./test_files") / filename file_path = Path("../test_files") / filename
if not file_path.exists(): if not file_path.exists():
print(f"{format_type}: {filename} - 文件不存在") print(f"{format_type}: {filename} - 文件不存在")
@ -129,14 +131,23 @@ async def test_multiple_formats():
try: try:
answer = await rag.query(query) answer = await rag.query(query)
if "抱歉" not in answer: if "抱歉" not in answer:
# 显示简化的回答 # 分离来源信息和内容
lines = answer.split('\n') parts = answer.split('\n\n', 1)
source_line = next((line for line in lines if line.startswith('基于')), "") if len(parts) == 2:
content_lines = [line for line in lines if line.strip() and not line.startswith('基于')] source_info = parts[0] # "基于以下文档..."
if content_lines: content = parts[1] # 实际内容
print(f" 💡 {content_lines[0][:100]}...")
if source_line: print(f" 📚 {source_info}")
print(f" 📚 {source_line}")
# 显示内容摘要前200字符
if len(content) > 200:
content_preview = content[:200] + "..."
else:
content_preview = content
print(f" 💡 {content_preview}")
else:
print(f" 💡 {answer}")
else: else:
print(f" 💡 {answer}") print(f" 💡 {answer}")
except Exception as e: except Exception as e:

BIN
status.db

Binary file not shown.

View File

@ -0,0 +1,5 @@
NumPy是Python中用于科学计算的基础库提供多维数组对象。
Pandas是强大的数据分析和处理库提供DataFrame数据结构。
Matplotlib是Python的绘图库用于创建静态、动态和交互式图表。
Scikit-learn是机器学习库提供各种算法和工具。

View File

@ -0,0 +1,5 @@
Python是一种高级编程语言由Guido van Rossum于1991年创建。
Python具有简洁易读的语法适合初学者学习编程。
Python是解释型语言支持面向对象、函数式等多种编程范式。
Python的设计哲学强调代码的可读性和简洁性。

View File

@ -0,0 +1,5 @@
Flask是一个轻量级的Python Web框架易于学习和使用。
Django是一个功能丰富的Python Web框架适合大型项目开发。
FastAPI是现代的Python Web框架专为构建API而设计。
Tornado是一个可扩展的非阻塞Web服务器和Web应用框架。