RAG-Anything:PDF、表格、公式全能讀!港大開源神器讓AI真正理解復雜文檔? 精華
?RAG-Anything是一個由香港大學數據智能實驗室開發的開源多模態RAG系統,支持處理包含文本、圖像、表格和公式的復雜文檔,提供從文檔攝取到智能查詢的端到端解決方案。系統基于多模態知識圖譜、靈活的解析架構和混合檢索機制,顯著提升復雜文檔處理能力,支持多種文檔格式,如PDF、Office文檔、圖像和文本文件等。
一、技術原理
(一)圖增強文本索引
RAG-Anything基于LLM從文本中提取實體(節點)及其關系(邊),將信息用于構建知識圖譜。為每個實體節點和關系邊生成文本鍵值對,鍵是用于高效檢索的單詞或短語,值是總結相關外部數據片段的文本段落。識別、合并來自不同文本片段的相同實體和關系,減少圖操作的開銷,提高數據處理效率。
(二)雙重檢索范式
- 低層次檢索:專注于檢索特定實體及其屬性或關系,適用需要精確信息的詳細查詢。
- 高層次檢索:處理更廣泛的主題和主題,基于聚合多個相關實體和關系的信息,提供對高級概念和總結的見解。
- 圖和向量集成:結合圖結構和向量表示,檢索算法用局部和全局關鍵詞,提高檢索效率和結果相關性。
(三)檢索增強型答案生成
用檢索到的信息,基于LLM生成基于收集數據的答案,包括實體和關系的名稱、描述及原始文本片段。將查詢與多源文本統一,LLM生成與用戶需求一致的答案,確保與查詢意圖對齊。
(四)復雜性分析
圖基索引階段用LLM從每個文本塊中提取實體和關系,無需額外開銷,高效管理新文本更新。圖基檢索階段用LLM生成相關關鍵詞,依靠向量搜索進行檢索,顯著減少檢索開銷。
二、主要功能
(一)端到端多模態流水線
從文檔解析到多模態智能查詢,提供一體化工作流程。系統能夠自動識別文檔中的不同模態內容,并將其路由到相應的處理通道,實現高效的內容解析和理解。
(二)多格式文檔支持
兼容PDF、Office文檔(DOC/DOCX、PPT/PPTX、XLS/XLSX)、圖像(JPG、PNG等)和文本文件(TXT、MD)。無論用戶手中的文檔是何種格式,RAG-Anything都能輕松應對,滿足不同場景下的文檔處理需求。
(三)多模態內容分析引擎
針對圖像、表格、公式和通用文本內容部署專門的處理器,確保各類內容的精準解析。例如,視覺內容分析器能夠生成基于視覺語義的上下文感知描述性標題,并提取視覺元素之間的空間關系和層次結構;結構化數據解釋器則對表格和結構化數據格式進行系統解釋,識別數據趨勢和語義關系。
(四)知識圖譜索引
自動提取實體和跨模態關系,構建語義連接網絡。通過構建多模態知識圖譜,系統能夠更好地理解文檔內容之間的關聯,為智能問答提供豐富的語義信息支持。
(五)靈活的處理架構
支持MinerU智能解析模式和直接多模態內容插入模式,適配多樣化場景。用戶可以根據自己的需求選擇合適的處理模式,無論是對復雜文檔的深度解析,還是對特定模態內容的快速處理,RAG-Anything都能提供靈活的解決方案。
(六)跨模態檢索機制
實現跨文本和多模態內容的智能檢索,提供精準的信息定位和匹配能力。用戶可以通過輸入文本查詢,系統能夠理解查詢意圖,并在多模態文檔中檢索出與之相關的信息,無論是文本描述、圖像內容還是表格數據,都能精準匹配。
三、應用場景
(一)學術研究
快速解析和理解大量學術文獻,提取關鍵信息和研究結果,支持文獻綜述和實驗數據分析,助力跨學科研究。研究人員可以利用RAG-Anything高效地處理學術論文,快速獲取所需信息,加速研究進程。
(二)企業知識管理
整合企業內部文檔,如會議記錄、項目報告等,提供智能查詢和知識共享,提升內部信息流通效率。企業員工可以通過RAG-Anything快速查找所需資料,促進知識共享和團隊協作。
(三)金融分析
處理財務報表和市場研究報告,提取關鍵財務指標和市場趨勢,輔助風險評估和投資決策。金融分析師可以借助RAG-Anything深入分析金融文檔,挖掘有價值的信息,為投資決策提供有力支持。
(四)醫療健康
解析病歷中的文本、圖像和表格,支持醫療診斷和治療方案制定,處理醫學研究文獻和實驗數據。醫療專業人員可以利用RAG-Anything高效地處理醫療文檔,提高診斷準確性和治療效果。
(五)智能客服
快速回答客戶問題,提高客服效率,整合企業知識庫,提供智能查詢和知識推薦,優化客戶體驗。客服人員可以借助RAG-Anything快速準確地回答客戶咨詢,提升客戶滿意度。
四、快速使用
(一)安裝
1、從PyPI安裝(推薦)
#
基本安裝
pip install raganything
#
安裝所有可選依賴
pip install 'raganything[all]'2、從源碼安裝
git clone https://github.com/HKUDS/RAG-Anything.git
cd RAG-Anything
pip install -e .(二)使用示例
1、端到端文檔處理
import asyncio
from raganything import RAGAnything, RAGAnythingConfig
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
from lightrag.utils import EmbeddingFunc
async def main():
# 設置API配置
api_key = "your-api-key"
base_url = "your-base-url" # 可選
# 創建RAGAnything配置
config = RAGAnythingConfig(
working_dir="./rag_storage",
mineru_parse_method="auto",
enable_image_processing=True,
enable_table_processing=True,
enable_equation_processing=True,
)
# 定義LLM模型函數
def llm_model_func(prompt, system_prompt=None, history_messages=[], **kwargs):
return openai_complete_if_cache(
"gpt-4o-mini",
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
api_key=api_key,
base_url=base_url,
**kwargs,
)
# 定義視覺模型函數用于圖像處理
def vision_model_func(
prompt, system_prompt=None, history_messages=[], image_data=None, **kwargs
):
if image_data:
return openai_complete_if_cache(
"gpt-4o",
"",
system_prompt=None,
history_messages=[],
messages=[
{"role": "system", "content": system_prompt}
if system_prompt
else None,
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_data}"
},
},
],
}
if image_data
else {"role": "user", "content": prompt},
],
api_key=api_key,
base_url=base_url,
**kwargs,
)
else:
return llm_model_func(prompt, system_prompt, history_messages, **kwargs)
# 定義嵌入函數
embedding_func = EmbeddingFunc(
embedding_dim=3072,
max_token_size=8192,
func=lambda texts: openai_embed(
texts,
model="text-embedding-3-large",
api_key=api_key,
base_url=base_url,
),
)
# 初始化RAGAnything
rag = RAGAnything(
cnotallow=config,
llm_model_func=llm_model_func,
vision_model_func=vision_model_func,
embedding_func=embedding_func,
)
# 處理文檔
await rag.process_document_complete(
file_path="path/to/your/document.pdf",
output_dir="./output",
parse_method="auto"
)
# 查詢處理后的內容
# 純文本查詢 - 用于基本知識庫搜索
text_result = await rag.aquery(
"What are the main findings shown in the figures and tables?",
mode="hybrid"
)
print("Text query result:", text_result)
# 多模態查詢,包含特定多模態內容
multimodal_result = await rag.aquery_with_multimodal(
"Explain this formula and its relevance to the document content",
multimodal_cnotallow=[{
"type": "equation",
"latex": "P(d|q) = \\frac{P(q|d) \\cdot P(d)}{P(q)}",
"equation_caption": "Document relevance probability"
}],
mode="hybrid"
)
print("Multimodal query result:", multimodal_result)
if __name__ == "__main__":
asyncio.run(main())2、直接多模態內容處理
import asyncio
from lightrag import LightRAG
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
from lightrag.utils import EmbeddingFunc
from raganything.modalprocessors import ImageModalProcessor, TableModalProcessor
async def process_multimodal_content():
# 設置API配置
api_key = "your-api-key"
base_url = "your-base-url" # 可選
# 初始化LightRAG
rag = LightRAG(
working_dir="./rag_storage",
llm_model_func=lambda prompt, system_prompt=None, history_messages=[], **kwargs: openai_complete_if_cache(
"gpt-4o-mini",
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
api_key=api_key,
base_url=base_url,
**kwargs,
),
embedding_func=EmbeddingFunc(
embedding_dim=3072,
max_token_size=8192,
func=lambda texts: openai_embed(
texts,
model="text-embedding-3-large",
api_key=api_key,
base_url=base_url,
),
)
)
await rag.initialize_storages()
# 處理圖像
image_processor = ImageModalProcessor(
lightrag=rag,
modal_caption_func=lambda prompt, 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} if system_prompt else None,
{"role": "user", "content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}
]} if image_data else {"role": "user", "content": prompt}
],
api_key=api_key,
base_url=base_url,
**kwargs,
) if image_data else openai_complete_if_cache(
"gpt-4o-mini",
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
api_key=api_key,
base_url=base_url,
**kwargs,
)
)
image_content = {
"img_path": "path/to/image.jpg",
"img_caption": ["Figure 1: Experimental results"],
"img_footnote": ["Data collected in 2024"]
}
description, entity_info = await image_processor.process_multimodal_content(
modal_cnotallow=image_content,
content_type="image",
file_path="research_paper.pdf",
entity_name="Experimental Results Figure"
)
# 處理表格
table_processor = TableModalProcessor(
lightrag=rag,
modal_caption_func=lambda prompt, system_prompt=None, history_messages=[], **kwargs: openai_complete_if_cache(
"gpt-4o-mini",
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
api_key=api_key,
base_url=base_url,
**kwargs,
)
)
table_content = {
"table_body": """
| Method | Accuracy | F1-Score |
|--------|----------|----------|
| RAGAnything | 95.2% | 0.94 |
| Baseline | 87.3% | 0.85 |
""",
"table_caption": ["Performance Comparison"],
"table_footnote": ["Results on test dataset"]
}
description, entity_info = await table_processor.process_multimodal_content(
modal_cnotallow=table_content,
content_type="table",
file_path="research_paper.pdf",
entity_name="Performance Results Table"
)
if __name__ == "__main__":
asyncio.run(process_multimodal_content())結語
RAG-Anything作為香港大學數據智能實驗室推出的開源多模態RAG系統,憑借其創新的技術架構和強大的功能,為多模態文檔處理與智能問答領域帶來了新的突破。它不僅支持多種文檔格式,還能精準解析圖像、表格、公式等多模態內容,并通過構建知識圖譜實現跨模態的智能檢索與問答。
GitHub倉庫:https://github.com/HKUDS/RAG-Anything
arXiv技術論文:?https://arxiv.org/pdf/2410.05779
本文轉載自???????小兵的AI視界???????,作者:AGI小兵

















