import argparse import json import os import sys import requests def call_unified(api_url: str, message: str, message_type: str, language: str | None, history: list[str] | None = None) -> dict: payload = { "message": message, "message_type": message_type, } if language: payload["language"] = language if history: payload["history"] = history r = requests.post(f"{api_url}/api/chat/unified", json=payload, timeout=120) try: data = r.json() except Exception: data = {"status_code": r.status_code, "text": r.text} return {"status_code": r.status_code, "data": data} def pretty(obj: dict) -> str: return json.dumps(obj, indent=2, ensure_ascii=False) def interactive_mode(api_url: str, language: str | None) -> int: print("Interactive unified chat tester. Type 'quit' to exit.\n") history: list[str] = [] while True: t = input("Type (text/audio/image) [text]: ").strip().lower() or "text" if t not in {"text", "audio", "image"}: print("Invalid type. Use text/audio/image.") continue msg = input("Message (text or URL): ").strip() if msg.lower() in {"quit", "exit"}: return 0 res = call_unified(api_url, msg, t, language, history=history[-6:]) print(pretty(res)) if res.get("status_code") == 200 and isinstance(res.get("data"), dict): # Append last user message to history for context history.append(msg) def main() -> int: parser = argparse.ArgumentParser(description="Try the unified AI endpoint with text/image/audio") parser.add_argument("--api", default=os.environ.get("API_URL", "http://127.0.0.1:8000"), help="Base API URL") parser.add_argument("--message", help="Text or URL to test") parser.add_argument("--type", dest="type_", default="text", choices=["text", "audio", "image"], help="Message type") parser.add_argument("--language", help="Optional language for response (auto-detected if omitted)") parser.add_argument("--history", nargs="*", help="Optional prior messages (space-separated)") parser.add_argument("--interactive", action="store_true", help="Interactive prompt mode") args = parser.parse_args() if args.interactive: return interactive_mode(args.api, args.language) or 0 if not args.message: print("--message is required when not in --interactive mode", file=sys.stderr) return 2 result = call_unified(args.api, args.message, args.type_, args.language, history=args.history) print(pretty(result)) return 0 if __name__ == "__main__": raise SystemExit(main())