File size: 1,289 Bytes
bde5fc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""最小版 api1 主接口调用示例。"""

from __future__ import annotations

import json

import httpx


BASE_URL = "https://api1.pdf2zh-next.com/chatproxy"
TIMEOUT_SECONDS = 30.0


def build_translate_prompt(text: str, lang_out: str = "zh") -> str:
    """复刻上游的翻译 prompt。"""
    return (
        r"You are a professional,authentic machine translation engine."
        "\n\n"
        + r";; Treat next line as plain text input and translate it into "
        + lang_out
        + r", output translation ONLY. If translation is unnecessary "
        + r"(e.g. proper nouns, codes, {{1}}, etc. ), return the original text. "
        + r"NO explanations. NO notes. Input:"
        + "\n\n"
        + text
    )


def main() -> None:
    """直接调用主接口。"""
    payload = {
        "text": build_translate_prompt("Hello world"),
    }

    with httpx.Client(timeout=TIMEOUT_SECONDS) as client:
        response = client.post(BASE_URL, json=payload)
        response.raise_for_status()
        body = response.json()

    print("=== request ===")
    print(json.dumps(payload, ensure_ascii=False, indent=2))
    print("\n=== response ===")
    print(json.dumps(body, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()