RAG-Anything采用分层架构,通过多阶段流水线扩展传统RAG,处理异构内容。其工作流程包括文档解析、内容分析、知识图谱构建和智能检索。
1、文档解析阶段
目标:从多种格式文档中提取和结构化多模态元素(文本、图像、表格、公式)。
核心组件:
2、多模态内容理解
目标:通过专用流水线并行处理异构内容,确保高效和完整性。
核心组件:
3、多模态分析引擎
目标:为不同模态内容部署专用处理器,确保精准解析。
核心组件:
4、多模态知识图谱索引
目标:将文档内容转化为结构化语义表示,提升检索效率。
核心功能:
5、模态感知检索
目标:实现精准、上下文感知的多模态检索。
核心机制:
RAG-Anything 的安装方式非常简单,支持从PyPI安装(推荐)或者源码部署。
1、从PyPI安装(推荐)
pipinstallraganything
2、从源码安装
gitclonehttps://github.com/HKUDS/RAG-Anything.git
cdRAG-Anything
pip install -e .然后需要检查MinerU是否安装:
# 验证安装
mineru --version
# 检查是否正确配置
python -c"from raganything import RAGAnything; rag = RAGAnything(); print('✅ MinerU安装正常' if rag.check_mineru_installation() else '❌ MinerU安装有问题')"模型在首次使用时会自动下载。
Python使用示例
Demo01:端到端文档处理
importasyncio
fromraganythingimportRAGAnything
fromlightrag.llm.openaiimportopenai_complete_if_cache, openai_embed
asyncdefmain():
# 初始化RAGAnything
rag = RAGAnything(
working_dir="./rag_storage",
llm_model_func=lambdaprompt, system_prompt=None, history_messages=[], **kwargs: openai_complete_if_cache(
"gpt-4o-mini",
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
api_key="your-api-key",
**kwargs,
),
vision_model_func=lambdaprompt, system_prompt=None, history_messages=[], image_data=None, **kwargs: openai_complete_if_cache(
"gpt-4o",
"",
system_prompt=None,
history_messages=[],
messages=[
{"role":"system","content": system_prompt}ifsystem_promptelseNone,
{"role":"user","content": [
{"type":"text","text": prompt},
{"type":"image_url","image_url": {"url":f"data:image/jpeg;base64,{image_data}"}}
]}ifimage_dataelse{"role":"user","content": prompt}
],
api_key="your-api-key",
**kwargs,
)ifimage_dataelseopenai_complete_if_cache(
"gpt-4o-mini",
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
api_key="your-api-key",
**kwargs,
),
embedding_func=lambdatexts: openai_embed(
texts,
model="text-embedding-3-large",
api_key="your-api-key",
),
embedding_dim=3072,
max_token_size=8192
)
# 处理文档
awaitrag.process_document_complete(
file_path="path/to/your/document.pdf",
output_dir="./output",
parse_method="auto"
)
# 查询处理后的内容
result =awaitrag.query_with_multimodal(
"图表中显示的主要发现是什么?",
mode="hybrid"
)
print(result)
if__name__ =="__main__":
asyncio.run(main())Demo02:直接多模态内容处理
importasyncio
fromlightragimportLightRAG
fromraganything.modalprocessorsimportImageModalProcessor, TableModalProcessor
asyncdefprocess_multimodal_content():
# 初始化LightRAG
rag = LightRAG(
working_dir="./rag_storage",
# ... 你的LLM和嵌入配置
)
awaitrag.initialize_storages()
# 处理图像
image_processor = ImageModalProcessor(
lightrag=rag,
modal_caption_func=your_vision_model_func
)
image_content = {
"img_path":"path/to/image.jpg",
"img_caption": ["图1:实验结果"],
"img_footnote": ["数据收集于2024年"]
}
description, entity_info =awaitimage_processor.process_multimodal_content(
modal_content=image_content,
content_type="image",
file_path="research_paper.pdf",
entity_name="实验结果图表"
)
# 处理表格
table_processor = TableModalProcessor(
lightrag=rag,
modal_caption_func=your_llm_model_func
)
table_content = {
"table_body":"""
| 方法 | 准确率 | F1分数 |
|------|--------|--------|
| RAGAnything | 95.2% | 0.94 |
| 基准方法 | 87.3% | 0.85 |
""",
"table_caption": ["性能对比"],
"table_footnote": ["测试数据集结果"]
}
description, entity_info =awaittable_processor.process_multimodal_content(
modal_content=table_content,
content_type="table",
file_path="research_paper.pdf",
entity_name="性能结果表格"
)
if__name__ =="__main__":
asyncio.run(process_multimodal_content())Demo03:批量处理
# 处理多个文档
awaitrag.process_folder_complete(
folder_path="./documents",
output_dir="./output",
file_extensions=[".pdf",".docx",".pptx"],
recursive=True,
max_workers=4
)Demo04:自定义多模态处理器
fromraganything.modalprocessorsimportGenericModalProcessor
classCustomModalProcessor(GenericModalProcessor):
asyncdefprocess_multimodal_content(self, modal_content, content_type, file_path, entity_name):
# 你的自定义处理逻辑
enhanced_description =awaitself.analyze_custom_content(modal_content)
entity_info =self.create_custom_entity(enhanced_description, entity_name)
returnawaitself._create_entity_and_chunk(enhanced_description, entity_info, file_path)Demo05:查询选项
# 不同的查询模式
result_hybrid =awaitrag.query_with_multimodal("你的问题", mode="hybrid")
result_local =awaitrag.query_with_multimodal("你的问题", mode="local")
result_global =awaitrag.query_with_multimodal("你的问题", mode="global")项目目录下也有相应的实际场景演示,examples/ 目录包含完整的使用示例:
raganything_example.py:基于MinerU的端到端文档处理
modalprocessors_example.py:直接多模态内容处理
office_document_test.py:Office文档解析测试(无需API密钥)
image_format_test.py:图像格式解析测试(无需API密钥)
text_format_test.py:文本格式解析测试(无需API密钥)在 RAG 系统百花齐放的今天,RAG-Anything是少有的“真正做全”的开源RAG系统之一。
从结构化提取到多模态融合,从问答到检索,它不仅支持多种文档格式,而且能智能分析、构建知识图谱、并在上下文语义层面实现真正的信息理解与调用。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |