2025-06-05 17:36:05 +08:00
|
|
|
"""
|
|
|
|
Example of directly using modal processors
|
|
|
|
|
|
|
|
This example demonstrates how to use LightRAG's modal processors directly without going through MinerU.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import argparse
|
|
|
|
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
|
|
|
from lightrag.kg.shared_storage import initialize_pipeline_status
|
|
|
|
from lightrag import LightRAG
|
2025-06-17 01:16:02 +08:00
|
|
|
from raganything.modalprocessors import (
|
2025-06-05 17:36:05 +08:00
|
|
|
ImageModalProcessor,
|
|
|
|
TableModalProcessor,
|
|
|
|
EquationModalProcessor,
|
|
|
|
)
|
|
|
|
|
|
|
|
WORKING_DIR = "./rag_storage"
|
|
|
|
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
def get_llm_model_func(api_key: str, base_url: str = None):
|
2025-06-05 17:37:11 +08:00
|
|
|
return (
|
|
|
|
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,
|
|
|
|
)
|
2025-06-05 17:36:05 +08:00
|
|
|
)
|
|
|
|
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
def get_vision_model_func(api_key: str, base_url: str = None):
|
2025-06-05 17:37:11 +08:00
|
|
|
return (
|
|
|
|
lambda prompt,
|
2025-06-05 17:36:05 +08:00
|
|
|
system_prompt=None,
|
|
|
|
history_messages=[],
|
2025-06-05 17:37:11 +08:00
|
|
|
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,
|
2025-06-05 17:36:05 +08:00
|
|
|
{
|
2025-06-05 17:37:11 +08:00
|
|
|
"role": "user",
|
|
|
|
"content": [
|
|
|
|
{"type": "text", "text": prompt},
|
|
|
|
{
|
|
|
|
"type": "image_url",
|
|
|
|
"image_url": {
|
|
|
|
"url": f"data:image/jpeg;base64,{image_data}"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2025-06-05 17:36:05 +08:00
|
|
|
}
|
2025-06-05 17:37:11 +08:00
|
|
|
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,
|
|
|
|
)
|
2025-06-05 17:36:05 +08:00
|
|
|
)
|
|
|
|
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
async def process_image_example(lightrag: LightRAG, vision_model_func):
|
|
|
|
"""Example of processing an image"""
|
|
|
|
# Create image processor
|
|
|
|
image_processor = ImageModalProcessor(
|
2025-06-05 17:37:11 +08:00
|
|
|
lightrag=lightrag, modal_caption_func=vision_model_func
|
2025-06-05 17:36:05 +08:00
|
|
|
)
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
# Prepare image content
|
|
|
|
image_content = {
|
|
|
|
"img_path": "image.jpg",
|
|
|
|
"img_caption": ["Example image caption"],
|
2025-06-05 17:37:11 +08:00
|
|
|
"img_footnote": ["Example image footnote"],
|
2025-06-05 17:36:05 +08:00
|
|
|
}
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
# Process image
|
|
|
|
description, entity_info = await image_processor.process_multimodal_content(
|
|
|
|
modal_content=image_content,
|
|
|
|
content_type="image",
|
|
|
|
file_path="image_example.jpg",
|
2025-06-05 17:37:11 +08:00
|
|
|
entity_name="Example Image",
|
2025-06-05 17:36:05 +08:00
|
|
|
)
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
print("Image Processing Results:")
|
|
|
|
print(f"Description: {description}")
|
|
|
|
print(f"Entity Info: {entity_info}")
|
|
|
|
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
async def process_table_example(lightrag: LightRAG, llm_model_func):
|
|
|
|
"""Example of processing a table"""
|
|
|
|
# Create table processor
|
|
|
|
table_processor = TableModalProcessor(
|
2025-06-05 17:37:11 +08:00
|
|
|
lightrag=lightrag, modal_caption_func=llm_model_func
|
2025-06-05 17:36:05 +08:00
|
|
|
)
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
# Prepare table content
|
|
|
|
table_content = {
|
|
|
|
"table_body": """
|
|
|
|
| Name | Age | Occupation |
|
|
|
|
|------|-----|------------|
|
|
|
|
| John | 25 | Engineer |
|
|
|
|
| Mary | 30 | Designer |
|
|
|
|
""",
|
|
|
|
"table_caption": ["Employee Information Table"],
|
2025-06-05 17:37:11 +08:00
|
|
|
"table_footnote": ["Data updated as of 2024"],
|
2025-06-05 17:36:05 +08:00
|
|
|
}
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
# Process table
|
|
|
|
description, entity_info = await table_processor.process_multimodal_content(
|
|
|
|
modal_content=table_content,
|
|
|
|
content_type="table",
|
|
|
|
file_path="table_example.md",
|
2025-06-05 17:37:11 +08:00
|
|
|
entity_name="Employee Table",
|
2025-06-05 17:36:05 +08:00
|
|
|
)
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
print("\nTable Processing Results:")
|
|
|
|
print(f"Description: {description}")
|
|
|
|
print(f"Entity Info: {entity_info}")
|
|
|
|
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
async def process_equation_example(lightrag: LightRAG, llm_model_func):
|
|
|
|
"""Example of processing a mathematical equation"""
|
|
|
|
# Create equation processor
|
|
|
|
equation_processor = EquationModalProcessor(
|
2025-06-05 17:37:11 +08:00
|
|
|
lightrag=lightrag, modal_caption_func=llm_model_func
|
2025-06-05 17:36:05 +08:00
|
|
|
)
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
# Prepare equation content
|
2025-06-05 17:37:11 +08:00
|
|
|
equation_content = {"text": "E = mc^2", "text_format": "LaTeX"}
|
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
# Process equation
|
|
|
|
description, entity_info = await equation_processor.process_multimodal_content(
|
|
|
|
modal_content=equation_content,
|
|
|
|
content_type="equation",
|
|
|
|
file_path="equation_example.txt",
|
2025-06-05 17:37:11 +08:00
|
|
|
entity_name="Mass-Energy Equivalence",
|
2025-06-05 17:36:05 +08:00
|
|
|
)
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
print("\nEquation Processing Results:")
|
|
|
|
print(f"Description: {description}")
|
|
|
|
print(f"Entity Info: {entity_info}")
|
|
|
|
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
async def initialize_rag(api_key: str, base_url: str = None):
|
|
|
|
rag = LightRAG(
|
|
|
|
working_dir=WORKING_DIR,
|
|
|
|
embedding_func=lambda texts: openai_embed(
|
|
|
|
texts,
|
|
|
|
model="text-embedding-3-large",
|
|
|
|
api_key=api_key,
|
|
|
|
base_url=base_url,
|
|
|
|
),
|
2025-06-05 17:37:11 +08:00
|
|
|
llm_model_func=lambda prompt,
|
|
|
|
system_prompt=None,
|
|
|
|
history_messages=[],
|
|
|
|
**kwargs: openai_complete_if_cache(
|
2025-06-05 17:36:05 +08:00
|
|
|
"gpt-4o-mini",
|
|
|
|
prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
api_key=api_key,
|
|
|
|
base_url=base_url,
|
|
|
|
**kwargs,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
await rag.initialize_storages()
|
|
|
|
await initialize_pipeline_status()
|
|
|
|
|
|
|
|
return rag
|
|
|
|
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
def main():
|
|
|
|
"""Main function to run the example"""
|
2025-06-05 17:37:11 +08:00
|
|
|
parser = argparse.ArgumentParser(description="Modal Processors Example")
|
|
|
|
parser.add_argument("--api-key", required=True, help="OpenAI API key")
|
|
|
|
parser.add_argument("--base-url", help="Optional base URL for API")
|
|
|
|
parser.add_argument(
|
|
|
|
"--working-dir", "-w", default=WORKING_DIR, help="Working directory path"
|
|
|
|
)
|
2025-06-05 17:36:05 +08:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Run examples
|
|
|
|
asyncio.run(main_async(args.api_key, args.base_url))
|
|
|
|
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
async def main_async(api_key: str, base_url: str = None):
|
|
|
|
# Initialize LightRAG
|
|
|
|
lightrag = await initialize_rag(api_key, base_url)
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
# Get model functions
|
|
|
|
llm_model_func = get_llm_model_func(api_key, base_url)
|
|
|
|
vision_model_func = get_vision_model_func(api_key, base_url)
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
# Run examples
|
|
|
|
await process_image_example(lightrag, vision_model_func)
|
|
|
|
await process_table_example(lightrag, llm_model_func)
|
|
|
|
await process_equation_example(lightrag, llm_model_func)
|
|
|
|
|
2025-06-05 17:37:11 +08:00
|
|
|
|
2025-06-05 17:36:05 +08:00
|
|
|
if __name__ == "__main__":
|
2025-06-05 17:37:11 +08:00
|
|
|
main()
|