|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import argparse |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +# Add project root to path |
| 8 | +sys.path.append(str(Path(__file__).parent.parent)) |
| 9 | + |
| 10 | +from raganything import RAGAnything, RAGAnythingConfig |
| 11 | +from lightrag.llm.openai import openai_complete_if_cache, openai_embed |
| 12 | +from lightrag.utils import EmbeddingFunc |
| 13 | +from dotenv import load_dotenv |
| 14 | + |
| 15 | +# Load environment variables |
| 16 | +load_dotenv(dotenv_path=".env", override=False) |
| 17 | + |
| 18 | + |
| 19 | +async def run_test(file_path: str): |
| 20 | + print(f"Starting Integration Test with {file_path}") |
| 21 | + |
| 22 | + if not os.path.exists(file_path): |
| 23 | + print(f"Dataset not found at: {file_path}") |
| 24 | + return |
| 25 | + |
| 26 | + print(f"Dataset found: {file_path}") |
| 27 | + |
| 28 | + # Configuration |
| 29 | + config = RAGAnythingConfig( |
| 30 | + working_dir="./rag_storage_test_core", |
| 31 | + enable_image_processing=False, |
| 32 | + enable_table_processing=True, |
| 33 | + enable_equation_processing=False, |
| 34 | + ) |
| 35 | + |
| 36 | + # API Configuration |
| 37 | + api_key = os.getenv("LLM_BINDING_API_KEY", "lm-studio") |
| 38 | + base_url = os.getenv("LLM_BINDING_HOST", "http://localhost:1234/v1") |
| 39 | + llm_model = os.getenv("LLM_MODEL", "openai/gpt-oss-20b") |
| 40 | + |
| 41 | + # LLM Function |
| 42 | + def llm_model_func(prompt, system_prompt=None, history_messages=[], **kwargs): |
| 43 | + return openai_complete_if_cache( |
| 44 | + llm_model, |
| 45 | + prompt, |
| 46 | + system_prompt=system_prompt, |
| 47 | + history_messages=history_messages, |
| 48 | + api_key=api_key, |
| 49 | + base_url=base_url, |
| 50 | + **kwargs, |
| 51 | + ) |
| 52 | + |
| 53 | + # Embedding Function |
| 54 | + embedding_dim = int(os.getenv("EMBEDDING_DIM", "768")) |
| 55 | + embedding_model = os.getenv("EMBEDDING_MODEL", "text-embedding-embeddinggemma-300m") |
| 56 | + |
| 57 | + embedding_func = EmbeddingFunc( |
| 58 | + embedding_dim=embedding_dim, |
| 59 | + max_token_size=8192, |
| 60 | + func=lambda texts: openai_embed.func( |
| 61 | + texts, |
| 62 | + model=embedding_model, |
| 63 | + api_key=api_key, |
| 64 | + base_url=base_url, |
| 65 | + ), |
| 66 | + ) |
| 67 | + |
| 68 | + # Initialize RAG |
| 69 | + rag = RAGAnything( |
| 70 | + config=config, |
| 71 | + llm_model_func=llm_model_func, |
| 72 | + embedding_func=embedding_func, |
| 73 | + ) |
| 74 | + |
| 75 | + # Ensure initialized |
| 76 | + await rag._ensure_lightrag_initialized() |
| 77 | + print("RAG Initialized") |
| 78 | + |
| 79 | + # Determine processing method based on file extension |
| 80 | + file_ext = os.path.splitext(file_path)[1].lower() |
| 81 | + |
| 82 | + if file_ext in [".xlsx", ".xls"]: |
| 83 | + print("Processing Excel file...") |
| 84 | + result = await rag.process_excel_file( |
| 85 | + file_path=file_path, |
| 86 | + max_rows=100, |
| 87 | + convert_to_text=True, |
| 88 | + include_summary=True, |
| 89 | + ) |
| 90 | + else: |
| 91 | + print(f"Processing Document file ({file_ext})...") |
| 92 | + # For non-excel, use standard process_document |
| 93 | + await rag.process_document_complete( |
| 94 | + file_path=file_path, output_dir="./output_test", parse_method="auto" |
| 95 | + ) |
| 96 | + result = {"success": True} # assume success if no exception |
| 97 | + |
| 98 | + if isinstance(result, dict) and result.get("success", True): |
| 99 | + print("Processing Successful") |
| 100 | + else: |
| 101 | + print(f"Processing Failed: {result}") |
| 102 | + return |
| 103 | + |
| 104 | + # Query |
| 105 | + query = "Summarize the key information in this document." |
| 106 | + print(f"\nQuerying: {query}") |
| 107 | + try: |
| 108 | + response = await rag.aquery(query, mode="hybrid") |
| 109 | + print(f"Answer: {response}") |
| 110 | + |
| 111 | + if response and len(str(response)) > 10: |
| 112 | + print("Query returned a valid response") |
| 113 | + else: |
| 114 | + print("Query returned empty or short response") |
| 115 | + |
| 116 | + except Exception as e: |
| 117 | + print(f"Query failed: {e}") |
| 118 | + |
| 119 | + |
| 120 | +def main(): |
| 121 | + parser = argparse.ArgumentParser(description="RAGAnything Core Endpoint Test") |
| 122 | + parser.add_argument("file_path", help="Path to the dataset file") |
| 123 | + args = parser.parse_args() |
| 124 | + |
| 125 | + asyncio.run(run_test(args.file_path)) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments