File size: 2,686 Bytes
411a994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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())