From 4d9d56ba3a3b079326eff81ab91ec6fefff7e7d4 Mon Sep 17 00:00:00 2001 From: Shuo Liu <13838152117@139.com> Date: Fri, 28 Aug 2026 16:29:41 +0800 Subject: [PATCH] fix: Introduce argparse to resolve hardcoded parameter values in the SDK test file (#16763) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix #16759 — SDK test script now supports CLI args + env vars (priority: CLI > env > defaults). API key required with clear errors. Base URL and agent ID keep defaults. Docs added. --------- Co-authored-by: Wang Qi --- sdk/python/test.py | 97 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 12 deletions(-) diff --git a/sdk/python/test.py b/sdk/python/test.py index 8cc0ae0394..4c9a7d90a9 100644 --- a/sdk/python/test.py +++ b/sdk/python/test.py @@ -1,17 +1,90 @@ -from .ragflow_sdk import RAGFlow +""" +You may use this script to test features related to the Python SDK. +Execution instructions are as follows: +>>> python test.py --api-key yourkeys --base-url YourBaseUrl --agent-id YourAgentID -rag_object = RAGFlow(api_key="ragflow-FDfRECsXDRagsKPxb_EfZdDPcmngavSgYEzbU_Blgq4", base_url="http://localhost:9222") -assistant = rag_object.get_agent("b0bc46e43dfc11f1b4ff84ba59bc54d9") -session = assistant.create_session() +You must provide api-key and agent-id via command‑line arguments or environment variables. +base‑url has a sensible default value. -print("\n==================== Miss R =====================\n") -print("Hello. What can I do for you?") +---------------------------------------------------------------------------------- +Supported environment variables: +- RAGFLOW_API_KEY: RAGFlow access API key (required) +- RAGFLOW_BASE_URL: RAGFlow service address (optional, default http://localhost:9380) +- RAGFLOW_AGENT_ID: Target agent unique identifier (required) + +You can configure the environment variables with the following commands on Linux/Mac: +>>> export RAGFLOW_API_KEY=YourApiKey +>>> export RAGFLOW_BASE_URL=YourBaseUrl +>>> export RAGFLOW_AGENT_ID=YourAgentID + +For Windows, use the commands below (PowerShell): +>>> $env:RAGFLOW_API_KEY=YourApiKey +>>> $env:RAGFLOW_BASE_URL=YourBaseUrl +>>> $env:RAGFLOW_AGENT_ID=YourAgentID + +Mixed usage is also supported. +Example: The API key is already set via environment variable, and you only want to temporarily override the agent ID: +>>> python test.py --agent-id YourAgentID +""" + +from argparse import Namespace, ArgumentParser + +# Add current script directory to Python module search path to locate ragflow_sdk package +import os +import sys + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from ragflow_sdk import RAGFlow + + +def parse_args() -> "Namespace": + """ + Parse Script Input Parameters + + Args: + None + Returns: + Namespace + + """ + parser = ArgumentParser(description="RAGFlow Agent Test Script") + parser.add_argument("--api-key", type=str, default=os.getenv("RAGFLOW_API_KEY", ""), help="RAGFlow API Key; read from env RAGFLOW_API_KEY if not specified (required)") + parser.add_argument("--base-url", type=str, default=os.getenv("RAGFLOW_BASE_URL", "http://localhost:9380"), help="RAGFlow service base url; read from env RAGFLOW_BASE_URL if not specified") + parser.add_argument("--agent-id", type=str, default=os.getenv("RAGFLOW_AGENT_ID", ""), help="Target Agent ID; read from env RAGFLOW_AGENT_ID if not specified") + return parser.parse_args() + + +def main(): + """ + Core Test Functionality + + Args: + None + + Returns: + None + """ + args = parse_args() + if not args.api_key.strip(): + raise SystemExit("Error: API key is required. Pass it via --api-key argument or set environment variable RAGFLOW_API_KEY.") + if not args.agent_id.strip(): + raise SystemExit("Error: Agent ID is required. Pass it via --agent-id argument or set environment variable RAGFLOW_AGENT_ID.") + rag_object = RAGFlow(api_key=args.api_key, base_url=args.base_url) + assistant = rag_object.get_agent(args.agent_id) + session = assistant.create_session() -while True: - question = input("\n==================== User =====================\n> ") print("\n==================== Miss R =====================\n") + print("Hello. What can I do for you?") - cont = "" - for ans in session.ask(question, stream=True): - print(ans.content[len(cont) :], end="", flush=True) - cont = ans.content + while True: + question = input("\n==================== User =====================\n> ") + print("\n==================== Miss R =====================\n") + + cont = "" + for ans in session.ask(question, stream=True): + print(ans.content[len(cont) :], end="", flush=True) + cont = ans.content + + +if __name__ == "__main__": + main()