Files
ragflow/sdk/python/test.py
T
Shuo Liu 4d9d56ba3a fix: Introduce argparse to resolve hardcoded parameter values in the SDK test file (#16763)
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 <wangq8@outlook.com>
2026-08-28 16:29:41 +08:00

91 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
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
You must provide api-key and agent-id via commandline arguments or environment variables.
baseurl has a sensible default value.
----------------------------------------------------------------------------------
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()
print("\n==================== Miss R =====================\n")
print("Hello. What can I do for you?")
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()