PDF / examples /free-api.py
BirkhoffLee's picture
example: 增加了示例 API
bde5fc0 unverified
#!/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()